source: trunk/Cosy/tcpip/MTcpIpIO.h@ 18877

Last change on this file since 18877 was 12598, checked in by tbretz, 13 years ago
Implemented a new class MTcpIpFact using a duplex communication for FACT
File size: 2.5 KB
Line 
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
15class TString;
16class TSocket;
17class TServerSocket;
18
19// A generalized class for receiving over tcp/ip
20class MTcpIpI : public MThread
21{
22protected:
23 TSocket *fRxSocket;
24 TMutex fRxMutex;
25
26private:
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
41public:
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
50class MTcpIpO : public MThread
51{
52protected:
53 TSocket *fTxSocket;
54 TMutex fMutex;
55
56 Int_t fPortTx;
57
58 Int_t Thread();
59
60public:
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
71class MTcpIpCC : public MTcpIpI
72{
73private:
74 Bool_t ReadSocket(TSocket &rx);
75
76public:
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
83class MTcpIpIO : public MTcpIpCC, public MTcpIpO
84{
85
86public:
87 MTcpIpIO(const char *addr, Int_t tx, Int_t rx, UInt_t timeout=5000);
88};
89
90class MTcpIpOI : public MTcpIpO
91{
92private:
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
98public:
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
104class MTcpIpFact : public MTcpIpCC
105{
106private:
107
108public:
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
Note: See TracBrowser for help on using the repository browser.