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

Last change on this file since 4436 was 4256, checked in by tbretz, 21 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)
70{
71 const MTime t(-1);
72
73 if ((double)t-(double)fTime<0.001*fSendInterval)
74 return true;
75
76 if (lout.Lock("MTcpIpIO::Send"))
77 {
78 //const Int_t rc = lout.IsOutputDeviceEnabled(MLog::eGui);
79 //lout.DisableOutputDevice(MLog::eGui);
80 lout << msg << flush;
81 lout.UnLock("MTcpIpIO::Send");
82 //if (rc)
83 // lout.EnableOutputDevice(MLog::eGui);
84 }
85
86 fTime = t;
87
88 if (!fTxSocket->IsValid())
89 return false;
90
91 const UInt_t len = fTxSocket->SendRaw(msg, strlen(msg));
92 if (len<0)
93 {
94 cout << "ERROR - Sending Message" << endl;
95 return false;
96 }
97 if (len!=strlen(msg))
98 {
99 cout << "Send wrong number (" << len << ") of Bytes." << endl;
100 return false;
101 }
102#ifdef DEBUG
103 cout << "Tx: " << msg << flush;
104#endif
105 return true;
106}
107
108bool MTcpIpIO::InterpreteStr(TString str)
109{
110 cout << "Rx: " << str << flush;
111}
112
113void MTcpIpIO::Clear()
114{
115 char c;
116 while (fRxSocket->RecvRaw(&c, 1)>0 && !HasStopFlag())
117 usleep(1);
118}
119
120void *MTcpIpIO::Thread()
121{
122 cout << "Starting receiver..." << endl;
123
124 while (!HasStopFlag())
125 {
126 fServSock=new TServerSocket(7404, kTRUE);
127 if (!fServSock->IsValid())
128 {
129 cout << "ServerSocket not valid: ";
130 switch (fServSock->GetErrorCode())
131 {
132 case 0: cout << "No error." << endl; break;
133 case -1: cout << "low level socket() call failed." << endl; break;
134 case -2: cout << "low level bind() call failed." << endl; break;
135 case -3: cout << "low level listen() call failed." << endl; break;
136 default: cout << "Unknown." << endl; break;
137 }
138 delete fServSock;
139 fServSock=NULL;
140 usleep(5000000);
141 continue;
142 }
143
144 fServSock->SetOption(kNoBlock, 1);
145
146 cout << "Waiting for conntection on port 7404..." << endl;
147 while (!HasStopFlag() && (Long_t)fRxSocket<=0)
148 {
149 fRxSocket = fServSock->Accept();
150 if (fRxSocket==0)
151 cout << "Error: TServerSock::Accept" << endl;
152 usleep(10);
153 }
154
155 // Can happen in case of HasStopFlag()
156 if (fRxSocket==(void*)-1)
157 fRxSocket=NULL;
158
159 if (fRxSocket==NULL)
160 {
161 delete fServSock;
162 fServSock=NULL;
163 continue;
164 }
165
166 if (!fRxSocket->IsValid())
167 {
168 cout << "TSocket not valid..." << endl;
169 delete fServSock;
170 delete fRxSocket;
171 fServSock = NULL;
172 fRxSocket = NULL;
173 continue;
174 }
175
176 cout << "Connection established..." << endl;
177
178 fRxSocket->SetOption(kNoBlock, 1);
179
180 Clear();
181
182 TString str;
183 while (!HasStopFlag())
184 {
185 char c;
186 const Int_t len = fRxSocket->RecvRaw(&c, 1);
187
188 // No data received (non-blocking mode)
189 if (len<0)
190 {
191 usleep(1);
192 continue;
193 }
194
195 // Data received with zero length!
196 if (len==0)
197 {
198 cout << "len==0" << endl;
199 continue;
200 }
201
202 // Data received
203 if (len>1)
204 {
205 cout << "Data too long!!!" << endl;
206 break;
207 }
208
209 // Data received (len==1)
210 if (c!='\n')
211 {
212 str += c;
213 continue;
214 }
215
216 // String completed
217 InterpreteStr(str);
218 str = "";
219 }
220 delete fServSock;
221 delete fRxSocket;
222 fServSock = NULL;
223 fRxSocket = NULL;
224 }
225
226 cout << "Receiver stopped..." << endl;
227
228 return NULL;
229}
Note: See TracBrowser for help on using the repository browser.