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 |
|
---|
11 | using 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 |
|
---|
35 | MTcpIpIO::MTcpIpIO(MLog &out)
|
---|
36 | : MThread(false), Log(out), fRxSocket(NULL), fServSock(NULL), fSendInterval(1000)
|
---|
37 | {
|
---|
38 | fTxSocket = new TSocket("ceco", 7304);
|
---|
39 | }
|
---|
40 |
|
---|
41 | MTcpIpIO::~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 |
|
---|
69 | bool 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 Int_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!=(Int_t)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 |
|
---|
111 | bool MTcpIpIO::InterpreteStr(TString str)
|
---|
112 | {
|
---|
113 | cout << "Rx: " << str << flush;
|
---|
114 | return true;
|
---|
115 | }
|
---|
116 |
|
---|
117 | void MTcpIpIO::Clear()
|
---|
118 | {
|
---|
119 | char c;
|
---|
120 | while (fRxSocket->RecvRaw(&c, 1)>0 && !HasStopFlag())
|
---|
121 | usleep(1);
|
---|
122 | }
|
---|
123 |
|
---|
124 | void *MTcpIpIO::Thread()
|
---|
125 | {
|
---|
126 | cout << "Starting receiver..." << endl;
|
---|
127 |
|
---|
128 | while (!HasStopFlag())
|
---|
129 | {
|
---|
130 | fServSock=new TServerSocket(7404, kTRUE);
|
---|
131 | if (!fServSock->IsValid())
|
---|
132 | {
|
---|
133 | cout << "ServerSocket not valid: ";
|
---|
134 | switch (fServSock->GetErrorCode())
|
---|
135 | {
|
---|
136 | case 0: cout << "No error." << endl; break;
|
---|
137 | case -1: cout << "low level socket() call failed." << endl; break;
|
---|
138 | case -2: cout << "low level bind() call failed." << endl; break;
|
---|
139 | case -3: cout << "low level listen() call failed." << endl; break;
|
---|
140 | default: cout << "Unknown." << endl; break;
|
---|
141 | }
|
---|
142 | delete fServSock;
|
---|
143 | fServSock=NULL;
|
---|
144 | usleep(5000000);
|
---|
145 | continue;
|
---|
146 | }
|
---|
147 |
|
---|
148 | fServSock->SetOption(kNoBlock, 1);
|
---|
149 |
|
---|
150 | cout << "Waiting for conntection on port 7404..." << endl;
|
---|
151 | while (!HasStopFlag() && (Long_t)fRxSocket<=0)
|
---|
152 | {
|
---|
153 | fRxSocket = fServSock->Accept();
|
---|
154 | if (fRxSocket==0)
|
---|
155 | cout << "Error: TServerSock::Accept" << endl;
|
---|
156 | usleep(10);
|
---|
157 | }
|
---|
158 |
|
---|
159 | // Can happen in case of HasStopFlag()
|
---|
160 | if (fRxSocket==(void*)-1)
|
---|
161 | fRxSocket=NULL;
|
---|
162 |
|
---|
163 | if (fRxSocket==NULL)
|
---|
164 | {
|
---|
165 | delete fServSock;
|
---|
166 | fServSock=NULL;
|
---|
167 | continue;
|
---|
168 | }
|
---|
169 |
|
---|
170 | if (!fRxSocket->IsValid())
|
---|
171 | {
|
---|
172 | cout << "TSocket not valid..." << endl;
|
---|
173 | delete fServSock;
|
---|
174 | delete fRxSocket;
|
---|
175 | fServSock = NULL;
|
---|
176 | fRxSocket = NULL;
|
---|
177 | continue;
|
---|
178 | }
|
---|
179 |
|
---|
180 | cout << "Connection established..." << endl;
|
---|
181 |
|
---|
182 | fRxSocket->SetOption(kNoBlock, 1);
|
---|
183 |
|
---|
184 | Clear();
|
---|
185 |
|
---|
186 | TString str;
|
---|
187 | while (!HasStopFlag())
|
---|
188 | {
|
---|
189 | char c;
|
---|
190 | const Int_t len = fRxSocket->RecvRaw(&c, 1);
|
---|
191 |
|
---|
192 | // No data received (non-blocking mode)
|
---|
193 | if (len<0)
|
---|
194 | {
|
---|
195 | usleep(1);
|
---|
196 | continue;
|
---|
197 | }
|
---|
198 |
|
---|
199 | // Data received with zero length!
|
---|
200 | if (len==0)
|
---|
201 | {
|
---|
202 | cout << "len==0" << endl;
|
---|
203 | continue;
|
---|
204 | }
|
---|
205 |
|
---|
206 | // Data received
|
---|
207 | if (len>1)
|
---|
208 | {
|
---|
209 | cout << "Data too long!!!" << endl;
|
---|
210 | break;
|
---|
211 | }
|
---|
212 |
|
---|
213 | // Data received (len==1)
|
---|
214 | if (c!='\n')
|
---|
215 | {
|
---|
216 | str += c;
|
---|
217 | continue;
|
---|
218 | }
|
---|
219 |
|
---|
220 | // String completed
|
---|
221 | InterpreteStr(str);
|
---|
222 | str = "";
|
---|
223 | }
|
---|
224 | delete fServSock;
|
---|
225 | delete fRxSocket;
|
---|
226 | fServSock = NULL;
|
---|
227 | fRxSocket = NULL;
|
---|
228 | }
|
---|
229 |
|
---|
230 | cout << "Receiver stopped..." << endl;
|
---|
231 |
|
---|
232 | return NULL;
|
---|
233 | }
|
---|