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

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