Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 17, 2005, 9:06:47 AM (18 years ago)
Author:
rennerc
Message:

network_unit_test.cc: added simple test for network_socket
network_socket: should compile and could work ;)
netdefs.h: changed #ifndef/#define _NETWORK_MANAGER to #ifndef/define _NETDEFS

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/network/src/lib/network/network_socket.cc

    r5614 r5624  
    3737  this->setClassID(CL_NETWORK_SOCKET, "NetworkSocket");
    3838
     39  tcpSocket = NULL;
     40  bufferlength = 0;
     41
     42  mutex = SDL_CreateMutex();
     43  terminateThread = false;
     44
    3945  /* Init SDL_net */
     46  //NOTE: do we need to call SDLNet_Init for all instances?
    4047  if(SDLNet_Init()==-1)
    4148  {
     
    5562NetworkSocket::~ NetworkSocket( )
    5663{
     64  SDL_DestroyMutex(mutex);
    5765
    5866  /* Quit SDL_net */
     67  // NOTE: what if other instances of NetworkSocket running?
    5968  SDLNet_Quit();
    6069  PRINTF(5)("SDL_net shutdown\n");
     
    6271
    6372/**
    64   * This function establishes a TCP/UDP connection to a given server (function argument).
    65   * It is called by the NetworkStream. It creates a TCP/UDP socket for the connection.
    66   */
     73 * This function establishes a TCP/UDP connection to a given server (function argument).
     74 * It is called by the NetworkStream. It creates a TCP/UDP socket for the connection.
     75 * @param ip
     76 * @param port
     77 */
    6778void NetworkSocket::connectToServer(IPaddress ip, unsigned int port)
    6879{
     80  //check if not already connected or listening
     81  if (tcpSocket)
     82  {
     83    PRINTF(1)("NetworkSocket::listen: tcpSocket!=NULL! maybe you already called listen or connectToServer or did not call disconnectServer()!");
     84  }
    6985
    7086  /* Connect to the host and port contained in ip using a TCP connection. */
     
    7692  }
    7793
    78 }
    79 
    80 /**
    81   * Tells the NetworkSocket to listen on a specific port for incoming connections.
    82   * NetworkSocket::writeBytes(...) will have no effect until there is a valuable connection.
    83   */
    84 void listen(unsigned int port)
    85 {
     94  SDL_CreateThread(thread_read, (void*)this);
     95}
     96
     97/**
     98 * Tells the NetworkSocket to listen on a specific port for incoming connections.
     99 * NetworkSocket::writeBytes(...) will have no effect until there is a valuable connection.
     100 * @param port
     101 */
     102void NetworkSocket::listen(unsigned int port)
     103{
     104  //check if not already connected or listening
     105  if (tcpSocket)
     106  {
     107    PRINTF(1)("NetworkSocket::listen: tcpSocket!=NULL! maybe you already called listen or connectToServer or did not call disconnectServer()!\n");
     108    return;
     109  }
     110
     111  IPaddress ip;
     112
     113  if (SDLNet_ResolveHost(&ip, NULL, port)==-1)
     114  {
     115    PRINTF(1)("SDLNet_ResolveHost: %s\n", SDLNet_GetError());
     116    return;
     117  }
     118
     119  tcpSocket = SDLNet_TCP_Open(&ip);
     120
     121  if (!tcpSocket)
     122  {
     123    PRINTF(1)("SDLNet_TCP_Open: %s\n", SDLNet_GetError());
     124    return;
     125  }
     126
     127  SDL_CreateThread(thread_listen, (void*)this);
    86128}
    87129
     
    91133void NetworkSocket::disconnectServer( )
    92134{
    93 
     135  terminateThread = true;
    94136  /* Close the connection */
    95137  SDLNet_TCP_Close(tcpSocket);
    96 
    97 }
    98 
    99 /**
    100   * This function writes some bytes (data) to the network connection (if the connection is already
    101   * estabilhed) otherwise it just does nothing (silently discarding the data). And writes some
    102   * warnings
    103   */
    104 int NetworkSocket::writeBytes(byte* data, int length)
    105 {
    106 }
    107 
    108  /**
    109   * Reads in the bytes from the network interface and passes it to the NetworkStream.
    110   * This function must internaly be implemented/connected as a thread, since the read
    111   * functions of many network libraries are blocking an would therefore block the whole
    112   * program.
    113   * From outside, the thread shouldn't be accessible at all.
    114   */
     138  tcpSocket = NULL;
     139
     140}
     141
     142
     143/**
     144 * This function writes some bytes (data) to the network connection (if the connection is already
     145 * estabilhed) otherwise it just does nothing (silently discarding the data). And writes some
     146 * warnings
     147 * @param data: pointer to the data to send
     148 * @param length: n bytes to send
     149 * @return the number successfully written bytes
     150 */
     151int NetworkSocket::writeBytes(byte * data, int length)
     152{
     153  if (!tcpSocket || data==NULL)
     154    return 0;
     155
     156  int res = SDLNet_TCP_Send(tcpSocket, data, length);
     157
     158  if (res<length)
     159  {
     160    PRINTF(1)("SDLNet_TCP_Send: %s\n", SDLNet_GetError());
     161    return res;
     162  }
     163}
     164
     165/**
     166 * Reads in the bytes from the network interface and passes it to the NetworkStream.
     167 * This function must internaly be implemented/connected as a thread, since the read
     168 * functions of many network libraries are blocking an would therefore block the whole
     169 * program.
     170 * From outside, the thread shouldn't be accessible at all.
     171 * @param data: pointer to memory, big enough to store length bytes
     172 * @param length: n bytes to read
     173 * @return the number successfully read bytes. -1 on error. may be less than length!
     174 */
    115175int NetworkSocket::readBytes(byte * data, int length)
    116176{
    117 }
     177  if (!tcpSocket || data==NULL)
     178    return 0;
     179
     180  int nbytes = (length<bufferlength) ? length : bufferlength;
     181
     182  // just in case ...
     183  if (nbytes<0)
     184    return -1;
     185
     186  if (nbytes==0)
     187      return 0;
     188
     189  SDL_mutexP(mutex);
     190
     191  memcpy(data, buf, nbytes);
     192
     193  //important: use memmove because the memory areas may overlap
     194  memmove(buf, buf+nbytes, bufferlength-nbytes);
     195  bufferlength -= nbytes;
     196
     197  SDL_mutexV(mutex);
     198
     199  return nbytes;
     200}
     201
     202/**
     203 * used to create a thread to listen
     204 * will call thrad_read when established connection
     205 * @param data: pointer to NetwortSocket
     206 */
     207int NetworkSocket::thread_listen( void * data )
     208{
     209  NetworkSocket * self = (NetworkSocket*)data;
     210  TCPsocket tempsocket;
     211
     212  tempsocket = SDLNet_TCP_Accept(self->tcpSocket);
     213
     214  SDLNet_TCP_Close(self->tcpSocket);
     215  self->tcpSocket = NULL;
     216
     217  if (!tempsocket)
     218  {
     219    PRINTF(1)("SDLNet_TCP_Accept: %s\n", SDLNet_GetError());
     220    return -1;
     221  }
     222
     223  self->tcpSocket = tempsocket;
     224
     225  return thread_read(data);
     226}
     227
     228/**
     229 * used to create a thread to read from socket
     230 * @param data: pointer to NetworkSocket
     231 */
     232int NetworkSocket::thread_read( void * data )
     233{
     234  int nbytesread = 0;
     235  int nbytestoread = 0;
     236  char buffer[_LOCAL_BUFFER_SIZE];
     237  NetworkSocket * self = (NetworkSocket*)data;
     238
     239  while (!self->terminateThread)
     240  {
     241#define min(a,b) (a<b)?a:b
     242    nbytestoread = min(_INCOMING_BUFFER_SIZE - self->bufferlength, _LOCAL_BUFFER_SIZE);
     243
     244    nbytesread = SDLNet_TCP_Recv(self->tcpSocket, buffer, nbytestoread);
     245
     246    SDL_mutexP(self->mutex);
     247
     248    if (nbytesread<=0)
     249    {
     250      PRINTF(1)("SDLNet_TCP_Recv: %s\n", SDLNet_GetError());
     251      SDLNet_TCP_Close(self->tcpSocket);
     252      self->tcpSocket = NULL;
     253      SDL_mutexV(self->mutex);
     254      return -1;
     255    }
     256
     257    memcpy(self->buf+self->bufferlength, buffer, nbytesread);
     258    self->bufferlength += nbytesread;
     259
     260    SDL_mutexV(self->mutex);
     261  }
     262
     263  return 0;
     264}
     265
     266
Note: See TracChangeset for help on using the changeset viewer.