| 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 | private:
|
|---|
| 23 | Int_t fPortRx; // Port on which to listen for connections
|
|---|
| 24 | Int_t fTimeout; // [ms] Timeout to listen for data to be received
|
|---|
| 25 |
|
|---|
| 26 | Bool_t fConnectionEstablished;
|
|---|
| 27 |
|
|---|
| 28 | MTime fLast;
|
|---|
| 29 |
|
|---|
| 30 | Bool_t WaitForData(TSocket &sock);
|
|---|
| 31 | void WaitForConnection(TServerSocket &server);
|
|---|
| 32 |
|
|---|
| 33 | Int_t Thread();
|
|---|
| 34 |
|
|---|
| 35 | virtual Bool_t ReadSocket(TSocket &rx) = 0;
|
|---|
| 36 |
|
|---|
| 37 | public:
|
|---|
| 38 | MTcpIpI(Int_t rx, UInt_t timeout=5000) : MThread(Form("MTcpIpI::%d", rx)), fPortRx(rx), fTimeout(timeout), fConnectionEstablished(kFALSE) { /*RunThread();*/ }
|
|---|
| 39 | ~MTcpIpI() { CancelThread(); }
|
|---|
| 40 |
|
|---|
| 41 | Bool_t IsConnectionEstablished() const { return fConnectionEstablished; }
|
|---|
| 42 | };
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 | // A generalized class for sending over tcp/ip
|
|---|
| 46 | class MTcpIpO : public MThread
|
|---|
| 47 | {
|
|---|
| 48 | private:
|
|---|
| 49 | TSocket *fTxSocket;
|
|---|
| 50 | TMutex fMutex;
|
|---|
| 51 |
|
|---|
| 52 | Int_t fPortTx;
|
|---|
| 53 |
|
|---|
| 54 | Int_t Thread();
|
|---|
| 55 |
|
|---|
| 56 | public:
|
|---|
| 57 | MTcpIpO(const char *addr, Int_t tx);
|
|---|
| 58 | ~MTcpIpO();
|
|---|
| 59 |
|
|---|
| 60 | static TString GetSocketAddress(const TSocket &s);
|
|---|
| 61 | static bool SendFrame(TSocket &tx, const char *msg, int len);
|
|---|
| 62 | static bool SendFrame(const char *addr, int port, const char *msg, int len);
|
|---|
| 63 |
|
|---|
| 64 | bool Send(const char *msg, int len);
|
|---|
| 65 | };
|
|---|
| 66 |
|
|---|
| 67 | // This class es espcially meant to receive and send ascii messages
|
|---|
| 68 | class MTcpIpIO : public MTcpIpI, public MTcpIpO
|
|---|
| 69 | {
|
|---|
| 70 | private:
|
|---|
| 71 | Bool_t ReadSocket(TSocket &rx);
|
|---|
| 72 |
|
|---|
| 73 | public:
|
|---|
| 74 | MTcpIpIO(const char *addr, Int_t tx, Int_t rx);
|
|---|
| 75 | ~MTcpIpIO();
|
|---|
| 76 |
|
|---|
| 77 | virtual bool InterpreteStr(TString str);
|
|---|
| 78 | };
|
|---|
| 79 |
|
|---|
| 80 | #endif
|
|---|