1 | #ifndef COSY_MTcpIpIO
|
---|
2 | #define COSY_MTcpIpIO
|
---|
3 |
|
---|
4 | #ifndef MARS_MThread
|
---|
5 | #include "MThread.h"
|
---|
6 | #endif
|
---|
7 | #ifndef MARS_MTime
|
---|
8 | #include "MTime.h"
|
---|
9 | #endif
|
---|
10 |
|
---|
11 |
|
---|
12 | class TString;
|
---|
13 | class TSocket;
|
---|
14 | class TServerSocket;
|
---|
15 |
|
---|
16 | // A generalized class for receiving over tcp/ip
|
---|
17 | class MTcpIpI : public MThread
|
---|
18 | {
|
---|
19 | private:
|
---|
20 | Int_t fPortRx; // Port on which to listen for connections
|
---|
21 | Int_t fTimeout; // [ms] Timeout to listen for data to be received
|
---|
22 |
|
---|
23 | Bool_t fConnectionEstablished;
|
---|
24 |
|
---|
25 | Bool_t WaitForData(TSocket &sock);
|
---|
26 | void WaitForConnection(TServerSocket &server);
|
---|
27 |
|
---|
28 | Int_t Thread();
|
---|
29 |
|
---|
30 | virtual Bool_t ReadSocket(TSocket &rx) = 0;
|
---|
31 |
|
---|
32 | public:
|
---|
33 | MTcpIpI(Int_t rx, UInt_t timeout=5000) : MThread(Form("MTcpIpI::%d", rx)), fPortRx(rx), fTimeout(timeout), fConnectionEstablished(kFALSE) { /*RunThread();*/ }
|
---|
34 | ~MTcpIpI() { CancelThread(); }
|
---|
35 |
|
---|
36 | Bool_t IsConnectionEstablished() const { return fConnectionEstablished; }
|
---|
37 | };
|
---|
38 |
|
---|
39 |
|
---|
40 | // A generalized class for sending over tcp/ip
|
---|
41 | class MTcpIpO
|
---|
42 | {
|
---|
43 | private:
|
---|
44 | TSocket *fTxSocket;
|
---|
45 |
|
---|
46 | public:
|
---|
47 | MTcpIpO(const char *addr, Int_t tx);
|
---|
48 | ~MTcpIpO();
|
---|
49 |
|
---|
50 | static TString GetSocketAddress(const TSocket &s);
|
---|
51 | static bool SendFrame(TSocket &tx, const char *msg, int len);
|
---|
52 | static bool SendFrame(const char *addr, int port, const char *msg, int len);
|
---|
53 |
|
---|
54 | TString GetSocketAddress() const;
|
---|
55 | bool Send(const char *msg, int len);
|
---|
56 | };
|
---|
57 |
|
---|
58 | // This class es espcially meant to receive and send ascii messages
|
---|
59 | class MTcpIpIO : public MTcpIpI, public MTcpIpO
|
---|
60 | {
|
---|
61 | private:
|
---|
62 | Bool_t ReadSocket(TSocket &rx);
|
---|
63 |
|
---|
64 | public:
|
---|
65 | MTcpIpIO(const char *addr, Int_t tx, Int_t rx);
|
---|
66 | ~MTcpIpIO();
|
---|
67 |
|
---|
68 | virtual bool InterpreteStr(TString str);
|
---|
69 | };
|
---|
70 |
|
---|
71 | #endif
|
---|