Changeset 8864 for trunk/MagicSoft/Cosy/tcpip
- Timestamp:
- 02/17/08 22:39:04 (17 years ago)
- Location:
- trunk/MagicSoft/Cosy/tcpip
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.cc
r8862 r8864 2 2 3 3 #include <unistd.h> // usleep 4 5 #include <errno.h> 4 6 5 7 #include <TSocket.h> … … 153 155 } 154 156 155 void MTcpIpIO::ReadSocket(TSocket &rx) 156 { 157 // Clear buffer! 158 char c; 159 // while (fRxSocket->RecvRaw(&c, 1)>0 && !HasStopFlag()) 160 while (rx.RecvRaw(&c, 1)>0 && !IsThreadCanceled()) 161 usleep(1); 162 157 Bool_t MTcpIpIO::ReadSocket(TSocket &rx) 158 { 163 159 TString str; 164 // while (!HasStopFlag()) 160 165 161 while (!IsThreadCanceled()) 166 162 { … … 175 171 } 176 172 177 // Data received with zero length! 173 // Data received with zero length! (Connection lost) 178 174 if (len==0) 179 { 180 // THIS MEANS CONNECTIION LOST!!!! 181 cout << "============> len==0 (CONNECTION LOST?)" << endl; 182 break; // This break is for TEST PURPOSE FIXME!!! 183 continue; 184 } 175 return kFALSE; // This break is for TEST PURPOSE FIXME!!! 185 176 186 177 // Data received … … 202 193 str = ""; 203 194 } 204 } 205 206 //void *MTcpIpIO::Thread() 195 196 return kTRUE; 197 } 198 199 Bool_t MTcpIpI::WaitForData(TSocket &sock) 200 { 201 // No connection established? 202 if (!sock.IsValid()) 203 { 204 gLog << warn << "TSocket invalid on port " << fPortRx << "." << endl; 205 return kFALSE; 206 } 207 208 fConnectionEstablished = kTRUE; 209 210 // Get connection on port fPortRx and redirected 211 while (!IsThreadCanceled()) 212 { 213 // Check for pending data (every ms) 214 switch (sock.Select(TSocket::kRead, 1)) 215 { 216 case kTRUE: // Data pending... go on reading 217 if (!ReadSocket(sock)) 218 { 219 gLog << warn << "WARNING - Connection lost to " << MTcpIpO::GetSocketAddress(sock) << endl; 220 return kFALSE; 221 } 222 223 // *FALLTHROUGH* 224 case kFALSE: // Time out, no data yet, go on waiting 225 226 // Go on waiting for new data 227 continue; 228 229 default: // Error occurance 230 gLog << err << "TSocket::Select returned an error: " << strerror(errno) << endl; 231 return kFALSE; 232 } 233 } 234 return kTRUE; //??? 235 } 236 237 void MTcpIpI::WaitForConnection(TServerSocket &server) 238 { 239 gLog << all << " Wait for connection" << endl; 240 241 while (!IsThreadCanceled()) 242 { 243 gLog << all << "Listening for new connection on port " << fPortRx << "..." << endl; 244 245 // Check for a connection request (reminder: we are in non-blocking mode) 246 TSocket *socket = 0; 247 248 while (!IsThreadCanceled()) 249 { 250 socket = server.Accept(); 251 252 // Accept returned an error 253 if (socket==0) 254 { 255 gLog << err << "Error: TServerSock::Accept on port " << fPortRx << ": " << strerror(errno) << endl; 256 // Since we don't know the type of the error we better shut down the socket 257 return; 258 } 259 260 // No connection request pending 261 if ((Long_t)socket<0) 262 { 263 MThread::Sleep(1000); // Wait a ms 264 continue; 265 } 266 267 // Connection established 268 break; 269 } 270 271 if ((Long_t)socket<=0) 272 return; 273 274 if (!WaitForData(*socket)) 275 fConnectionEstablished = kFALSE; 276 277 delete socket; 278 } 279 } 280 281 Int_t MTcpIpI::Thread() 282 { 283 gLog << inf << "- Starting server listening on port " << fPortRx << "..." << endl; 284 285 while (!IsThreadCanceled()) 286 { 287 TServerSocket *server=new TServerSocket(fPortRx, kTRUE); 288 server->SetOption(kNoBlock, 1); 289 290 while (!IsThreadCanceled() && server->IsValid()) 291 WaitForConnection(*server); 292 293 if (!server->IsValid()) 294 { 295 gLog << err << "ServerSocket on port " << fPortRx << " invalid: "; 296 switch (server->GetErrorCode()) 297 { 298 case 0: gLog << "No error." << endl; break; 299 case -1: gLog << "low level socket() call failed." << endl; break; 300 case -2: gLog << "low level bind() call failed." << endl; break; 301 case -3: gLog << "low level listen() call failed." << endl; break; 302 default: gLog << "Unknown." << endl; break; 303 } 304 } 305 306 delete server; 307 308 MThread::Sleep(5000000); 309 } 310 311 gLog << inf << "- Listening server stopped on port " << fPortRx << "." << endl; 312 313 return 0; 314 } 315 316 /* 207 317 Int_t MTcpIpI::Thread() 208 318 { … … 276 386 gLog << all << "Connection established on port " << fPortRx << "." << endl; 277 387 278 fRxSocket->SetOption(kNoBlock, 1); 388 389 //fRxSocket->SetOption(kNoBlock, 1); 390 391 // Waqit for data 392 while (!IsThreadCanceled()) 393 { 394 switch (fRxSocket->Select(kRead, 1)) 395 { 396 case kTRUE: // Data waiting to be read 397 break; 398 case kFALSE: // time out 399 usleep(10); 400 continue; 401 } 402 403 // ERROR 404 cout << "Error: TRxSocket::Select on port " << fPortRx << "." << endl; 405 406 delete fServSock; 407 delete fRxSocket; 408 fServSock = NULL; 409 fRxSocket = NULL; 410 break; 411 }¨ 412 413 if (!fServSock) 414 continue; 415 416 if (IsThreadCanceled()) 417 { 418 delete fServSock; 419 delete fRxSocket; 420 fServSock = NULL; 421 fRxSocket = NULL; 422 continue; 423 } 279 424 280 425 // ------ IDENTICAL UP TO HERE ------ 281 426 427 // Read and evaluate data 282 428 ReadSocket(*fRxSocket); 283 429 … … 292 438 return 0; 293 439 // return NULL; 294 } 440 }*/ -
trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.h
r8862 r8864 18 18 { 19 19 private: 20 Int_t fPortRx; 20 Int_t fPortRx; 21 Bool_t fConnectionEstablished; 22 23 Bool_t WaitForData(TSocket &sock); 24 void WaitForConnection(TServerSocket &server); 21 25 22 26 Int_t Thread(); 23 27 24 virtual voidReadSocket(TSocket &rx) = 0;28 virtual Bool_t ReadSocket(TSocket &rx) = 0; 25 29 26 30 public: 27 MTcpIpI(Int_t rx) : MThread(Form("MTcpIpI::%d", rx)), fPortRx(rx) { /*RunThread();*/ }31 MTcpIpI(Int_t rx) : MThread(Form("MTcpIpI::%d", rx)), fPortRx(rx), fConnectionEstablished(kFALSE) { /*RunThread();*/ } 28 32 ~MTcpIpI() { CancelThread(); } 33 34 Bool_t IsConnectionEstablished() const { return fConnectionEstablished; } 29 35 }; 30 36 … … 52 58 { 53 59 private: 54 voidReadSocket(TSocket &rx);60 Bool_t ReadSocket(TSocket &rx); 55 61 56 62 public:
Note:
See TracChangeset
for help on using the changeset viewer.