-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClientImpl.cs
203 lines (169 loc) · 5.61 KB
/
ClientImpl.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
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;
using System.Text.Json;
namespace InterProcessCommunication
{
internal class ClientImpl
{
private IPEndPoint mRemoteEP;
private Socket mSocket;
private string mMessage = string.Empty;
public string Response() => mMessage;
public ClientImpl()
{
}
~ClientImpl()
{
if (mSocket.Connected) { Disconnect(); }
}
public bool IsConnected
{
get { return mSocket.Connected; }
}
public bool Connect(string pServerAddr, int pPort)
{
try
{
IPAddress ipAddress = IPAddress.Loopback;
if(Uri.CheckHostName(pServerAddr) == UriHostNameType.Dns)
{
IPHostEntry ipHostInfo = Dns.GetHostEntry(pServerAddr);
ipAddress = ipHostInfo.AddressList[0];
}
else
{
ipAddress = IPAddress.Parse(pServerAddr);
}
mRemoteEP = new IPEndPoint(ipAddress, pPort);
// Create a TCP/IP socket.
mSocket = new Socket(
ipAddress.AddressFamily,
SocketType.Stream,
ProtocolType.Tcp);
// Connect to the remote endpoint.
mSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
mSocket.Connect(mRemoteEP);
Console.WriteLine(
"Connected to {0}",
mSocket.RemoteEndPoint.ToString());
if(!mSocket.Connected)
{
return false;
}
}
catch (Exception e)
{
Console.WriteLine("Can't connect to the server {0}:{1}", mRemoteEP.Address, mRemoteEP.Port);
return false;
}
return true;
}
public void Disconnect()
{
// Release the socket.
mSocket.Shutdown(SocketShutdown.Both);
mSocket.Close();
Console.WriteLine("Socket closed");
}
public void Receive()
{
SocketStateObject state = new SocketStateObject();
state.mClientSocket = mSocket;
while (mSocket.Connected)
{
Thread.Sleep(10);
try
{
if (mSocket.Available > 0)
{
state.mSb.Clear();
// Create the state object.
ReadSocket(mSocket, state);
}
}
catch(System.Net.Sockets.SocketException e)
{
Console.WriteLine("Socket Exception, quit");
return;
}
catch (Exception e)
{
Console.WriteLine("Can't receive data from the server: {0}");
return;
}
}
}
private void ReadSocket(Socket pSocket, SocketStateObject pStateObject)
{
try
{
int bytesRead = pSocket.Receive(pStateObject.mBuffer);
if (bytesRead > 0)
{
pStateObject.mSb.Append(
Encoding.ASCII.GetString(
pStateObject.mBuffer,
0,
bytesRead));
if ( pSocket.Available > 0)
{
ReadSocket(pSocket, pStateObject);
}
}
if (pStateObject.mSb.Length > 1)
{
mMessage = pStateObject.mSb.ToString();
// Process with the incoming data here
Console.WriteLine(
"Arrived {0} bytes from socket.\n Data: {1}",
mMessage.Length,
mMessage);
// Process with the incoming data here
// Data exchange, decoding
DataEncoderImpl.Decapsulate(mMessage);
}
}
catch (Exception e)
{
Console.WriteLine("Can't receive data from the server.");
}
}
public int Send(string pData)
{
byte[] byteData = Encoding.ASCII.GetBytes(pData);
try {
mSocket.BeginSend(
byteData,
0,
byteData.Length,
0,
new AsyncCallback(SendCallback),
mSocket);
}
catch (Exception e)
{
Console.WriteLine("Can't send data to the server");
}
return byteData.Length;
}
public void SendCallback(IAsyncResult pAr)
{
try
{
Socket client = (Socket)pAr.AsyncState;
int bytesSent = client.EndSend(pAr);
Console.WriteLine(
"Sent {0} bytes to server.",
bytesSent);
}
catch (Exception e)
{
Console.WriteLine(
"Can't send data to the server");
}
}
}
}