Connection Class Reference

#include <Connection.h>

Inheritance diagram for Connection:

Client Server List of all members.

Public Member Functions

 Connection ()
virtual ~Connection ()
virtual void startListen (ConnectionData *data)=0
SDL_Thread * getThread ()
TCPsocket getSocket ()

Static Public Member Functions

MessageIngetMessage (TCPsocket socket)
Uint32 putMessage (TCPsocket socket, MessageOut *msg)
int startThread (void *data)

Protected Attributes

ConnectionDatadata

Detailed Description

Abstract class (interface) for a generic connection.

Author:
stonedz
Since:
pre-alpha

Definition at line 24 of file Connection.h.


Constructor & Destructor Documentation

Connection::Connection  ) 
 

Constructor for a generic connection.

Author:
stonedz
Since:
pre-alpha

Definition at line 9 of file Connection.cpp.

00010         :data(new ConnectionData())
00011 {
00012 }

Connection::~Connection  )  [virtual]
 

Virtual destructor.

Author:
stonedz
Since:
pre-alpha

Definition at line 14 of file Connection.cpp.

00015 {
00016         delete this->data;
00017 }


Member Function Documentation

MessageIn * Connection::getMessage TCPsocket  socket  )  [static]
 

Gets a NetPacket from network and then builds a Message that will be returned.

Author:
stonedz
Since:
pre-alpha
Parameters:
socket Socket to monitor.
Returns:
A MessageIn pointer. A NULL pointer means failure.
Todo:
TODO Comment the implementation.

TODO Improve error handling.

Definition at line 34 of file Connection.cpp.

Referenced by ClientFSM::Login(), and ClientFSM::Ready().

00034                                                  {
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 }

SDL_Thread * Connection::getThread  ) 
 

Returns connection's thread. Useful to join threads.

Author:
stonedz
Since:
pre-alpha
Returns:
Connection's thread.

Definition at line 26 of file Connection.cpp.

References data, and ConnectionData::thread.

00026                                  {
00027         return this->data->thread;
00028 }

Uint32 Connection::putMessage TCPsocket  socket,
MessageOut msg
[static]
 

Puts a Message to the given socket. It transforms the given Message into a NetPacket, then it sends the packet to the given socket.

Author:
stonedz
Since:
pre-alpha
Parameters:
socket Socket to send the message to.
msg Message to be sent.
Returns:
Size of sent data. 0 (zero) means failure.
Todo:
TODO Comment the implementation.

TODO Improve error handling.

Definition at line 86 of file Connection.cpp.

References NetPacket::data, MessageOut::getPacket(), and NetPacket::len.

Referenced by Server::generalChatMsg(), and ClientFSM::SendKA_ACK().

00086                                                               {
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 }

virtual void Connection::startListen ConnectionData data  )  [pure virtual]
 

Listening routine.

This is what a Connection is supposed to do while running.

Author:
stonedz
Since:
pre-alpha
Parameters:
data Contains the connection's data.

Implemented in Client, and Server.

Referenced by startThread().

int Connection::startThread void *  data  )  [static]
 

It's a wrapper to use SDL's threads.

This is because SDL_threads are written in pure C, and a function or a static method is needed in order to create a thread. It passes the control to the startListen method

Author:
stonedz
Since:
pre-alpha
Parameters:
data A ConnectionData pointer.
See also:
ConnectionData

startListen

Definition at line 19 of file Connection.cpp.

References ConnectionData::self, and startListen().

00019                                       {
00020         ConnectionData* trueData = reinterpret_cast<ConnectionData *>(data);
00021         trueData->self->startListen(trueData); // Calls the correct object's method.
00022         return 1;
00023 }


Member Data Documentation

ConnectionData* Connection::data [protected]
 

Contains data needed to start a thread.

Definition at line 109 of file Connection.h.

Referenced by getThread().


The documentation for this class was generated from the following files:
Generated on Mon Dec 3 04:30:13 2007 for lo-testserver by  doxygen 1.3.9.1