OpendTect-6_4  6.4
netreqconnection.h
Go to the documentation of this file.
1 #ifndef netreqcommunic_h
2 #define netreqcommunic_h
3 
4 /*+
5 ________________________________________________________________________
6 
7  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
8  Author: K. Tingdahl
9  Date: August 2014
10  RCS: $Id$
11 ________________________________________________________________________
12 
13 -*/
14 
15 #include "networkmod.h"
16 #include "gendefs.h"
17 #include "thread.h"
18 #include "objectset.h"
19 #include "callback.h"
20 #include "uistring.h"
21 
22 
23 namespace Network
24 {
25 
26 class Socket;
27 class Server;
28 class RequestPacket;
29 
30 
31 /*\brief
32  Manages RequestPackets by sending/receiveing them using a Tcp Connection.
33 
34  To send a packet, make the RequestPacket and use sendPacket().
35 
36  To receive, you want only packets for your request ID. If you set a timeout
37  the connection will wait for a packet with your ID if there wasn't already
38  one present. On success, the returned packet is yours.
39 
40  If the other side can also send requests to us, then you need to periodically
41  fetch your packets until getNextExternalPacket() returns null.
42 
43  */
44 
45 
48 public:
49  RequestConnection(const char* servername,
50  unsigned short serverport,
51  bool multithreaded=true,
52  int connectiontimeout=-1);
54  RequestConnection(Socket*);
59 
60  bool isOK() const;
61  const char* server() const { return servername_; }
62  unsigned short port() const { return serverport_; }
63  int ID() const { return id_; }
64 
65  bool sendPacket(const RequestPacket&,
66  bool waitforfinish=false);
71  RequestPacket* pickupPacket(od_int32 reqid,int timeout /* in ms */,
72  int* errorcode=0);
77  RequestPacket* getNextExternalPacket();
78 
79  static int cInvalidRequest() { return 1; }
80  static int cTimeout() { return 2; }
81  static int cDisconnected() { return 3; }
82 
83  bool isMultiThreaded() { return socketthread_; }
84  Socket* socket() { return socket_; }
85 
88 
89  uiString errMsg() const { return errmsg_; }
90 
91 private:
92 
94 
95  enum ThreadReadStatus { None, TryRead, ReadOK, ReadFail };
97 
100 
102  Socket* socket_;
103  int timeout_;
105 
111 
112  int id_;
113 
115  unsigned short serverport_;
116 
118  void socketThreadFunc(CallBacker*);
119  bool stopflag_;
121 
122  void connectToHost();
123  void flush();
124  void connCloseCB(CallBacker*);
125  void newConnectionCB(CallBacker*);
126  void dataArrivedCB(CallBacker*);
127 
128  bool doSendPacket(const RequestPacket&,
129  bool waitforfinish);
130 
131  bool readFromSocket();
132  bool writeToSocket();
133 
134  Network::RequestPacket* readConnection(int);
135  Network::RequestPacket* getNextAlreadyRead(int);
136  void requestEnded(od_int32);
137 };
138 
139 
140 
147 public:
148  RequestServer(unsigned short serverport);
149  ~RequestServer();
150 
151  bool isOK() const;
152  Server* server() { return server_; }
153 
155  RequestConnection* pickupNewConnection();
157 
158  uiString errMsg() const { return errmsg_; }
159 
160 private:
161 
162  void newConnectionCB(CallBacker*);
163 
165 
167 
169  unsigned short serverport_;
170  Server* server_;
171 };
172 
173 
174 }; //Namespace
175 
176 
177 #endif
static int cDisconnected()
Definition: netreqconnection.h:81
#define mExpClass(module)
Definition: commondefs.h:160
Is an object that faciliates many threads to wait for something to happen.
Definition: thread.h:108
ThreadReadStatus
Definition: netreqconnection.h:95
To be able to send and/or receive CallBacks, inherit from this class.
Definition: callback.h:272
A lock of a type that (hopefully) suits your needs. To use it, you need the Locker class...
Definition: threadlock.h:53
bool readfirst_
Definition: netreqconnection.h:120
Definition: jobcommunic.h:24
bool sendwithwait_
Definition: netreqconnection.h:107
bool sendresult_
Definition: netreqconnection.h:108
Threads::Thread * socketthread_
Definition: netreqconnection.h:117
#define mODTextTranslationClass(clss)
Definition: uistring.h:38
CNotifier< RequestConnection, od_int32 > packetArrived
Definition: netreqconnection.h:86
Socket * socket()
Definition: netreqconnection.h:84
ThreadReadStatus threadreadstatus_
Definition: netreqconnection.h:96
int timeout_
Definition: netreqconnection.h:103
const char * server() const
Definition: netreqconnection.h:61
int id_
Definition: netreqconnection.h:112
unsigned short serverport_
Definition: netreqconnection.h:115
int ID() const
Definition: netreqconnection.h:63
Server * server_
Definition: netreqconnection.h:170
Socket * socket_
Definition: netreqconnection.h:102
unsigned short serverport_
Definition: netreqconnection.h:169
uiString errMsg() const
Becomes yours.
Definition: netreqconnection.h:158
Definition: uistring.h:89
Definition: netreqpacket.h:39
FixedString None()
Definition: keystrs.h:90
uiString errmsg_
Definition: netreqconnection.h:93
Definition: netreqconnection.h:46
Definition: callback.h:254
Set of pointers to objects.
Definition: commontypes.h:32
Is the base class for all threads. Start it by creating it and give it the function or CallBack to ex...
Definition: thread.h:283
Definition: netreqconnection.h:95
Definition: netreqconnection.h:145
bool isMultiThreaded()
Definition: netreqconnection.h:83
Threads::Lock lock_
Definition: netreqconnection.h:168
const RequestPacket * packettosend_
Definition: netreqconnection.h:106
unsigned short port() const
Definition: netreqconnection.h:62
TypeSet< od_int32 > ourrequestids_
Definition: netreqconnection.h:98
Threads::ConditionVar lock_
Definition: netreqconnection.h:101
bool stopflag_
Definition: netreqconnection.h:119
#define od_int32
Definition: plftypes.h:31
OD::String with its own variable length buffer. The buffer has a guaranteed minimum size...
Definition: bufstring.h:40
Notifier< RequestConnection > connectionClosed
Definition: netreqconnection.h:87
bool sendingfinished_
Definition: netreqconnection.h:109
Server * server()
Definition: netreqconnection.h:152
BufferString errmsg_
Definition: horizontracker.h:119
bool triggerread_
Definition: netreqconnection.h:110
ObjectSet< RequestPacket > receivedpackets_
Definition: netreqconnection.h:99
static int cTimeout()
Definition: netreqconnection.h:80
static int cInvalidRequest()
Definition: netreqconnection.h:79
uiString errmsg_
Definition: netreqconnection.h:164
Notifier with automatic capsule creation.
Definition: callback.h:395
Notifier< RequestServer > newConnection
Definition: netreqconnection.h:154
ObjectSet< RequestConnection > pendingconns_
Definition: netreqconnection.h:166
uiString errMsg() const
Definition: netreqconnection.h:89
BufferString servername_
Definition: netreqconnection.h:114
bool ownssocket_
Definition: netreqconnection.h:104

Generated at for the OpendTect seismic interpretation project. Copyright (C): dGB Beheer B. V. 2019