source: trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.cc@ 7764

Last change on this file since 7764 was 4865, checked in by rwagner, 20 years ago
*** empty log message ***
File size: 5.6 KB
Line 
1#include "MTcpIpIO.h"
2
3#include <unistd.h> // usleep
4#include <iostream>
5
6#include <TSocket.h>
7#include <TServerSocket.h>
8
9#undef DEBUG
10
11using namespace std;
12
13 /*
14 enum ESockOptions {
15 kSendBuffer, // size of send buffer
16 kRecvBuffer, // size of receive buffer
17 kOobInline, // OOB message inline
18 kKeepAlive, // keep socket alive
19 kReuseAddr, // allow reuse of local portion of address 5-tuple
20 kNoDelay, // send without delay
21 kNoBlock, // non-blocking I/O
22 kProcessGroup, // socket process group (used for SIGURG and SIGIO)
23 kAtMark, // are we at out-of-band mark (read only)
24 kBytesToRead // get number of bytes to read, FIONREAD (read only)
25 };
26
27 enum ESendRecvOptions {
28 kDefault, // default option (= 0)
29 kOob, // send or receive out-of-band data
30 kPeek, // peek at incoming message (receive only)
31 kDontBlock // send/recv as much data as possible without blocking
32 };
33 */
34
35MTcpIpIO::MTcpIpIO(MLog &out)
36 : MThread(false), Log(out), fRxSocket(NULL), fServSock(NULL), fSendInterval(1000)
37{
38 fTxSocket = new TSocket("ceco", 7304);
39}
40
41MTcpIpIO::~MTcpIpIO()
42{
43 //
44 // Make sure, that no loop waiting for connection
45 // is running anymore!
46 //
47 Stop();
48
49 //
50 // Now delete all TCP/IP objects
51 //
52 //cout << "Delete TxSocket " << fTxSocket << "..." << flush;
53 delete fTxSocket;
54 //cout << "Done." << endl;
55 if (fServSock)
56 {
57 //cout << "Delete ServSock " << fServSock << "..." << flush;
58 delete fServSock;
59 //cout << "Done." << endl;
60 }
61 if (fRxSocket)
62 {
63 //cout << "Delete RxSocket " << fRxSocket << "..." << flush;
64 delete fRxSocket;
65 //cout << "Done." << endl;
66 }
67}
68
69bool MTcpIpIO::Send(const char *msg, bool force=kFALSE)
70{
71
72 const MTime t(-1);
73
74 if ((double)t-(double)fTime<0.001*fSendInterval && !force)
75 return true;
76
77
78 if (lout.Lock("MTcpIpIO::Send"))
79 {
80 //const Int_t rc = lout.IsOutputDeviceEnabled(MLog::eGui);
81 //lout.DisableOutputDevice(MLog::eGui);
82 lout << msg << flush;
83 lout.UnLock("MTcpIpIO::Send");
84 //if (rc)
85 // lout.EnableOutputDevice(MLog::eGui);
86 }
87
88 fTime = t;
89
90 if (!fTxSocket->IsValid())
91 return false;
92
93 const UInt_t len = fTxSocket->SendRaw(msg, strlen(msg));
94 if (len<0)
95 {
96 cout << "ERROR - Sending Message" << endl;
97 return false;
98 }
99 if (len!=strlen(msg))
100 {
101 cout << "Send wrong number (" << len << ") of Bytes." << endl;
102 return false;
103 }
104#ifdef DEBUG
105 cout << "Tx: " << msg << flush;
106#endif
107
108 return true;
109}
110
111bool MTcpIpIO::InterpreteStr(TString str)
112{
113 cout << "Rx: " << str << flush;
114}
115
116void MTcpIpIO::Clear()
117{
118 char c;
119 while (fRxSocket->RecvRaw(&c, 1)>0 && !HasStopFlag())
120 usleep(1);
121}
122
123void *MTcpIpIO::Thread()
124{
125 cout << "Starting receiver..." << endl;
126
127 while (!HasStopFlag())
128 {
129 fServSock=new TServerSocket(7404, kTRUE);
130 if (!fServSock->IsValid())
131 {
132 cout << "ServerSocket not valid: ";
133 switch (fServSock->GetErrorCode())
134 {
135 case 0: cout << "No error." << endl; break;
136 case -1: cout << "low level socket() call failed." << endl; break;
137 case -2: cout << "low level bind() call failed." << endl; break;
138 case -3: cout << "low level listen() call failed." << endl; break;
139 default: cout << "Unknown." << endl; break;
140 }
141 delete fServSock;
142 fServSock=NULL;
143 usleep(5000000);
144 continue;
145 }
146
147 fServSock->SetOption(kNoBlock, 1);
148
149 cout << "Waiting for conntection on port 7404..." << endl;
150 while (!HasStopFlag() && (Long_t)fRxSocket<=0)
151 {
152 fRxSocket = fServSock->Accept();
153 if (fRxSocket==0)
154 cout << "Error: TServerSock::Accept" << endl;
155 usleep(10);
156 }
157
158 // Can happen in case of HasStopFlag()
159 if (fRxSocket==(void*)-1)
160 fRxSocket=NULL;
161
162 if (fRxSocket==NULL)
163 {
164 delete fServSock;
165 fServSock=NULL;
166 continue;
167 }
168
169 if (!fRxSocket->IsValid())
170 {
171 cout << "TSocket not valid..." << endl;
172 delete fServSock;
173 delete fRxSocket;
174 fServSock = NULL;
175 fRxSocket = NULL;
176 continue;
177 }
178
179 cout << "Connection established..." << endl;
180
181 fRxSocket->SetOption(kNoBlock, 1);
182
183 Clear();
184
185 TString str;
186 while (!HasStopFlag())
187 {
188 char c;
189 const Int_t len = fRxSocket->RecvRaw(&c, 1);
190
191 // No data received (non-blocking mode)
192 if (len<0)
193 {
194 usleep(1);
195 continue;
196 }
197
198 // Data received with zero length!
199 if (len==0)
200 {
201 cout << "len==0" << endl;
202 continue;
203 }
204
205 // Data received
206 if (len>1)
207 {
208 cout << "Data too long!!!" << endl;
209 break;
210 }
211
212 // Data received (len==1)
213 if (c!='\n')
214 {
215 str += c;
216 continue;
217 }
218
219 // String completed
220 InterpreteStr(str);
221 str = "";
222 }
223 delete fServSock;
224 delete fRxSocket;
225 fServSock = NULL;
226 fRxSocket = NULL;
227 }
228
229 cout << "Receiver stopped..." << endl;
230
231 return NULL;
232}
Note: See TracBrowser for help on using the repository browser.