00001 /* 00002 $Id: network_delivery_socket.h,v 1.2 2001/02/28 15:06:03 sphair Exp $ 00003 00004 ------------------------------------------------------------------------ 00005 ClanLib, the platform independent game SDK. 00006 00007 This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE 00008 version 2. See COPYING for details. 00009 00010 For a total list of contributers see CREDITS. 00011 00012 ------------------------------------------------------------------------ 00013 00014 File purpose: 00015 Lowlevel socket communication classes. 00016 */ 00017 00018 #ifndef header_network_delivery_socket 00019 #define header_network_delivery_socket 00020 00021 #ifdef WIN32 00022 #include <windows.h> 00023 #else 00024 #include <sys/time.h> 00025 #include <sys/types.h> 00026 #include <sys/socket.h> 00027 #include <arpa/inet.h> 00028 #include <netinet/in.h> 00029 #include <unistd.h> 00030 #include <errno.h> 00031 #include <fcntl.h> 00032 #endif 00033 00034 #include <string.h> 00035 #include <list> 00036 #include <queue> 00037 00038 #include "Network/Generic/network_delivery_impl.h" 00039 00040 #ifdef WIN32 00041 #define CL_INVALID_SOCKET INVALID_SOCKET 00042 #define CL_SOCKET_ERROR SOCKET_ERROR 00043 #define CL_SOCKET SOCKET 00044 const char *get_win32_sock_error(DWORD err); 00045 #define PRINT_SOCK_ERROR() get_win32_sock_error(WSAGetLastError()) 00046 typedef int socklen_t; 00047 #else 00048 #include <netdb.h> 00049 #define CL_INVALID_SOCKET -1 00050 #define CL_SOCKET_ERROR -1 00051 #define closesocket(a) close(a) 00052 #define PRINT_SOCK_ERROR() strerror(errno) 00053 #define CL_SOCKET int 00054 // where is this one defined?? 00055 #define TCP_NODELAY 1 00056 #endif 00057 00058 class CL_Connections_Unix; 00059 class CL_UDPConnection; 00060 00061 class CL_UniformSocket : public CL_Connection 00062 { 00063 public: 00064 virtual int get_socket() { return sock; } 00065 00066 CL_UniformSocket(CL_ConnectionProvider *provider); 00067 virtual ~CL_UniformSocket(); 00068 00069 // CL_UniformSocket defined virtual function 00070 virtual bool try_connect(unsigned long remote_ip_network_format, int port); 00071 virtual bool init_socket(int init_socket=-1); 00072 00073 // CL_Connection inherited functions 00074 virtual bool peek(); 00075 virtual CL_ConnectionPacket receive(); 00076 virtual void send(CL_ConnectionPacket message); 00077 virtual bool connection_lost(); 00078 00079 bool send_avail(); 00080 00081 protected: 00082 CL_SOCKET sock; 00083 bool is_connection_lost; 00084 00085 char *cur_message; 00086 int cur_message_size; 00087 00088 unsigned long read_int(); 00089 void write_int(unsigned long value); 00090 inline void write_data(void *data, unsigned int size); 00091 00092 // read from socket: 00093 enum 00094 { 00095 expect_magic, 00096 expect_packet_size, 00097 expect_packet_data, 00098 packet_finished 00099 } recv_state; 00100 00101 bool read_avail(); 00102 bool require_avail(unsigned int size); 00103 bool get_avail(void *buf, unsigned int size); 00104 00105 CL_ConnectionProvider *provider; 00106 00107 private: 00108 // std::deque<char> recv_buffer; 00109 std::string recv_buffer; 00110 std::queue<std::string> send_buffer; 00111 int send_pos; 00112 }; 00113 00114 class CL_UniformAcceptSocket : public CL_UniformSocket 00115 { 00116 public: 00117 CL_UniformAcceptSocket(CL_ConnectionProvider *provider); 00118 virtual ~CL_UniformAcceptSocket(); 00119 00120 bool bind(int port); 00121 00122 CL_UniformSocket *accept(); 00123 int get_port() { return port; } 00124 00125 // CL_UniformSocket inherited functions 00126 virtual bool try_connect(unsigned long remote_ip_network_format, int port); 00127 00128 // CL_Connection inherited functions 00129 virtual bool peek(); 00130 virtual CL_ConnectionPacket receive(); 00131 virtual void send(CL_ConnectionPacket message); 00132 virtual bool connection_lost(); 00133 00134 private: 00135 bool is_inited; 00136 int port; 00137 }; 00138 00139 00140 class CL_UniformUDPConnection : public CL_UDPConnection 00141 { 00142 public: 00143 virtual int get_socket() { return sock; } 00144 00145 CL_UniformUDPConnection(); 00146 virtual ~CL_UniformUDPConnection(); 00147 00148 bool bind(unsigned int port); 00149 00150 // CL_UDPConnection inherited functions 00151 virtual bool peek(); 00152 virtual CL_UDPConnectionPacket receive(); 00153 virtual void send(CL_UDPConnectionPacket message); 00154 virtual void broadcast(CL_UDPConnectionPacket message); 00155 virtual unsigned int get_port(); 00156 00157 protected: 00158 int sock; 00159 int port; 00160 }; 00161 00162 #endif
1.2.6 written by Dimitri van Heesch,
© 1997-2001