tcpipServer.cpp

00001 /*
00002   TCP/IP 接続用のサーバ
00003   Satofumi KAMIMURA
00004   $Id$
00005 */
00006 
00007 #include "tcpipServer.h"
00008 #include <string.h>
00009 #include "detect_os.h"
00010 #ifdef Linux
00011 #include <sys/utsname.h>
00012 #else
00013 #include <windows.h>
00014 #endif
00015 
00016 
00017 TcpipServer::TcpipServer(void)
00018 #if !HAVE_CONFIG_H || HAVE_LIBSDL_NET
00019   : accept_socket(NULL)
00020 #endif
00021 {
00022 }
00023 
00024 
00025 TcpipServer::~TcpipServer(void) {
00026 #if !HAVE_CONFIG_H || HAVE_LIBSDL_NET
00027   if (accept_socket) {
00028     SDLNet_TCP_DelSocket(accept_set, accept_socket);
00029     SDLNet_FreeSocketSet(accept_set);
00030     accept_set = NULL;
00031     SDLNet_TCP_Close(accept_socket);
00032     accept_socket = NULL;
00033   }
00034 #endif
00035 }
00036 
00037 
00038 void TcpipServer::activate(unsigned short port) {
00039 #if !HAVE_CONFIG_H || HAVE_LIBSDL_NET
00040   SDLNet_ResolveHost(&ip, NULL, port);
00041 
00042   if (accept_socket) {
00043     SDLNet_TCP_Close(accept_socket);
00044   }
00045   accept_socket = SDLNet_TCP_Open(&ip);
00046   if (!accept_socket) {
00047     char* message = new char [80 + strlen(SDLNet_GetError())];
00048     sprintf(message, "Create TCP/IP server(port %d): %s",
00049             port, SDLNet_GetError());
00050     throw ConnectionDevice_Exception(message);
00051   }
00052   accept_set = SDLNet_AllocSocketSet(1);
00053   SDLNet_TCP_AddSocket(accept_set, accept_socket);
00054 #endif
00055 }
00056 
00057 
00058 TcpipDevice* TcpipServer::accept(int timeout, bool manualCheckSockets) {
00059 #if !HAVE_CONFIG_H || HAVE_LIBSDL_NET
00060   int n = SDLNet_CheckSockets(accept_set, timeout);
00061   if ((n <= 0) || !SDLNet_SocketReady(accept_socket)) {
00062     return NULL;
00063   }
00064   TCPsocket net_socket = SDLNet_TCP_Accept(accept_socket);
00065   if (!net_socket) {
00066     return NULL;
00067   }
00068   TcpipDevice* socket = new TcpipDevice(net_socket, manualCheckSockets);
00069   socket->addSocket(net_socket);
00070 
00071   return socket;
00072 #else
00073   return NULL;
00074 #endif
00075 }
00076 
00077 
00078 std::string TcpipServer::getOwnIP(void) {
00079 #if HAVE_CONFIG_H && !HAVE_LIBSDL_NET
00080   return "";
00081 #else
00082 #ifdef Linux
00083   struct utsname uts;
00084   if (uname(&uts) < 0) {
00085     return "";
00086   }
00087   char* host_name = uts.nodename;
00088 #else
00089   char host_name[MAX_COMPUTERNAME_LENGTH +1];
00090   DWORD length = sizeof(host_name);
00091   GetComputerName(host_name, &length);
00092 #endif
00093 
00094   enum { ConnectionTestPort = 24590 };
00095   IPaddress host_ip;
00096   if (SDLNet_ResolveHost(&host_ip, host_name, ConnectionTestPort) < 0) {
00097     return "";
00098   }
00099   char ip_name[] = "XXX.XXX.XXX.XXX";
00100   sprintf(ip_name, "%d.%d.%d.%d",
00101           host_ip.host & 0xff, (host_ip.host >> 8) & 0xff,
00102           (host_ip.host >> 16) & 0xff, (host_ip.host >> 24) & 0xff);
00103 
00104   return ip_name;
00105 #endif
00106 }
00107 

Generated on Mon Apr 13 22:52:05 2009 by  doxygen 1.5.7.1