src/network/Connection.cpp

00001 #include "Connection.h"
00002 
00003 #include "Message.h"
00004 #include "MessageIn.h"
00005 #include "MessageOut.h"
00006 #include "NetPacket.h"
00007 #include "crc32.h"
00008 
00009 Connection::Connection()
00010         :data(new ConnectionData())
00011 {
00012 }
00013 
00014 Connection::~Connection()
00015 {
00016         delete this->data;
00017 }
00018 
00019 int Connection::startThread(void * data){
00020         ConnectionData* trueData = reinterpret_cast<ConnectionData *>(data);
00021         trueData->self->startListen(trueData); // Calls the correct object's method.
00022         return 1;
00023 }
00024 
00025 
00026 SDL_Thread* Connection::getThread(){
00027         return this->data->thread;
00028 }
00029 
00030 TCPsocket Connection::getSocket(){
00031     return this->data->socket;
00032 }
00033 
00034 MessageIn* Connection::getMessage(TCPsocket socket){
00035         Uint32 result;
00036         message_type type;
00037         Uint16 len;
00038         Uint32 crc;
00039         char* msg;
00040         char* buf;
00041 
00042         result=SDLNet_TCP_Recv(socket,(char *) &type,2); // Gets message's type
00043         if(result<2) {
00044                 if(SDLNet_GetError() && strlen(SDLNet_GetError())) // sometimes blank!
00045                         printf("SDLNet_TCP_Recv: %s\n", SDLNet_GetError());
00046                 return NULL;
00047         }
00048 
00049         result=SDLNet_TCP_Recv(socket,(char *) &len,2);// Gets payload's size
00050         if(result<2) {
00051                 if(SDLNet_GetError() && strlen(SDLNet_GetError())) // sometimes blank!
00052                         printf("SDLNet_TCP_Recv: %s\n", SDLNet_GetError());
00053                 return NULL;
00054         }
00055 
00056         msg = (char*) malloc (SDL_SwapBE16(len)); // Gets the payload
00057 
00058         result=SDLNet_TCP_Recv(socket,msg,SDL_SwapBE16(len));
00059         if(result<(SDL_SwapBE16(len))) {
00060                 if(SDLNet_GetError() && strlen(SDLNet_GetError())) // sometimes blank!
00061                         printf("SDLNet_TCP_Recv: %s\n", SDLNet_GetError());
00062                 free(msg);
00063                 return NULL;
00064         }
00065 
00066         result=SDLNet_TCP_Recv(socket,(char *) &crc, 4); // Gets the CRC
00067         if(result < 4){
00068                 if(SDLNet_GetError() && strlen(SDLNet_GetError())) // sometimes blank!
00069                         printf("SDLNet_TCP_Recv: %s\n", SDLNet_GetError());
00070                 return NULL;
00071         }
00072 
00073         buf = (char*) malloc (SDL_SwapBE16(len) + 8);   // 8 = (type)+(payload size)+(crc)
00074 
00075         memcpy(buf, (char *) &type , 2);
00076         memcpy(buf + 2, (char *) &len, 2);
00077         memcpy(buf + 4, (char *) msg, SDL_SwapBE16(len));
00078         memcpy((buf + 4 + SDL_SwapBE16(len)),(char *) &crc ,4);
00079 
00080         NetPacket* packet = new NetPacket(buf, (SDL_SwapBE16(len) + 8));// This will be destroyed by MessageIn destructor.
00081         MessageIn* message = new MessageIn(packet);
00082 
00083         return message;
00084 }
00085 
00086 Uint32 Connection::putMessage(TCPsocket socket, MessageOut* msg){
00087         Uint32 result;
00088         const NetPacket* packet = msg->getPacket();
00089         char *tmp = packet->data;
00090 
00091         result=SDLNet_TCP_Send(socket,tmp,(packet->len));
00092         if(result<(packet->len)) {
00093                 if(SDLNet_GetError() && strlen(SDLNet_GetError())) // sometimes blank!
00094                         printf("SDLNet_TCP_Send: %s\n", SDLNet_GetError());
00095                 return(0);
00096         }
00097 
00098         return result;
00099 }

Generated on Mon Dec 3 04:30:13 2007 for lo-testserver by  doxygen 1.3.9.1