| 1 | #ifndef COSY_MTcpIpIO
|
|---|
| 2 | #define COSY_MTcpIpIO
|
|---|
| 3 |
|
|---|
| 4 | #ifndef COSY_MsgQueue
|
|---|
| 5 | #include "msgqueue.h"
|
|---|
| 6 | #endif
|
|---|
| 7 | #ifndef MARS_MTime
|
|---|
| 8 | #include "MTime.h"
|
|---|
| 9 | #endif
|
|---|
| 10 | #ifndef ROOT_TMutex
|
|---|
| 11 | #include <TMutex.h>
|
|---|
| 12 | #endif
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 | class TString;
|
|---|
| 16 | class TSocket;
|
|---|
| 17 | class TServerSocket;
|
|---|
| 18 |
|
|---|
| 19 | // A generalized class for receiving over tcp/ip
|
|---|
| 20 | class MTcpIpI : public MThread
|
|---|
| 21 | {
|
|---|
| 22 | protected:
|
|---|
| 23 | TSocket *fRxSocket;
|
|---|
| 24 | TMutex fRxMutex;
|
|---|
| 25 |
|
|---|
| 26 | private:
|
|---|
| 27 | Int_t fPortRx; // Port on which to listen for connections
|
|---|
| 28 | Int_t fTimeout; // [ms] Timeout to listen for data to be received
|
|---|
| 29 |
|
|---|
| 30 | Bool_t fConnectionEstablished;
|
|---|
| 31 |
|
|---|
| 32 | MTime fLast;
|
|---|
| 33 |
|
|---|
| 34 | Bool_t WaitForData(TSocket &sock);
|
|---|
| 35 | void WaitForConnection(TServerSocket &server);
|
|---|
| 36 |
|
|---|
| 37 | Int_t Thread();
|
|---|
| 38 |
|
|---|
| 39 | virtual Bool_t ReadSocket(TSocket &rx) = 0;
|
|---|
| 40 |
|
|---|
| 41 | public:
|
|---|
| 42 | MTcpIpI(Int_t rx, UInt_t timeout=5000) : MThread(Form("MTcpIpI::%d", rx)), fRxSocket(0), fPortRx(rx), fTimeout(timeout), fConnectionEstablished(kFALSE) { /*RunThread();*/ }
|
|---|
| 43 | ~MTcpIpI() { CancelThread(); }
|
|---|
| 44 |
|
|---|
| 45 | Bool_t IsConnectionEstablished() const { return fConnectionEstablished; }
|
|---|
| 46 | };
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 | // A generalized class for sending over tcp/ip
|
|---|
| 50 | class MTcpIpO : public MThread
|
|---|
| 51 | {
|
|---|
| 52 | protected:
|
|---|
| 53 | TSocket *fTxSocket;
|
|---|
| 54 | TMutex fMutex;
|
|---|
| 55 |
|
|---|
| 56 | Int_t fPortTx;
|
|---|
| 57 |
|
|---|
| 58 | Int_t Thread();
|
|---|
| 59 |
|
|---|
| 60 | public:
|
|---|
| 61 | MTcpIpO(const char *addr, Int_t tx);
|
|---|
| 62 | ~MTcpIpO();
|
|---|
| 63 |
|
|---|
| 64 | static TString GetSocketAddress(const TSocket &s);
|
|---|
| 65 | static bool SendFrame(TSocket &tx, const char *msg, int len);
|
|---|
| 66 | static bool SendFrame(const char *addr, int port, const char *msg, int len);
|
|---|
| 67 |
|
|---|
| 68 | bool Send(const char *msg, int len);
|
|---|
| 69 | };
|
|---|
| 70 |
|
|---|
| 71 | class MTcpIpCC : public MTcpIpI
|
|---|
| 72 | {
|
|---|
| 73 | private:
|
|---|
| 74 | Bool_t ReadSocket(TSocket &rx);
|
|---|
| 75 |
|
|---|
| 76 | public:
|
|---|
| 77 | MTcpIpCC(Int_t rx, UInt_t timeout=5000);
|
|---|
| 78 |
|
|---|
| 79 | virtual bool InterpreteStr(TString str);
|
|---|
| 80 | };
|
|---|
| 81 |
|
|---|
| 82 | // This class es espcially meant to receive and send ascii messages
|
|---|
| 83 | class MTcpIpIO : public MTcpIpCC, public MTcpIpO
|
|---|
| 84 | {
|
|---|
| 85 |
|
|---|
| 86 | public:
|
|---|
| 87 | MTcpIpIO(const char *addr, Int_t tx, Int_t rx, UInt_t timeout=5000);
|
|---|
| 88 | };
|
|---|
| 89 |
|
|---|
| 90 | class MTcpIpOI : public MTcpIpO
|
|---|
| 91 | {
|
|---|
| 92 | private:
|
|---|
| 93 | Int_t Thread();
|
|---|
| 94 | virtual Bool_t ReadSocket(TSocket &rx) = 0;
|
|---|
| 95 |
|
|---|
| 96 | Bool_t fConnectionEstablished;
|
|---|
| 97 | Int_t fTimeout; // [ms] Timeout to listen for data to be received
|
|---|
| 98 | public:
|
|---|
| 99 | MTcpIpOI(const char *addr, Int_t tx, UInt_t timeout=5000) : MTcpIpO(addr, tx), fConnectionEstablished(kFALSE), fTimeout(timeout) { }
|
|---|
| 100 |
|
|---|
| 101 | Bool_t IsConnectionEstablished() const { return fConnectionEstablished; }
|
|---|
| 102 | };
|
|---|
| 103 |
|
|---|
| 104 | class MTcpIpFact : public MTcpIpCC
|
|---|
| 105 | {
|
|---|
| 106 | private:
|
|---|
| 107 |
|
|---|
| 108 | public:
|
|---|
| 109 | MTcpIpFact(const char *dumm1, Int_t dummy2, Int_t rx, UInt_t timeout) : MTcpIpCC(rx, timeout)
|
|---|
| 110 | {
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | bool Send(const char *msg, int len);
|
|---|
| 114 | };
|
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 | #endif
|
|---|