add .gitignore
[tedtools.git] / connection.h
1 /*
2  * Copyright (c) 2004 Teodor Sigaev <teodor@sigaev.ru>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *      notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *      notice, this list of conditions and the following disclaimer in the
12  *      documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the author nor the names of any co-contributors
14  *      may be used to endorse or promote products derived from this software
15  *      without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #ifndef __CONNECTION_H__
31 #define __CONNECTION_H__
32
33 #include <sys/types.h>
34 #include <sys/time.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38
39 #include "tools.h"
40
41 #define CS_OK           0
42 #define CS_INPROCESS    1
43 #define CS_CONNECTED    2
44 #define CS_READ  3
45 #define CS_SEND  4
46 #define CS_WAIT  5
47 #define CS_ERROR        6
48 #define CS_FINISHSEND   7
49 #define CS_FINISHREAD   8
50 #define CS_TIMEOUT      9
51 #define CS_CLOSED       10      
52 #define CS_NOTINITED    11
53 #define CS_AGAIN        12
54 #define CS_FINISH       13
55
56
57 #define READ_INCRIMENT_BUFSIZ 1024
58
59 /*
60  * any message shoulbe compatible with TCMsg struct
61  */
62 typedef struct {
63                 /* len and type should be transferred in network byte order */
64         u_int32_t       len; 
65         u_int32_t       type;
66         char        data[1];
67 } TCMsg;
68
69 #define TCMSGHDRSZ      offsetof(TCMsg, data)
70
71 /* tcp */
72 typedef struct {
73         /* I/O buffer */
74         u_int32_t  len;
75         char    *ptr;
76         TCMsg   *buf; /* send buf */
77
78         /* internal fields */
79         int     fd;
80         u_int32_t
81                 readyio:1,  
82                 state:29;
83         struct sockaddr_in serv_addr;
84
85         /* external link */
86         void*           data;
87 } TC_Connection;
88
89 #define TCCONNHDRSZ  offsetof(TC_Connection, data)
90
91 TC_Connection *TC_fillConnection( TC_Connection *cs, char *name, u_int32_t port );
92 TC_Connection* TC_AcceptTcp(TC_Connection *cs);
93 u_int32_t TC_ClientInitConnection(TC_Connection *cs, char *name, u_int32_t port);
94 u_int32_t TC_ServerInitConnect( TC_Connection *cs );
95 u_int32_t TC_ServerConnect( TC_Connection *cs, int timeout );
96 /*
97  * TC_Send doesn't promise to keep TC_Connection->buf unchanged
98  */
99 u_int32_t TC_Send( TC_Connection *cs );
100 u_int32_t TC_Read( TC_Connection *cs, size_t maxsize );
101 u_int32_t TC_Talk( TC_Connection *cs, size_t maxsize );
102 void TC_FreeConnection( TC_Connection *cs );
103 int TC_ReadyIO( TC_Connection **cs, int number, int timeout );
104
105 /* udp */
106
107
108 typedef struct {
109         char    *host;
110         u_int32_t       port;
111         TCMsg   *msg;
112
113         /* private members */
114         int             sockfd;
115         struct sockaddr_in host_addr;
116 } Msg; 
117
118 /* 
119  * Udp server loop:
120  * TC_Connection  conn,*connptr;
121  * conn.fd = TC_AcceptUdp("127.0.0.1", 5432);
122  * conn.state = CS_READ;
123  * connptr = &conn;
124  * while(1) {
125  *      if ( TC_ReadyIO( &connptr, 1, 100 ) ) {
126  *              Msg m; 
127  *              if ( TC_getMsg(conn.fd, &m) == CS_OK ) {
128  *                      //do something
129  *              } 
130  *      }
131  * }
132  * close(conn.fd);
133  *
134  * Udp client send:
135  * Msg msg;
136  * msg.host     = "127.0.0.1";
137  * msg.port     = 5432;
138  * msg.sockfd =-1;
139  * msg.msg = GOTFILLEDPMSG();
140  * if ( TC_sendMsg(&msg)!=CS_OK ) {
141  *      //Very bad
142  * }
143  * msg.msg = GOTFILLEDPMSG();
144  * if ( TC_sendMsg(&msg)!=CS_OK ) {
145  *      //Very bad
146  * }
147  * TC_closefd(&msg);
148  */ 
149
150 int TC_AcceptUdp(char *host, int port);
151 u_int32_t TC_getMsg( int sockfd, Msg *msg );
152 u_int32_t TC_sendMsg( Msg *msg );
153 void      TC_closefd( Msg *msg );
154
155 /*
156  * Connection pool
157  */
158
159 typedef struct {
160         u_int32_t       len;
161         u_int32_t       number;
162         TC_Connection   **conn;
163 } PoolConnection;
164
165 void TC_addConnection(PoolConnection *pool, TC_Connection *c);
166 void TC_deleteConnectionByN(PoolConnection *pool, int n);
167 void TC_deleteConnectionByC(PoolConnection *pool, TC_Connection *c);
168
169 #ifndef HAVE_HSTRERROR
170 #define hstrerror       strerror
171 #define h_errno         errno
172 #endif
173
174 #endif
175