#include <sys/time.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <pthread.h>
#include "protocol.h"
#include "options.h"
#include "communication.h"
Go to the source code of this file.
Functions | |
void* | IncomingThread (void* argz) |
The Incoming Thread. More... | |
void* | OutgoingThread (void* argz) |
The Outgoing Thread. More... |
|
The Incoming Thread.
args | The arguments passed from the clientThread |
Definition at line 43 of file communication.cpp.
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 }
|
The Outgoing Thread.
args | The arguments passed from the clientThread |
Definition at line 127 of file communication.cpp.
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 }