acceptSocket.cpp
00001
00002
00003
00004
00005
00006
00007 #include "acceptSocket.h"
00008 #include "socketCtrl.h"
00009 #include <stdlib.h>
00010
00011
00012 AcceptSocket::AcceptSocket(Uint16 port)
00013 : accept_socket(NULL), accept_set(NULL) {
00014 IPaddress ip;
00015 SDLNet_ResolveHost(&ip, NULL, port);
00016 accept_socket = SDLNet_TCP_Open(&ip);
00017 if (!accept_socket) {
00018 fprintf(stderr, "SDLNet_TCP_Open: %s\n", SDLNet_GetError());
00019 exit(1);
00020 }
00021 accept_set = SDLNet_AllocSocketSet(1);
00022 if (!accept_set) {
00023 fprintf(stderr, "SDLNet_AllocSocketSet: %s\n", SDLNet_GetError());
00024 exit(1);
00025 }
00026 SDLNet_TCP_AddSocket(accept_set, accept_socket);
00027 }
00028
00029
00030 AcceptSocket::~AcceptSocket(void) {
00031 if (accept_set) {
00032 SDLNet_TCP_DelSocket(accept_set, accept_socket);
00033 SDLNet_FreeSocketSet(accept_set);
00034 }
00035 if (accept_socket) {
00036 SDLNet_TCP_Close(accept_socket);
00037 }
00038 }
00039
00040
00041 SocketCtrl* AcceptSocket::accept(int timeout) {
00042 int numready = SDLNet_CheckSockets(accept_set, timeout);
00043 if ((numready > 0) && SDLNet_SocketReady(accept_socket)) {
00044 return new SocketCtrl(SDLNet_TCP_Accept(accept_socket));
00045 }
00046 return NULL;
00047 }
00048