1 | #include "MTcpIpIO.h"
|
---|
2 |
|
---|
3 | #include <unistd.h> // usleep
|
---|
4 | #include <iostream>
|
---|
5 |
|
---|
6 | #include <TSocket.h>
|
---|
7 | #include <TServerSocket.h>
|
---|
8 |
|
---|
9 | using namespace std;
|
---|
10 |
|
---|
11 | /*
|
---|
12 | enum ESockOptions {
|
---|
13 | kSendBuffer, // size of send buffer
|
---|
14 | kRecvBuffer, // size of receive buffer
|
---|
15 | kOobInline, // OOB message inline
|
---|
16 | kKeepAlive, // keep socket alive
|
---|
17 | kReuseAddr, // allow reuse of local portion of address 5-tuple
|
---|
18 | kNoDelay, // send without delay
|
---|
19 | kNoBlock, // non-blocking I/O
|
---|
20 | kProcessGroup, // socket process group (used for SIGURG and SIGIO)
|
---|
21 | kAtMark, // are we at out-of-band mark (read only)
|
---|
22 | kBytesToRead // get number of bytes to read, FIONREAD (read only)
|
---|
23 | };
|
---|
24 |
|
---|
25 | enum ESendRecvOptions {
|
---|
26 | kDefault, // default option (= 0)
|
---|
27 | kOob, // send or receive out-of-band data
|
---|
28 | kPeek, // peek at incoming message (receive only)
|
---|
29 | kDontBlock // send/recv as much data as possible without blocking
|
---|
30 | };
|
---|
31 | */
|
---|
32 |
|
---|
33 | MTcpIpIO::MTcpIpIO(MLog &out) : MThread(false), Log(out), fRxSocket(NULL), fServSock(NULL)
|
---|
34 | {
|
---|
35 | fTxSocket = new TSocket("ceco", 7304);
|
---|
36 | }
|
---|
37 |
|
---|
38 | MTcpIpIO::~MTcpIpIO()
|
---|
39 | {
|
---|
40 | cout << "Delete TxSocket..." << flush;
|
---|
41 | delete fTxSocket;
|
---|
42 | cout << "Done." << endl;
|
---|
43 | if (fServSock)
|
---|
44 | {
|
---|
45 | cout << "Delete ServSock..." << flush;
|
---|
46 | delete fServSock;
|
---|
47 | cout << "Done." << endl;
|
---|
48 | }
|
---|
49 | if (fRxSocket)
|
---|
50 | {
|
---|
51 | cout << "Delete RxSocket..." << flush;
|
---|
52 | delete fRxSocket;
|
---|
53 | cout << "Done." << endl;
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | bool MTcpIpIO::Send(const char *msg)
|
---|
58 | {
|
---|
59 | if (!fTxSocket->IsValid())
|
---|
60 | return false;
|
---|
61 |
|
---|
62 | const UInt_t len = fTxSocket->SendRaw(msg, strlen(msg));
|
---|
63 | if (len<0)
|
---|
64 | {
|
---|
65 | cout << "ERROR - Sending Message" << endl;
|
---|
66 | return false;
|
---|
67 | }
|
---|
68 | if (len!=strlen(msg))
|
---|
69 | {
|
---|
70 | cout << "Send wrong number (" << len << ") of Bytes." << endl;
|
---|
71 | return false;
|
---|
72 | }
|
---|
73 |
|
---|
74 | cout << "Tx: " << msg << flush;
|
---|
75 |
|
---|
76 | return true;
|
---|
77 | }
|
---|
78 |
|
---|
79 | bool MTcpIpIO::InterpreteStr(TString str)
|
---|
80 | {
|
---|
81 | cout << "Rx: " << str << flush;
|
---|
82 | }
|
---|
83 |
|
---|
84 | void MTcpIpIO::Clear()
|
---|
85 | {
|
---|
86 | char c;
|
---|
87 | while (fRxSocket->RecvRaw(&c, 1)>0 && !HasStopFlag())
|
---|
88 | usleep(1);
|
---|
89 | }
|
---|
90 |
|
---|
91 | void *MTcpIpIO::Thread()
|
---|
92 | {
|
---|
93 | cout << "Starting receiver..." << endl;
|
---|
94 |
|
---|
95 | while (!HasStopFlag())
|
---|
96 | {
|
---|
97 | fServSock=new TServerSocket(7404, kTRUE);
|
---|
98 | if (!fServSock->IsValid())
|
---|
99 | {
|
---|
100 | cout << "ServerSocket not valid: ";
|
---|
101 | switch (fServSock->GetErrorCode())
|
---|
102 | {
|
---|
103 | case 0: cout << "No error." << endl; break;
|
---|
104 | case -1: cout << "low level socket() call failed." << endl; break;
|
---|
105 | case -2: cout << "low level bind() call failed." << endl; break;
|
---|
106 | case -3: cout << "low level listen() call failed." << endl; break;
|
---|
107 | default: cout << "Unknown." << endl; break;
|
---|
108 | }
|
---|
109 | delete fServSock;
|
---|
110 | fServSock=NULL;
|
---|
111 | usleep(5000000);
|
---|
112 | continue;
|
---|
113 | }
|
---|
114 |
|
---|
115 | fServSock->SetOption(kNoBlock, 1);
|
---|
116 |
|
---|
117 | cout << "Waiting for conntection on port 7404..." << endl;
|
---|
118 | while (!HasStopFlag() && (Long_t)fRxSocket<=0)
|
---|
119 | {
|
---|
120 | fRxSocket = fServSock->Accept();
|
---|
121 | if (fRxSocket==0)
|
---|
122 | cout << "Error: TServerSock::Accept" << endl;
|
---|
123 | usleep(1);
|
---|
124 | }
|
---|
125 |
|
---|
126 | if (fRxSocket==NULL)
|
---|
127 | {
|
---|
128 | delete fServSock;
|
---|
129 | fServSock=NULL;
|
---|
130 | continue;
|
---|
131 | }
|
---|
132 |
|
---|
133 | if (!fRxSocket->IsValid())
|
---|
134 | {
|
---|
135 | cout << "TSocket not valid..." << endl;
|
---|
136 | delete fServSock;
|
---|
137 | delete fRxSocket;
|
---|
138 | fServSock = NULL;
|
---|
139 | fRxSocket = NULL;
|
---|
140 | continue;
|
---|
141 | }
|
---|
142 |
|
---|
143 | cout << "Connection established..." << endl;
|
---|
144 |
|
---|
145 | fRxSocket->SetOption(kNoBlock, 1);
|
---|
146 |
|
---|
147 | Clear();
|
---|
148 |
|
---|
149 | TString str;
|
---|
150 | while (!HasStopFlag())
|
---|
151 | {
|
---|
152 | char c;
|
---|
153 | const Int_t len = fRxSocket->RecvRaw(&c, 1);
|
---|
154 |
|
---|
155 | // No data received (non-blocking mode)
|
---|
156 | if (len<0)
|
---|
157 | {
|
---|
158 | usleep(1);
|
---|
159 | continue;
|
---|
160 | }
|
---|
161 |
|
---|
162 | // Data received with zero length!
|
---|
163 | if (len==0)
|
---|
164 | {
|
---|
165 | cout << "len==0" << endl;
|
---|
166 | continue;
|
---|
167 | }
|
---|
168 |
|
---|
169 | // Data received
|
---|
170 | if (len>1)
|
---|
171 | {
|
---|
172 | cout << "Data too long!!!" << endl;
|
---|
173 | break;
|
---|
174 | }
|
---|
175 |
|
---|
176 | // Data received (len==1)
|
---|
177 | if (c!='\n')
|
---|
178 | {
|
---|
179 | str += c;
|
---|
180 | continue;
|
---|
181 | }
|
---|
182 |
|
---|
183 | // String completed
|
---|
184 | InterpreteStr(str);
|
---|
185 | str = "";
|
---|
186 | }
|
---|
187 | delete fServSock;
|
---|
188 | delete fRxSocket;
|
---|
189 | fServSock = NULL;
|
---|
190 | fRxSocket = NULL;
|
---|
191 | }
|
---|
192 |
|
---|
193 | cout << "Receiver stopped..." << endl;
|
---|
194 |
|
---|
195 | return NULL;
|
---|
196 | }
|
---|