Changeset 9439 for trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.cc
- Timestamp:
- 05/09/09 13:48:12 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.cc
r9432 r9439 43 43 MTcpIpO::MTcpIpO(const char *addr, Int_t tx) : fPortTx(tx) 44 44 { 45 gLog << inf2 << "- Open send socket to " << addr << ":" << tx << endl; 45 // was "inf2" but nobody knows what the prg is doing if it times out 46 gLog << all << "- Open send socket to " << addr << ":" << tx << endl; 47 46 48 fTxSocket = new TSocket(addr, tx); 49 fTxSocket->SetOption(kNoBlock, 1); 50 51 MTcpIpO::RunThread(); 47 52 } 48 53 49 54 MTcpIpO::~MTcpIpO() 50 55 { 56 CancelThread(); 57 51 58 // Now delete all TCP/IP objects 52 59 delete fTxSocket; … … 55 62 MTcpIpIO::MTcpIpIO(const char *addr, Int_t tx, Int_t rx) : MTcpIpI(rx), MTcpIpO(addr, tx) 56 63 { 57 RunThread();64 MTcpIpI::RunThread(); 58 65 } 59 66 … … 74 81 } 75 82 83 /* 76 84 TString MTcpIpO::GetSocketAddress() const 77 85 { 86 TLockGuard guard(const_cast<TMutex*>(&fMutex)); 78 87 return GetSocketAddress(*fTxSocket); 79 88 } 80 89 */ 81 90 bool MTcpIpO::SendFrame(TSocket &tx, const char *msg, int len) 82 91 { 83 92 if (!tx.IsValid()) 84 {85 //gLog << warn << "WARNING - No transmission to " << GetSocketAddress(tx) << " possible." << endl;86 93 return false; 87 }88 94 89 95 #ifdef DEBUG … … 91 97 #endif 92 98 93 const Int_t l = tx.SendRaw(msg, len); 99 const Int_t l = tx.SendRaw(msg, len, kDontBlock); 100 101 // Frame not sent, EWOULDBLOCK 102 if (l==-4) 103 { 104 gLog << err << "ERROR - Sending to " << GetSocketAddress(tx) << " would block." << endl; 105 /*--->*/ tx.Close(); // ????? 106 return false; 107 } 108 94 109 if (l<0) 95 110 { … … 100 115 if (l!=len) 101 116 { 102 gLog << err << "ERROR - Sent wrong number (" << l << ") ofbytes to " << GetSocketAddress(tx) << endl;117 gLog << err << "ERROR - Could only sent " << l << " out of " << len << " bytes to " << GetSocketAddress(tx) << endl; 103 118 return false; 104 119 } … … 109 124 bool MTcpIpO::SendFrame(const char *addr, int port, const char *msg, int len) 110 125 { 111 // R__LOCKGUARD2(myMutex);112 126 #ifdef DEBUG 113 127 cout << "Connecting to " << addr << ":" << port << endl; … … 117 131 TSocket tx(addr, port); 118 132 return SendFrame(tx, msg, len); 119 /* 120 if (!tx.IsValid()) 121 { 122 gLog << warn << "WARNING - No transmission to " << addr << ":" << port << " possible." << endl; 133 } 134 135 bool MTcpIpO::Send(const char *msg, Int_t len) 136 { 137 const Int_t mtx = fMutex.TryLock(); 138 if (mtx==13) 139 gLog << warn << "MTcpIpO::Send - mutex is already locked by this thread." << endl; 140 141 // If Mutex cannot be locked, i.e. we are currently reopening 142 // the send socket we cannot wait, because we would block the 143 // executing threrad. 144 if (mtx) 123 145 return false; 124 } 125 126 gLog << dbg << "Sending to " << addr << ":" << port << endl; 127 128 const Int_t l = tx.SendRaw(msg, len, kDontBlock); 129 if (l<0) 130 { 131 gLog << err << "ERROR - Sending TCP/IP frame to " << addr << ":" << port << endl; 132 return false; 133 } 134 135 if (l!=len) 136 { 137 gLog << err << "ERROR - Sent wrong number (" << l << ") of bytes to " << addr << ":" << port << endl; 138 return false; 139 } 140 141 #ifdef DEBUG 142 cout << "Tx: " << msg << flush; 143 #endif 144 145 return true; 146 */ 147 } 148 149 bool MTcpIpO::Send(const char *msg, Int_t len) 150 { 151 if (!fTxSocket->IsValid()) 152 { 153 const TInetAddress &a = fTxSocket->GetInetAddress(); 154 if (!a.IsValid()) 155 return false; 146 147 const bool rc = SendFrame(*fTxSocket, msg, len); 148 149 fMutex.UnLock(); 150 151 return rc; 152 } 153 154 Int_t MTcpIpO::Thread() 155 { 156 const TInetAddress &a = fTxSocket->GetInetAddress(); 157 if (!a.IsValid()) 158 { 159 gLog << err << "ERROR: Send socket address invalid." << endl; 160 return 0; 161 } 162 163 Int_t wait = 1000; /// Wait a ms 164 165 while (!IsThreadCanceled()) 166 { 167 if (fTxSocket->IsValid()) 168 { 169 MThread::Sleep(1000); // Wait a ms 170 wait = 1000; 171 continue; 172 } 173 174 156 175 #ifdef DEBUG 157 176 cout << "- Reopen send socket to " << a.GetHostAddress() << ":" << fPortTx << endl; 158 177 #endif 178 179 fMutex.Lock(); 180 159 181 delete fTxSocket; 182 160 183 fTxSocket = new TSocket(a.GetHostAddress(), fPortTx); 161 } 162 163 return SendFrame(*fTxSocket, msg, len); 184 fTxSocket->SetOption(kNoBlock, 1); 185 186 fMutex.UnLock(); 187 188 if (fTxSocket->IsValid()) 189 continue; 190 191 MThread::Sleep(wait); // Wait a ms 192 if (wait<30000000) // 30s 193 wait *= 2; 194 } 195 196 return 1; 164 197 } 165 198 … … 174 207 TString str; 175 208 176 while (! IsThreadCanceled())209 while (!MTcpIpI::IsThreadCanceled()) 177 210 { 178 211 char c;
Note:
See TracChangeset
for help on using the changeset viewer.