-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathServerTerminal.cs
More file actions
158 lines (130 loc) · 4.18 KB
/
ServerTerminal.cs
File metadata and controls
158 lines (130 loc) · 4.18 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
using System;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
namespace SDRSharp.RDSOutput
{
public class ServerTerminal
{
Socket m_socket;
SocketListener m_listener;
private bool m_Closed;
private Socket m_socWorker;
public event TCPTerminal_MessageRecivedDel MessageRecived;
public event TCPTerminal_ConnectDel ClientConnect;
public event TCPTerminal_DisconnectDel ClientDisconnect;
public void StartListen(int port)
{
IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any, port);
m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//bind to local IP Address...
//if ip address is allready being used write to log log
try
{
m_socket.Bind(ipLocal);
}
catch(Exception ex)
{
Debug.Fail(ex.ToString(),string.Format("Can't connect to port {0}!", port));
return;
}
//start listening...
m_socket.Listen(4);
// create the call back for any client connections...
m_socket.BeginAccept(new AsyncCallback(OnClientConnection), null);
}
private void OnClientConnection(IAsyncResult asyn)
{
if (m_Closed)
{
return;
}
try
{
m_socWorker = m_socket.EndAccept(asyn);
RaiseClientConnected(m_socWorker);
m_listener = new SocketListener();
m_listener.MessageRecived += OnMessageRecived;
m_listener.Disconnected += OnClientDisconnection;
m_listener.StartReciving(m_socWorker);
}
catch (ObjectDisposedException odex)
{
Debug.Fail(odex.ToString(), "OnClientConnection: Socket has been closed");
}
catch (Exception sex)
{
Debug.Fail(sex.ToString(), "OnClientConnection: Socket failed");
}
}
private void OnClientDisconnection(Socket socket)
{
RaiseClientDisconnected(socket);
// Try to re-establish connection
m_socket.BeginAccept(new AsyncCallback(OnClientConnection), null);
}
public void SendMessage(string mes)
{
if (m_socWorker == null)
{
return;
}
try
{
Object objData = mes;
byte[] byData = System.Text.Encoding.ASCII.GetBytes(objData.ToString());
m_socWorker.Send(byData);
}
catch(SocketException se)
{
}
/* catch (SocketException se)
{
Debug.Fail(se.ToString(), string.Format("Message '{0}' could not be sent", mes));
}*/
}
public void Close()
{
try
{
if (m_socket != null)
{
m_Closed = true;
if (m_listener != null)
{
m_listener.StopListening();
}
m_socket.Close();
m_listener = null;
m_socWorker = null;
m_socket = null;
}
}
catch (ObjectDisposedException odex)
{
Debug.Fail(odex.ToString(), "Stop failed");
}
}
private void OnMessageRecived(string message)
{
if (MessageRecived != null)
{
MessageRecived(message);
}
}
private void RaiseClientConnected(Socket socket)
{
if (ClientConnect != null)
{
ClientConnect(socket);
}
}
private void RaiseClientDisconnected(Socket socket)
{
if (ClientDisconnect != null)
{
ClientDisconnect(socket);
}
}
}
}