-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSocketListener.cs
More file actions
145 lines (123 loc) · 4.04 KB
/
SocketListener.cs
File metadata and controls
145 lines (123 loc) · 4.04 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
using System;
using System.Diagnostics;
using System.Net.Sockets;
namespace SDRSharp.RDSOutput
{
public class SocketListener
{
public class CSocketPacket
{
public System.Net.Sockets.Socket thisSocket;
public byte[] dataBuffer;
public CSocketPacket(int buffeLength)
{
dataBuffer = new byte[buffeLength];
}
}
private const int BufferLength = 1000;
AsyncCallback pfnWorkerCallBack;
Socket m_socWorker;
public event TCPTerminal_MessageRecivedDel MessageRecived;
public event TCPTerminal_DisconnectDel Disconnected;
public void StartReciving(Socket socket)
{
m_socWorker = socket;
WaitForData(socket);
}
private void WaitForData(System.Net.Sockets.Socket soc)
{
try
{
if (pfnWorkerCallBack == null)
{
pfnWorkerCallBack = new AsyncCallback(OnDataReceived);
}
CSocketPacket theSocPkt = new CSocketPacket(BufferLength);
theSocPkt.thisSocket = soc;
// now start to listen for any data...
soc.BeginReceive(
theSocPkt.dataBuffer,
0,
theSocPkt.dataBuffer.Length,
SocketFlags.None,
pfnWorkerCallBack,
theSocPkt);
}
catch (SocketException sex)
{
Debug.Fail(sex.ToString(), "WaitForData: Socket failed");
}
}
private void OnDataReceived(IAsyncResult asyn)
{
CSocketPacket theSockId = (CSocketPacket)asyn.AsyncState;
Socket socket = theSockId.thisSocket;
if (!socket.Connected)
{
return;
}
try
{
int iRx;
try
{
iRx = socket.EndReceive(asyn);
}
catch (SocketException)
{
Debug.Write("Apperently client has been closed and cannot answer.");
OnConnectionDroped(socket);
return;
}
if (iRx == 0)
{
Debug.Write("Apperently client socket has been closed.");
// If client socket has been closed (but client still answers)-
// EndReceive will return 0.
OnConnectionDroped(socket);
return;
}
char[] chars = new char[iRx + 1];
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
System.String szData = new System.String(chars);
HandleMessage(szData);
WaitForData(m_socWorker);
}
catch (Exception ex)
{
Debug.Fail(ex.ToString(), "OnClientConnection: Socket failed");
}
}
public void StopListening()
{
// Incase connection has been established with remote client -
// Raise the OnDisconnection event.
if (m_socWorker != null)
{
// m_socWorker.Shutdown(SocketShutdown.Both);
m_socWorker.Close();
m_socWorker = null;
}
}
private void HandleMessage(string data)
{
if (MessageRecived != null)
{
MessageRecived(data);
}
}
private void OnDisconnection(Socket socket)
{
if (Disconnected != null)
{
Disconnected(socket);
}
}
private void OnConnectionDroped(Socket socket)
{
m_socWorker = null;
OnDisconnection(socket);
}
}
}