tcpipDevice.cpp
00001
00002
00003
00004
00005
00006
00007 #include "tcpipDevice.h"
00008 #include <stdlib.h>
00009 #include <list>
00010
00011 #ifndef SocketSetBaseNum
00012 #define SocketSetBaseNum 1
00013 #endif
00014
00015
00016 #if !HAVE_CONFIG_H || HAVE_LIBSDL_NET
00017 bool TcpipDevice::initialized = false;
00018 int TcpipDevice::sockets_num = 0;
00019 int TcpipDevice::sockets_max = SocketSetBaseNum;
00020 SDLNet_SocketSet TcpipDevice::socket_set;
00021 std::list<TCPsocket> stored_sockets;
00022 #endif
00023
00024
00025 void TcpipDevice::resizeSocketSet(void) {
00026 #if !HAVE_CONFIG_H || HAVE_LIBSDL_NET
00027 SDLNet_FreeSocketSet(socket_set);
00028 sockets_max <<= 1;
00029 socket_set = SDLNet_AllocSocketSet(sockets_max);
00030 for (std::list<TCPsocket>::iterator it = stored_sockets.begin();
00031 it != stored_sockets.end(); ++it) {
00032 SDLNet_TCP_AddSocket(socket_set, *it);
00033 }
00034 #endif
00035 }
00036
00037
00038 #if !HAVE_CONFIG_H || HAVE_LIBSDL_NET
00039 void TcpipDevice::addSocket(TCPsocket socket) {
00040 if (own_set) {
00041 SDLNet_TCP_AddSocket(own_set, socket);
00042 } else {
00043 if (sockets_num >= sockets_max) {
00044 resizeSocketSet();
00045 }
00046 SDLNet_TCP_AddSocket(socket_set, socket);
00047 stored_sockets.push_back(socket);
00048 ++sockets_num;
00049 }
00050 }
00051
00052
00053 void TcpipDevice::delSocket(TCPsocket socket) {
00054 stored_sockets.remove(socket);
00055 SDLNet_TCP_DelSocket(socket_set, socket);
00056 --sockets_num;
00057 }
00058
00059
00060 int TcpipDevice::checkAllSockets(long timeout) {
00061 return SDLNet_CheckSockets(socket_set, timeout);
00062 }
00063 #endif
00064
00065
00066 int TcpipDevice::checkSocket(long timeout) {
00067 #if !HAVE_CONFIG_H || HAVE_LIBSDL_NET
00068 if (!own_set) {
00069 return checkAllSockets(timeout);
00070 } else {
00071 return SDLNet_CheckSockets(own_set, timeout);
00072 }
00073 #else
00074 return -1;
00075 #endif
00076 }
00077
00078
00079 void TcpipDevice::initialize(bool eachCheckSocket) {
00080 #if !HAVE_CONFIG_H || HAVE_LIBSDL_NET
00081 if (!initialized) {
00082 if (SDLNet_Init() < 0) {
00083 return;
00084 }
00085 atexit(SDLNet_Quit);
00086
00087 sockets_num = 0;
00088 sockets_max = SocketSetBaseNum;
00089 socket_set = SDLNet_AllocSocketSet(sockets_max);
00090
00091 initialized = true;
00092 }
00093
00094 if (eachCheckSocket) {
00095 own_set = SDLNet_AllocSocketSet(1);
00096 }
00097 #endif
00098 }
00099
00100
00101 TcpipDevice::TcpipDevice(int buffer_size, bool eachCheckSockets)
00102 : SDL_Base(), ConnectionDevice(buffer_size)
00103 #if !HAVE_CONFIG_H || HAVE_LIBSDL_NET
00104 , tcp_socket(NULL), own_set(NULL)
00105 #endif
00106 {
00107 initialize(eachCheckSockets);
00108 }
00109
00110
00111 TcpipDevice::TcpipDevice(bool eachCheckSockets)
00112 : SDL_Base()
00113 #if !HAVE_CONFIG_H || HAVE_LIBSDL_NET
00114 , tcp_socket(NULL), own_set(NULL)
00115 #endif
00116 {
00117 initialize(eachCheckSockets);
00118 }
00119
00120
00121 #if !HAVE_CONFIG_H || HAVE_LIBSDL_NET
00122 TcpipDevice::TcpipDevice(TCPsocket net_socket, bool eachCheckSockets,
00123 int buffer_size)
00124 : SDL_Base(), ConnectionDevice(buffer_size),
00125 tcp_socket(net_socket), own_set(NULL) {
00126 initialize(eachCheckSockets);
00127 }
00128 #endif
00129
00130
00131 TcpipDevice::~TcpipDevice(void) {
00132 raw_disconnect();
00133 }
00134
00135
00136 const char* TcpipDevice::what(void) {
00137 #if !HAVE_CONFIG_H || HAVE_LIBSDL_NET
00138 return SDLNet_GetError();
00139 #else
00140 return "SDL_net is not installed.";
00141 #endif
00142 }
00143
00144
00145 bool TcpipDevice::isConnected(void) {
00146 #if !HAVE_CONFIG_H || HAVE_LIBSDL_NET
00147 return (tcp_socket) ? true : false;
00148 #else
00149 return false;
00150 #endif
00151 }
00152
00153
00154 int TcpipDevice::raw_connect(const char* host, long port) {
00155 #if !HAVE_CONFIG_H || HAVE_LIBSDL_NET
00156 if (!initialized) {
00157 return LibraryInitFail;
00158 }
00159
00160 IPaddress ip;
00161 SDLNet_ResolveHost(&ip, host, static_cast<short>(port));
00162 tcp_socket = SDLNet_TCP_Open(&ip);
00163 if (!tcp_socket) {
00164 disconnect();
00165 return DeviceOpenFail;
00166 }
00167 addSocket(tcp_socket);
00168
00169 return 0;
00170 #else
00171 return -1;
00172 #endif
00173 }
00174
00175
00176 void TcpipDevice::raw_disconnect(void) {
00177 #if !HAVE_CONFIG_H || HAVE_LIBSDL_NET
00178 if (own_set) {
00179 SDLNet_FreeSocketSet(own_set);
00180 own_set = NULL;
00181 }
00182 if (tcp_socket) {
00183 delSocket(tcp_socket);
00184 SDLNet_TCP_Close(tcp_socket);
00185 tcp_socket = NULL;
00186 }
00187 #endif
00188 }
00189
00190
00191 int TcpipDevice::raw_setBaudrate(long baudrate) {
00192 return 0;
00193 }
00194
00195
00196 int TcpipDevice::raw_send(const char* data, int len) {
00197 #if !HAVE_CONFIG_H || HAVE_LIBSDL_NET
00198 #if 0
00199 for (int i = 0; i < len; ++i) {
00200 if (data[i] == '\r') {
00201 fprintf(stderr, "\n");
00202 } else {
00203 fprintf(stderr, "%c", data[i]);
00204 }
00205 }
00206 #endif
00207 return SDLNet_TCP_Send(tcp_socket, const_cast<char*>(data), len);
00208 #else
00209 return 0;
00210 #endif
00211 }
00212
00213
00214 void TcpipDevice::raw_flush(void) {
00215 }
00216
00217
00218 void TcpipDevice::raw_check(int size, long timeout) {
00219 #if !HAVE_CONFIG_H || HAVE_LIBSDL_NET
00220 checkSocket(timeout);
00221 if (isConnected() && SDLNet_SocketReady(tcp_socket)) {
00222 char buffer[BufferSize];
00223 int n = SDLNet_TCP_Recv(tcp_socket, buffer, size);
00224 if (n <= 0) {
00225 disconnect();
00226 return;
00227 }
00228 recv_buffer->put(buffer, n);
00229 }
00230 #endif
00231 }
00232