forked from cautonwong/GuruxDLMSLibServerExample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGXNet.h
More file actions
343 lines (294 loc) · 7.1 KB
/
GXNet.h
File metadata and controls
343 lines (294 loc) · 7.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
//
// --------------------------------------------------------------------------
// Gurux Ltd
//
//
//
// Filename: $HeadURL$
//
// Version: $Revision$,
// $Date$
// $Author$
//
// Copyright (c) Gurux Ltd
//
//---------------------------------------------------------------------------
//
// DESCRIPTION
//
// This file is a part of Gurux Device Framework.
//
// Gurux Device Framework is Open Source software; you can redistribute it
// and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; version 2 of the License.
// Gurux Device Framework is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.
//
// More information of Gurux products: http://www.gurux.org
//
// This code is licensed under the GNU General Public License v2.
// Full text may be retrieved at http://www.gnu.org/licenses/gpl-2.0.txt
//---------------------------------------------------------------------------
#pragma once
#include "GuruxCommon/GXMedia.h"
#include "GuruxCommon/ReceiveParameters.h"
#include "GuruxCommon/GXMediaListener.h"
#include "ConnectionEventArgs.h"
#include "GuruxCommon/ReceiveEventArgs.h"
#include "GXNetListener.h"
/**
The GXNet component determines methods that make the communication possible using Internet.
*/
class CGXNet : public IGXMedia
{
//TODO; HANDLE m_Received;
bool m_Synchronous;
GX_TRACE_LEVEL m_Trace;
int m_Port;
basic_string<char> m_HostName;
unsigned long m_BytesReceived, m_BytesSent;
enum GX_NW_TYPE m_Protocol;
bool m_IsServer;
vector<IGXNetListener*> m_NetListeners;
vector<IGXMediaListener*> m_MediaListeners;
void Init(enum GX_NW_TYPE protocol, basic_string<char>& hostName, int port);
basic_string<char> GetProtocolAsString()
{
if (m_Protocol == GX_NW_TCPIP)
{
return "TCP/IP";
}
return "UDP";
}
int ShowError(const char* msg)
{
#ifdef WIN32
return WSAGetLastError();
#else
perror(msg);
return errno;
#endif
}
public:
unsigned char m_Receivebuff[1024];
int m_ReceiveBuffPos;
int m_Socket;
int m_ServerSocket;
public:
/**
Constructor.
*/
CGXNet(void);
/**
Server Constructor.
*/
CGXNet(GX_NW_TYPE protocol, int port);
/**
Client Constructor.
*/
CGXNet(GX_NW_TYPE protocol, string hostName, int port);
~CGXNet(void);
/**
Checks if the connection is in synchronous mode.
@return True, if the connection is in synchronous mode.
*/
bool GetSynchronous()
{
return m_Synchronous;
}
/**
Sets connection synchronous mode.
*/
void SetSynchronous(bool value)
{
m_Synchronous = value;
}
/**
Opens the media.
*/
int Open();
/**
Closes the active connection.
@see Open Open
*/
int Close();
/**
Retrieves or sets the name or IP address of the host.
<value>
The name of the host.
</value>
@see Open Open
@see Port Port
@see Protocol Protocol
*/
basic_string<char> GetHostName();
void SetHostName(basic_string<char> value);
/**
Retrieves or sets the host or server port number.
<value>
Host or server port number.
</value>
@see Open Open
@see HostName HostName
@see Protocol Protocol
*/
int GetPort()
{
return m_Port;
}
void SetPort(int value)
{
m_Port = value;
}
/**
Retrieves or sets the protocol.
Defaut protocol is UDP.
<value>
Protocol
</value>
*/
GX_NW_TYPE getProtocol()
{
return m_Protocol;
}
void SetProtocol(GX_NW_TYPE value)
{
m_Protocol = value;
}
/**
Determines if the component is in server, or in client, mode.
<value>
True, if component is a server. False, if component is a client.
</value>
@see Open Open
*/
bool IsServer()
{
return m_IsServer;
}
void IsServer(bool value)
{
m_IsServer = value;
}
/**
Received byte count.
@see BytesSent BytesSent
@see ResetByteCounters ResetByteCounters
*/
unsigned long GetBytesReceived()
{
return m_BytesReceived;
}
/**
Sent byte count.
@see BytesReceived BytesReceived
@see ResetByteCounters ResetByteCounters
*/
unsigned long GetBytesSent()
{
return m_BytesSent;
}
/**
Trace level of the IGXMedia for System.Diagnostic.Trace.Writes.
{@link OnTrace}
*/
GX_TRACE_LEVEL GetTrace()
{
return m_Trace;
}
void SetTrace(GX_TRACE_LEVEL value)
{
m_Trace = value;
}
/**
Checks if the connection is established.
@return True, if the connection is established.
*/
bool IsOpen()
{
if (m_IsServer)
{
return m_ServerSocket != -1;
}
return m_Socket != -1;
}
/**
Waits for more reply data After SendSync if whole packet is not received yet.
@param args Receive data arguments.
*/
int Receive(CReceiveParameters& args);
/**
Resets BytesReceived and BytesSent counters.
@see BytesSent BytesSent
@see BytesReceived BytesReceived
*/
void ResetByteCounters();
/**
Sends data asynchronously. <br/>
No reply from the receiver, whether or not the operation was successful, is expected.
@param data Data to send to the device.
@param receiver IP address of the receiver (optional).
Reply data is received through OnReceived event.<br/>
@see OnReceived OnReceived
@see Open Open
@see Close Close
*/
int Send(vector<unsigned char>& data, basic_string<char> receiver);
int Send(unsigned char* pData, int len, basic_string<char> receiver);
/*
* Start to listen media events.
*/
void AddListener(IGXMediaListener* pListener)
{
m_MediaListeners.push_back(pListener);
IGXNetListener* p = dynamic_cast<IGXNetListener*>(pListener);
if (p != NULL)
{
m_NetListeners.push_back(p);
}
}
/*
* Stop to listen media events.
*/
void RemoveListener(IGXMediaListener* pListener)
{
for(vector<IGXMediaListener*>::iterator it = m_MediaListeners.begin();
it != m_MediaListeners.end(); ++it)
{
if (pListener == (*it))
{
m_MediaListeners.erase(it);
break;
}
}
IGXNetListener* p = dynamic_cast<IGXNetListener*>(pListener);
if (p != NULL)
{
for(vector<IGXNetListener*>::iterator it = m_NetListeners.begin();
it != m_NetListeners.end(); ++it)
{
if (p == (*it))
{
m_NetListeners.erase(it);
break;
}
}
}
}
//Notify that client is connected.
void NotifyClientConnected(CConnectionEventArgs& e);
//Notify that client is disconnected.
void NotifyClientDisconnected(CConnectionEventArgs& e);
//Notify that property is changed.
void NotifyPropertyChanged(basic_string<char>& info);
//Notify error.
void NotifyError(basic_string<char>& ex);
//Notify that new data is received..
void NotifyReceived(CReceiveEventArgs& e);
//Notify trace.
void NotifyTrace(CTraceEventArgs& e);
//Notify that media state is changed.
void NotifyMediaStateChange(GX_MEDIA_STATE_CHANGE state);
};