* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
+ * notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
* 3. Neither the name of the author nor the names of any co-contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
#include <netinet/in.h>
#include <arpa/inet.h>
-
+#include "tools.h"
#define CS_OK 0
#define CS_INPROCESS 1
#define CS_CONNECTED 2
-#define CS_READ 3
-#define CS_SEND 4
-#define CS_WAIT 5
-#define CS_ERROR 6
+#define CS_READ 3
+#define CS_SEND 4
+#define CS_WAIT 5
+#define CS_ERROR 6
#define CS_FINISHSEND 7
#define CS_FINISHREAD 8
#define CS_TIMEOUT 9
#define READ_INCRIMENT_BUFSIZ 1024
+/*
+ * any message shoulbe compatible with TCMsg struct
+ */
+typedef struct {
+ /* len and type should be transferred in network byte order */
+ u_int32_t len;
+ u_int32_t type;
+ char data[1];
+} TCMsg;
+
+#define TCMSGHDRSZ offsetof(TCMsg, data)
+
+/* tcp */
typedef struct {
/* I/O buffer */
- u_int32_t len;
- char *buf;
- char *ptr;
+ u_int32_t len;
+ char *ptr;
+ TCMsg *buf; /* send buf */
/* internal fields */
- int fd;
- u_int32_t
+ int fd;
+ u_int32_t
readyio:1,
state:29;
- struct sockaddr_in serv_addr;
+ struct sockaddr_in serv_addr;
/* external link */
void* data;
} TC_Connection;
-#define TCCONNHDRSZ ( sizeof(TC_Connection) - sizeof(void*) )
+#define TCCONNHDRSZ offsetof(TC_Connection, data)
TC_Connection *TC_fillConnection( TC_Connection *cs, char *name, u_int32_t port );
TC_Connection* TC_AcceptTcp(TC_Connection *cs);
u_int32_t TC_ClientInitConnection(TC_Connection *cs, char *name, u_int32_t port);
u_int32_t TC_ServerInitConnect( TC_Connection *cs );
u_int32_t TC_ServerConnect( TC_Connection *cs, int timeout );
+/*
+ * TC_Send doesn't promise to keep TC_Connection->buf unchanged
+ */
u_int32_t TC_Send( TC_Connection *cs );
u_int32_t TC_Read( TC_Connection *cs, size_t maxsize );
u_int32_t TC_Talk( TC_Connection *cs, size_t maxsize );
void TC_FreeConnection( TC_Connection *cs );
int TC_ReadyIO( TC_Connection **cs, int number, int timeout );
-typedef struct {
- u_int32_t len;
- u_int32_t type;
- char data[1];
-} TCMsg;
-
-#define TCMSGHDRSZ (2*sizeof(u_int32_t))
+/* udp */
-/* udp */
typedef struct {
char *host;
u_int32_t port;
*
* Udp client send:
* Msg msg;
- * msg.host = "127.0.0.1";
- * msg.port = 5432;
+ * msg.host = "127.0.0.1";
+ * msg.port = 5432;
* msg.sockfd =-1;
* msg.msg = GOTFILLEDPMSG();
* if ( TC_sendMsg(&msg)!=CS_OK ) {
- * //Very bad
+ * //Very bad
* }
* msg.msg = GOTFILLEDPMSG();
* if ( TC_sendMsg(&msg)!=CS_OK ) {
- * //Very bad
+ * //Very bad
* }
* TC_closefd(&msg);
*/
u_int32_t TC_sendMsg( Msg *msg );
void TC_closefd( Msg *msg );
+/*
+ * Connection pool
+ */
+
typedef struct {
- u_int32_t len;
- u_int32_t number;
- TC_Connection **conn;
+ u_int32_t len;
+ u_int32_t number;
+ TC_Connection **conn;
} PoolConnection;
void TC_addConnection(PoolConnection *pool, TC_Connection *c);
void TC_deleteConnectionByN(PoolConnection *pool, int n);
void TC_deleteConnectionByC(PoolConnection *pool, TC_Connection *c);
+#ifndef HAVE_HSTRERROR
+#define hstrerror strerror
+#define h_errno errno
+#endif
+
#endif