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

Last change on this file since 2517 was 2517, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 5.2 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) : MThread(false), Log(out), fRxSocket(NULL), fServSock(NULL)
36{
37 fTxSocket = new TSocket("ceco", 7304);
38}
39
40MTcpIpIO::~MTcpIpIO()
41{
42 //
43 // Make sure, that no loop waiting for connection
44 // is running anymore!
45 //
46 Stop();
47
48 //
49 // Now delete all TCP/IP objects
50 //
51 cout << "Delete TxSocket " << fTxSocket << "..." << flush;
52 delete fTxSocket;
53 cout << "Done." << endl;
54 if (fServSock)
55 {
56 cout << "Delete ServSock " << fServSock << "..." << flush;
57 delete fServSock;
58 cout << "Done." << endl;
59 }
60 if (fRxSocket)
61 {
62 cout << "Delete RxSocket " << fRxSocket << "..." << flush;
63 delete fRxSocket;
64 cout << "Done." << endl;
65 }
66}
67
68bool MTcpIpIO::Send(const char *msg)
69{
70 if (!fTxSocket->IsValid())
71 return false;
72
73 const UInt_t len = fTxSocket->SendRaw(msg, strlen(msg));
74 if (len<0)
75 {
76 cout << "ERROR - Sending Message" << endl;
77 return false;
78 }
79 if (len!=strlen(msg))
80 {
81 cout << "Send wrong number (" << len << ") of Bytes." << endl;
82 return false;
83 }
84#ifdef DEBUG
85 cout << "Tx: " << msg << flush;
86#endif
87 return true;
88}
89
90bool MTcpIpIO::InterpreteStr(TString str)
91{
92 cout << "Rx: " << str << flush;
93}
94
95void MTcpIpIO::Clear()
96{
97 char c;
98 while (fRxSocket->RecvRaw(&c, 1)>0 && !HasStopFlag())
99 usleep(1);
100}
101
102void *MTcpIpIO::Thread()
103{
104 cout << "Starting receiver..." << endl;
105
106 while (!HasStopFlag())
107 {
108 fServSock=new TServerSocket(7404, kTRUE);
109 if (!fServSock->IsValid())
110 {
111 cout << "ServerSocket not valid: ";
112 switch (fServSock->GetErrorCode())
113 {
114 case 0: cout << "No error." << endl; break;
115 case -1: cout << "low level socket() call failed." << endl; break;
116 case -2: cout << "low level bind() call failed." << endl; break;
117 case -3: cout << "low level listen() call failed." << endl; break;
118 default: cout << "Unknown." << endl; break;
119 }
120 delete fServSock;
121 fServSock=NULL;
122 usleep(5000000);
123 continue;
124 }
125
126 fServSock->SetOption(kNoBlock, 1);
127
128 cout << "Waiting for conntection on port 7404..." << endl;
129 while (!HasStopFlag() && (Long_t)fRxSocket<=0)
130 {
131 fRxSocket = fServSock->Accept();
132 if (fRxSocket==0)
133 cout << "Error: TServerSock::Accept" << endl;
134 usleep(10);
135 }
136
137 // Can happen in case of HasStopFlag()
138 if (fRxSocket==(void*)-1)
139 fRxSocket=NULL;
140
141 if (fRxSocket==NULL)
142 {
143 delete fServSock;
144 fServSock=NULL;
145 continue;
146 }
147
148 if (!fRxSocket->IsValid())
149 {
150 cout << "TSocket not valid..." << endl;
151 delete fServSock;
152 delete fRxSocket;
153 fServSock = NULL;
154 fRxSocket = NULL;
155 continue;
156 }
157
158 cout << "Connection established..." << endl;
159
160 fRxSocket->SetOption(kNoBlock, 1);
161
162 Clear();
163
164 TString str;
165 while (!HasStopFlag())
166 {
167 char c;
168 const Int_t len = fRxSocket->RecvRaw(&c, 1);
169
170 // No data received (non-blocking mode)
171 if (len<0)
172 {
173 usleep(1);
174 continue;
175 }
176
177 // Data received with zero length!
178 if (len==0)
179 {
180 cout << "len==0" << endl;
181 continue;
182 }
183
184 // Data received
185 if (len>1)
186 {
187 cout << "Data too long!!!" << endl;
188 break;
189 }
190
191 // Data received (len==1)
192 if (c!='\n')
193 {
194 str += c;
195 continue;
196 }
197
198 // String completed
199 InterpreteStr(str);
200 str = "";
201 }
202 delete fServSock;
203 delete fRxSocket;
204 fServSock = NULL;
205 fRxSocket = NULL;
206 }
207
208 cout << "Receiver stopped..." << endl;
209
210 return NULL;
211}
Note: See TracBrowser for help on using the repository browser.