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

Last change on this file since 10398 was 10031, checked in by tbretz, 14 years ago
Changed connection from SPS to a client-only connection.
File size: 2.2 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{
22private:
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
37public:
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
46class MTcpIpO : public MThread
47{
48protected:
49 TSocket *fTxSocket;
50 TMutex fMutex;
51
52 Int_t fPortTx;
53
54 Int_t Thread();
55
56public:
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
68class MTcpIpIO : public MTcpIpI, public MTcpIpO
69{
70private:
71 Bool_t ReadSocket(TSocket &rx);
72
73public:
74 MTcpIpIO(const char *addr, Int_t tx, Int_t rx, UInt_t timeout=5000);
75
76 virtual bool InterpreteStr(TString str);
77};
78
79class MTcpIpOI : public MTcpIpO
80{
81private:
82 Int_t Thread();
83 virtual Bool_t ReadSocket(TSocket &rx) = 0;
84
85 Bool_t fConnectionEstablished;
86 Int_t fTimeout; // [ms] Timeout to listen for data to be received
87public:
88 MTcpIpOI(const char *addr, Int_t tx, UInt_t timeout=5000) : MTcpIpO(addr, tx), fConnectionEstablished(kFALSE), fTimeout(timeout) { }
89
90 Bool_t IsConnectionEstablished() const { return fConnectionEstablished; }
91};
92
93#endif
Note: See TracBrowser for help on using the repository browser.