00001 #ifndef CTL_SOCKET_H
00002 #define CTL_SOCKET_H
00003
00016
00017
00018
00019
00020 #ifndef MAX_SOCKETS
00021 #define MAX_SOCKETS 1024
00022 #endif
00023 #if !defined(FD_SETSIZE) || (FD_SETSIZE < MAX_SOCKETS)
00024 #define FD_SETSIZE MAX_SOCKETS
00025 #endif
00026
00027 #ifdef WIN32
00028 #include <winsock2.h>
00029 #else
00030 #include <netdb.h>
00031 #include <netinet/in.h>
00032 #include <arpa/inet.h>
00033 #include <sys/socket.h>
00034 #include <sys/types.h>
00035 #include <sys/uio.h>
00036 #include <unistd.h>
00037 #include <errno.h>
00038 typedef int SOCKET;
00039 #endif
00040 #ifndef RINGBUFF_H
00041 #include "ctl/ringbuff.h"
00042 #endif
00043 #ifndef LL_H
00044 #include "ctl/ll.h"
00045 #endif
00046 #ifndef CTL_MSTIME_H
00047 #include "ctl/mstime.h"
00048 #endif
00049 #ifndef CTL_HEAP_H
00050 #include "ctl/heap.h"
00051 #endif
00052
00053 ppCPP(extern "C" {)
00054
00055 struct Socket;
00056
00060 typedef struct SockVTAB
00061 {
00062 void (*ReadCycle)(struct Socket*);
00063 void (*WriteCycle)(struct Socket*);
00064 void (*Close)(struct Socket*);
00065 } SockVTAB;
00066
00070 typedef enum Socket_Type
00071 {
00072 stUninitialized,
00073 stSendingFinal,
00074 stListener,
00075 stLogin,
00076 stUser,
00077 stClient,
00078 stRaw,
00079 } Socket_Type;
00080
00084 typedef struct Socket
00085 {
00086 const SockVTAB* vtab;
00087 Socket_Type type;
00088 char label[32];
00089 uint32 id;
00090 SOCKET socket;
00091 mstime created;
00092 mstime lastmsg;
00093 mstime timeout;
00094 dll_decllink( struct Socket, active );
00096
00097
00098
00099
00100 union
00101 {
00102 struct sockaddr address;
00103 struct sockaddr_in ip;
00104
00105 uint8 socka_buff[64];
00106 };
00107
00108
00109
00110
00111
00112
00113 uint8* pOutput;
00114 RingBuff output;
00116
00117
00118
00119
00120
00121 uint8* pInput;
00122 RingBuff input;
00124
00125
00126
00127 uint8* pAppHeap;
00128 ctl_heap appheap;
00135 void (*appReceived)( struct Socket* sock, ctl_serial* serial );
00136
00141 void (*appDisconnect)( struct Socket* sock);
00142 } Socket;
00146 #define Socket_ShouldIgnore(sock) ( (sock)->type <= stSendingFinal )
00147
00148
00149
00150
00151
00152 bool SocketsInit( size_t input, size_t output, size_t heap );
00153 void SocketsShutdown(void);
00154 bool SocketsCycle( unsigned ms );
00155 const char* SocketsLastErrorString(void);
00156 void SocketsLogLastError( unsigned mask, const char*fmt, ... );
00157 struct addrinfo *SocketsParseAddress( const char* szAddress );
00158 void SocketsLogHostent( uint32 logmask, const char* name );
00159 typedef void (*NewSocketCallback)( Socket* socket );
00160 NewSocketCallback SocketsSetLogin( NewSocketCallback callback );
00161 NewSocketCallback SocketsSetUser( NewSocketCallback callback );
00162 typedef const char* (*MLCallback)(void);
00163 MLCallback SocketsSetHTTP( MLCallback callback );
00164 MLCallback SocketsSetFlashPolicy( MLCallback callback );
00165
00166
00167
00168
00169 uint32 SocketID( const Socket* sock );
00170 Socket* SocketLut( uint32 id );
00171 Socket* SocketFind( const char* label );
00172
00173
00174
00175
00176 dll_externlist(Socket,active, socksActive);
00181 #define socket_foreach( sock ) dll_foreach(Socket,active, socksActive, sock )
00182
00186 #define socket_foreach_const( sock ) dll_foreach_const(Socket,active, socksActive, sock )
00187
00191 #define socket_foreach_deletable( sock ) dll_foreach_deletable(Socket,active, socksActive, sock )
00192
00197 #define socket_foreach_type( sock, stype ) dll_foreach_deletable(Socket,active, socksActive, sock ) if( sock->type == (stype) )
00198
00199
00200
00201
00202 Socket* Socket_Create_Client( const char* address );
00203 Socket* Socket_Create_Listener( const char* label, const char* address );
00204 Socket* Socket_Create_Login( SOCKET isock );
00205 Socket* Socket_Create_User( Socket* sock, const char* label );
00206 Socket* Socket_Create_Raw( Socket* sock );
00207 void Socket_Close( Socket* sock );
00208 Socket* Socket_Create(void);
00209 Socket* Socket_Customize( Socket* sock, size_t input, size_t output, size_t heap );
00210 void Socket_LogError( Socket* sock, unsigned mask, const char*fmt, ... );
00211
00212 #ifdef DEBUG
00213 void Socket_Validate( Socket* sock );
00214 #else
00215 #define Socket_Validate(sock)
00216 #endif
00217
00218
00219
00220
00221 bool Socket_Write( Socket* sock, const void* buff, size_t length );
00222 bool Socket_Alloc_Serial( Socket* sock, ctl_serial* serial, size_t length );
00223 bool Socket_Write_Serial( Socket* sock, const ctl_serial* serial );
00224
00225
00226 #define IP_FORMAT "%s:%u"
00227 #define IP_ARGS_SOCK(sock) inet_ntoa((sock)->ip.sin_addr), ntohs((sock)->ip.sin_port)
00228 #define IP_ARGS_SADDR(saddr) inet_ntoa(((struct sockaddr_in*)(saddr))->sin_addr), ntohs(((struct sockaddr_in*)(saddr))->sin_port)
00229 #ifdef NDEBUG
00230 #define traceip(msg,saddr)
00231 #else
00232 #define traceip(msg,saddr) trace(( msg IP_FORMAT "\n", IP_ARGS_SADDR(saddr) ))
00233 #endif
00234 #define SocketLogIP(sock,mask,msg) debug_log( (mask), msg IP_FORMAT " (%s)\n", IP_ARGS_SOCK(sock), (sock)->label )
00235
00236 ppCPP(})
00237
00238 #endif
00239