2 * Copyright (c) 2004 Teodor Sigaev <teodor@sigaev.ru>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
30 #ifndef __CONNECTION_H__
31 #define __CONNECTION_H__
33 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
42 #define CS_INPROCESS 1
43 #define CS_CONNECTED 2
48 #define CS_FINISHSEND 7
49 #define CS_FINISHREAD 8
52 #define CS_NOTINITED 11
57 #define READ_INCRIMENT_BUFSIZ 1024
60 * any message shoulbe compatible with TCMsg struct
63 /* len and type should be transferred in network byte order */
69 #define TCMSGHDRSZ offsetof(TCMsg, data)
76 TCMsg *buf; /* send buf */
83 struct sockaddr_in serv_addr;
89 #define TCCONNHDRSZ offsetof(TC_Connection, data)
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 );
97 * TC_Send doesn't promise to keep TC_Connection->buf unchanged
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 );
113 /* private members */
115 struct sockaddr_in host_addr;
120 * TC_Connection conn,*connptr;
121 * conn.fd = TC_AcceptUdp("127.0.0.1", 5432);
122 * conn.state = CS_READ;
125 * if ( TC_ReadyIO( &connptr, 1, 100 ) ) {
127 * if ( TC_getMsg(conn.fd, &m) == CS_OK ) {
136 * msg.host = "127.0.0.1";
139 * msg.msg = GOTFILLEDPMSG();
140 * if ( TC_sendMsg(&msg)!=CS_OK ) {
143 * msg.msg = GOTFILLEDPMSG();
144 * if ( TC_sendMsg(&msg)!=CS_OK ) {
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 );
162 TC_Connection **conn;
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);
169 #ifndef HAVE_HSTRERROR
170 #define hstrerror strerror
171 #define h_errno errno