Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

communication.cpp

Go to the documentation of this file.
00001 /********************************************************************
00002 Description:
00003 part of the 3Dsia project
00004 created: StonedBones, 1.2.2000
00005 
00006 History:
00007     date,   name,       changes,                    in funtion
00008     240400  xandi       merge, code clean, bugfixes
00009     180400  StonedBones added dynamic packet length
00010     010200  StonedBones created source
00011 
00012 ********************************************************************/
00013 
00014 #include <sys/time.h>
00015 #include <sys/socket.h>
00016 #include <sys/types.h>
00017 #include <stdlib.h>
00018 #include <unistd.h>
00019 #include <stdio.h>
00020 #include <string.h>
00021 #include <string>
00022 #include <pthread.h>
00023 
00024 #include "protocol.h"
00025 #include "options.h"
00026 
00027 #include "communication.h"
00028 
00029 /*...................................................................
00030 Description: Thread that manages the incoming buffer
00031 Args: ComArgs* args: various arguments (see header-file)
00032 Returns: <none>
00033 Created: StonedBones, 1.2.2000
00034 [ToDo:]
00035 [Comments:]
00036 Changes:
00037     fixed all (i hope) bugs                             230400, xandi
00038     merged server/communication & client-commthreads
00039     into common/communication                           240400, xandi
00040     code cleanup                                        240400, xandi
00041 -------------------------------------------------------------------*/
00042 
00043 void* IncomingThread (void* argz)
00044     {
00045     ComArgs* args = (ComArgs*) argz;
00046     fd_set  checkIt;
00047 
00048     Buffer* myBuffer = args->buffer;
00049     int mySocket     = args->socket;
00050 
00051     if (myBuffer == 0)
00052         {
00053         fprintf(stderr,"[comT.incoming] PANIK!! my buffer is not existing!\n");
00054         exit(1);
00055         }
00056 
00057     fprintf(stderr,"[comT.incoming] Incoming Thread started for socket %d\n",args->socket);
00058 
00059     FD_ZERO (&checkIt);
00060     FD_SET (mySocket, &checkIt);
00061 
00062     if (select (70, &checkIt, 0, 0, NULL) < 0)
00063         {
00064         fprintf (stderr,"[comT.incoming] error: select (not critical)");
00065         }
00066 
00067     while (1)
00068         {
00069         packet* pak = new packet;
00070 
00071 #ifdef DEBUG
00072         fprintf(stderr,"[comT.incoming] I am waiting on socket %d....\n",mySocket);
00073 #endif
00074         if (FD_ISSET (mySocket, &checkIt))
00075             {
00076             packet_header tmpH;
00077 
00078             unsigned int temp  = read (mySocket, &tmpH, sizeof (packet_header) );
00079             char* tmpCh = new char[tmpH.length];
00080 
00081             temp += read (mySocket, tmpCh, tmpH.length );
00082 
00083             #ifdef DEBUG
00084             fprintf ( stderr,"[comT.incoming] received %d bytes\n",temp);
00085             #endif
00086 
00087             if ( temp == ( tmpH.length + sizeof (packet_header ) ) )
00088                 {
00089                 pak->h = tmpH;
00090                 pak->data.assign (tmpCh, tmpH.length);
00091                 myBuffer->write (pak);
00092 
00093                 #ifdef DEBUG
00094                 fprintf(stderr,"[comT.incoming] packet (type %d, size = %d) written into buffer...\n",pak->h.header[0],tmpH.length);
00095                 #endif
00096                }
00097             else
00098                 {
00099                 fprintf(stderr,"[comT.incoming] seems read was not successful..\n");
00100                 break;
00101                 }
00102             }
00103         else
00104             {
00105             fprintf(stderr,"[comT.incoming] FD_ISSET gave false...\n");
00106             break;
00107             }
00108         }
00109 
00110     fprintf(stderr,"[comT.incoming] Thread terminated\n");
00111     packet* pak = new packet;
00112     pak->h.header[0] = PROT_KILL_CLIENT;
00113     myBuffer->write (pak);
00114     pthread_exit (NULL);
00115     }
00116 
00117 /*...................................................................
00118 Description: Thread that manages the outgoing buffer
00119 Args: ComArgs* args: various arguments (see header-files)
00120 Returns: <none>
00121 Created: StonedBones, 1.2.2000
00122 [ToDo:]
00123 [Comments:]
00124 Changes:
00125 -------------------------------------------------------------------*/
00126 
00127 void* OutgoingThread (void* argz)
00128     {
00129     ComArgs* args = (ComArgs*) argz;
00130     Buffer* myBuffer = args->buffer;
00131     int mySocket     = args->socket;
00132 
00133     if ( !myBuffer )
00134         {
00135         fprintf(stderr,"[comT.outgoing] PANIK!! my buffer is not existing!\n");
00136         exit(1);
00137         }
00138 
00139     fprintf(stderr,"[comT.outgoing] OutgoingThread started for socket %d\n",mySocket);
00140 
00141     while (1)
00142         {
00143         if (!myBuffer->isEmpty ())
00144             {
00145             packet* pak = myBuffer->read ();
00146 
00147 //            if ( !pak->data.length() ) //TEMP!!!!!!!!!!!
00148 //                pak->data = "x";  //TEMP!!!!!!!!!!!
00149 
00150             if (pak->h.header[0] == PROT_KILL_CLIENT)
00151                 pthread_exit (NULL);
00152 
00153             #ifdef DEBUG
00154             fprintf(stderr,"[comT.outgoing] sending a package..\n");
00155             #endif
00156 
00157             packet_header tmpH  = pak->h;
00158             tmpH.length = pak->data.length ();
00159             const char*   tmpCh = pak->data.data ();
00160 
00161             unsigned int temp;
00162             temp  = write ( mySocket, &tmpH, sizeof (packet_header) );
00163             temp += write ( mySocket, tmpCh, tmpH.length );
00164 
00165             if ( temp != ( sizeof (packet_header) + tmpH.length ) )
00166                 {
00167                 fprintf (stderr,"[comT.outgoing] Error: write\n");
00168                 pthread_exit (NULL);
00169                 }
00170 
00171             #ifdef DEBUG
00172             fprintf(stderr,"[comT.outoing] %d bytes sent\n", tmpH.length);
00173             #endif
00174             delete pak;
00175             pak = 0;
00176             }
00177         }
00178     }
00179 

Generated at Sat May 13 13:50:18 2000 for 3Dsia by doxygen 1.1.2 written by Dimitri van Heesch, © 1997-2000