forked from rickparrish/RMLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIPCSocketServerThread.cs
157 lines (137 loc) · 5.8 KB
/
IPCSocketServerThread.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
/*
RMLib: Nonvisual support classes used by multiple R&M Software programs
Copyright (C) Rick Parrish, R&M Software
This file is part of RMLib.
RMLib is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
RMLib 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with RMLib. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.IO;
using RandM.RMLib;
using System.Globalization;
using System.Threading;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Net;
using System.Text;
namespace RandM.RMLib
{
public class IPCSocketServerThread : RMThread
{
public const char EndStatement = '\xFF';
public event EventHandler<StringEventArgs> ClientCommandEvent = null;
public event EventHandler<StringEventArgs> ErrorMessageEvent = null;
public event EventHandler<ExceptionEventArgs> ExceptionEvent = null;
public event EventHandler<StringEventArgs> MessageEvent = null;
private StringBuilder _Buffer = new StringBuilder();
private TcpConnection _ClientConnection = null;
private TcpConnection _Listener = null;
public IPCSocketServerThread(TcpConnection serverConnection)
{
_Listener = serverConnection;
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
protected override void Execute()
{
while (!_Stop)
{
// Accept an incoming control client connection
if (_Listener.CanAccept(1000)) // 1 second
{
try
{
RaiseMessageEvent("IPCSocketServer about to accept a client connection");
_ClientConnection = _Listener.AcceptTCP();
if (_ClientConnection != null)
{
string Request = _ClientConnection.ReadLn("\r\n", false, '\0', 1000);
if (Request == "OK?")
{
_ClientConnection.WriteLn("OK!");
RaiseMessageEvent("Control server accepted a client connection from " + _ClientConnection.GetRemoteIP() + ":" + _ClientConnection.GetRemotePort());
_Buffer.Length = 0;
while ((!_Stop) && (_ClientConnection.Connected))
{
if (_ClientConnection.CanRead(1000))
{
ParseClientCommands(_ClientConnection.ReadString());
}
}
}
if (_ClientConnection.Connected) _ClientConnection.Close();
}
else
{
RaiseErrorMessageEvent("IPCSocketServer failed to accept a client connection");
}
}
catch (Exception ex)
{
RaiseExceptionEvent("Error in IPCSocketServerThread::Execute()", ex);
}
}
}
}
public static IPCSocketServerThread GetNewServer(string localAddress, int localPort)
{
TcpConnection ServerConnection = new TcpConnection();
if (ServerConnection.Listen(localAddress, localPort))
{
IPCSocketServerThread Result = new IPCSocketServerThread(ServerConnection);
Result.Start();
return Result;
}
else
{
return null;
}
}
private void ParseClientCommands(string data)
{
for (int i = 0; i < data.Length; i++)
{
if (data[i] == IPCSocketServerThread.EndStatement)
{
RaiseClientCommandEvent(_Buffer.ToString());
_Buffer.Length = 0;
}
else
{
_Buffer.Append(data[i]);
}
}
}
private void RaiseClientCommandEvent(string command)
{
EventHandler<StringEventArgs> Handler = ClientCommandEvent;
if (Handler != null) Handler(this, new StringEventArgs(command));
}
private void RaiseErrorMessageEvent(string message)
{
EventHandler<StringEventArgs> Handler = ErrorMessageEvent;
if (Handler != null) Handler(this, new StringEventArgs(message));
}
private void RaiseExceptionEvent(string message, Exception exception)
{
EventHandler<ExceptionEventArgs> Handler = ExceptionEvent;
if (Handler != null) Handler(this, new ExceptionEventArgs(message, exception));
}
private void RaiseMessageEvent(string message)
{
EventHandler<StringEventArgs> Handler = MessageEvent;
if (Handler != null) Handler(this, new StringEventArgs(message));
}
public void SendMessage(string command)
{
_ClientConnection.Write(command.Replace(IPCSocketServerThread.EndStatement.ToString(), "") + IPCSocketServerThread.EndStatement);
}
}
}