-
Notifications
You must be signed in to change notification settings - Fork 1
/
SocketClient.cs
253 lines (212 loc) · 7.63 KB
/
SocketClient.cs
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
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
using System.Timers;
namespace SocketClientServer
{
public class SocketClient : SocketClientServerBase
{
public bool Connected { get { return Socket != null && Socket.Connected; } }
public Socket Socket { get; private set; }
public int ConnectTimeout { get; private set; }
public bool Reconnect
{
get { return _reconnect; }
set
{
_reconnect = value;
if (_reconnect)
{
_reconnectTimer = new Timer(ReconnectTimeout);
_reconnectTimer.AutoReset = true;
_reconnectTimer.Elapsed += _reconnectTimer_Elapsed;
_reconnectTimer.Start();
}
else
{
_reconnectTimer = null;
}
}
}
private void _reconnectTimer_Elapsed(object sender, ElapsedEventArgs e)
{
lock (_reconnectTimer)
{
if (Reconnect && Socket != null && !Socket.IsSocketConnected())
{
Open(_addresss, _port);
}
}
}
private int _reconnectTimeout = 10000;
public int ReconnectTimeout
{
get { return _reconnectTimeout; }
set
{
_reconnectTimeout = value;
if (_reconnectTimer != null) _reconnectTimer.Interval = value;
}
}
private IAsyncResult m_result;
private AsyncCallback m_pfnCallBack;
private int _timeout = 0, _rtc = 0;
private Timer _timer;
//private string _sendFrame = string.Empty;
private long _InCount = 0, _OutCount = 0;
public delegate void ReadCallbackDelegate(object parameter);
public delegate void OpenCallbackDelegate(bool state);
public delegate void CloseCallbackDelegate(bool state);
public delegate void ErrorCallbackDelegate(Exception e);
public ReadCallbackDelegate OnReadCallback = null;
public OpenCallbackDelegate OnOpenCallback = null;
public CloseCallbackDelegate OnCloseCallback = null;
private bool _reconnect = false;
private Timer _reconnectTimer;
private string _addresss;
private int _port;
public SocketClient()
{
_timer = new Timer(1000);
_timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed);
_timer.Stop();
}
private void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
ConnectTimeout++;
}
public void Send(string frame)
{
try
{
// New code to send strings
/*NetworkStream networkStream = new NetworkStream(m_clientSocket);
System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(networkStream);
streamWriter.WriteLine(msg);
streamWriter.Flush();*/
// send bytes
byte[] byData = System.Text.Encoding.ASCII.GetBytes(frame);
if (Socket != null)
{
Socket.Send(byData);
_OutCount += frame.Length;
}
}
catch (SocketException se)
{
HandleException("Send", se);
}
}
public void Open(string address, int port)
{
_addresss = address;
_port = port;
Task.Factory
.StartNew(() =>
{
if (Socket != null && Socket.Connected)
{
_timer.Stop();
ConnectTimeout = 0;
return;
}
try
{
_timer.Start();
// Create the socket instance
Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Cet the remote IP address
IPAddress ip = IPAddress.Parse(address);
int iPortNo = System.Convert.ToInt16(port);
logger.Info(string.Format("Connecting to {0}:{1}", ip.ToString(), iPortNo));
// Create the end point
var ipEnd = new IPEndPoint(ip, iPortNo);
// Connect to the remote host
Socket.Connect(ipEnd);
if (Socket.Connected)
{
if (OnOpenCallback != null)
{
OnOpenCallback(true);
}
//Wait for data asynchronously
WaitForData();
}
}
catch (Exception e)
{
HandleException("Open", e);
}
});
}
public class SocketPacket
{
public System.Net.Sockets.Socket thisSocket;
public byte[] dataBuffer = new byte[1024];
}
private void WaitForData()
{
try
{
if (m_pfnCallBack == null)
{
m_pfnCallBack = new AsyncCallback(OnDataReceived);
}
SocketPacket theSocPkt = new SocketPacket();
theSocPkt.thisSocket = Socket;
// Start listening to the data asynchronously
if (Socket != null)
m_result = Socket.BeginReceive(theSocPkt.dataBuffer,
0, theSocPkt.dataBuffer.Length,
SocketFlags.None,
m_pfnCallBack,
theSocPkt);
}
catch (SocketException se)
{
HandleException("Wait for data", se);
}
}
private void OnDataReceived(IAsyncResult asyn)
{
try
{
SocketPacket theSockId = (SocketPacket)asyn.AsyncState;
if (!theSockId.thisSocket.Connected)
return;
var iRx = theSockId.thisSocket.EndReceive(asyn);
var chars = new char[iRx + 1];
var d = System.Text.Encoding.UTF8.GetDecoder();
var charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
var szData = new System.String(chars);
_InCount += szData.Length;
if (OnReadCallback != null)
{
OnReadCallback(szData);
}
WaitForData();
}
catch (ObjectDisposedException e)
{
HandleException("On data received object disposed", e);
}
catch (SocketException e)
{
HandleException("On data received socket exception", e);
}
}
public void Close()
{
if (Socket != null)
{
Socket.Close();
Socket = null;
if (OnCloseCallback != null)
{
OnCloseCallback(false);
}
}
}
}
}