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

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