110,534
社区成员
发帖
与我相关
我的任务
分享
#region void AcceptLink() // 接受请求线程体
/// <summary>
/// 接受请求线程体
/// </summary>
void AcceptLink()
{
Listener.Start();
while (!ThreadStop)
{
TcpClient Client = null;
NetworkStream Stream = null;
Byte[] ReadBytes = new Byte[8];
Byte[] WriteBytes = new Byte[2];
try
{
Client = Listener.AcceptTcpClient();
IPAddress ClientIPAddress = ((IPEndPoint)Client.Client.RemoteEndPoint).Address;
int AcceptClientIPAddressIndex = IPAddresses.IndexOf(ClientIPAddress);
Stream = Client.GetStream();
Stream.Read(ReadBytes, 0, 8);
String Command = Encoding.Default.GetString(ReadBytes);
if (AcceptClientIPAddressIndex == -1 && Command == "A0650001") // 固化 连接
{
WriteBytes[0] = 48;
WriteBytes[1] = 49;
Stream.Write(WriteBytes, 0, 2);
IPAddresses.Add(ClientIPAddress); // 完全成功后生效
}
else if (AcceptClientIPAddressIndex >= 0 && Command == "A0650002") // 固化 服务关闭
{
IPAddresses.RemoveAt(AcceptClientIPAddressIndex); // 立即失效
WriteBytes[0] = 48;
WriteBytes[1] = 49;
Stream.Write(WriteBytes, 0, 2);
}
else if (AcceptClientIPAddressIndex == -1 && Command == "A0650003") // 固化 连接测试
{
WriteBytes[0] = 48;
WriteBytes[1] = 49;
Stream.Write(WriteBytes, 0, 2);
IPAddresses.Add(ClientIPAddress); // 完全成功后生效
}
else if (AcceptClientIPAddressIndex >= 0 && Command == "0000000A") // 固化 服务开始
{
System.ServiceProcess.ServiceController sc = new ServiceController();
sc.ServiceName = Consts.SERVICE_NAME;
sc.Start();
sc.WaitForStatus(ServiceControllerStatus.Running);
WriteBytes[0] = 48;
WriteBytes[1] = 49;
Stream.Write(WriteBytes, 0, 2);
}
else if (AcceptClientIPAddressIndex >= 0 && Command == "0000000B") // 固化 服务停止
{
System.ServiceProcess.ServiceController sc = new ServiceController();
sc.ServiceName = Consts.SERVICE_NAME;
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped);
WriteBytes[0] = 48;
WriteBytes[1] = 49;
Stream.Write(WriteBytes, 0, 2);
}
else if (AcceptClientIPAddressIndex >= 0 && Command == "0000000C") // 固化 获取服务状态
{
System.ServiceProcess.ServiceController sc = new ServiceController();
sc.ServiceName = Consts.SERVICE_NAME;
if (sc.Status == ServiceControllerStatus.StopPending)
{
WriteBytes[0] = 48;
WriteBytes[1] = 49;
}
else if (sc.Status == ServiceControllerStatus.Stopped)
{
WriteBytes[0] = 48;
WriteBytes[1] = 50;
}
else if (sc.Status == ServiceControllerStatus.StartPending)
{
WriteBytes[0] = 48;
WriteBytes[1] = 51;
}
else if (sc.Status == ServiceControllerStatus.Running)
{
WriteBytes[0] = 48;
WriteBytes[1] = 52;
}
Stream.Write(WriteBytes, 0, 2);
}
else if (AcceptClientIPAddressIndex >= 0 && Command == "0000000D") // 固化 本地化分析
{
WriteBytes[0] = 48;
WriteBytes[1] = 48;
Stream.Write(WriteBytes, 0, 2);
Byte[] ReadInfoBytes = new Byte[5000000];
Stream.Read(ReadInfoBytes, 0,5000000);
MemoryStream MS = new MemoryStream(ReadInfoBytes);
IFormatter BF = new BinaryFormatter();
Params StartupParams = (Params)BF.Deserialize(MS);
ProcessEntry PE = new ProcessEntry();
PE.Main(StartupParams);
MS = new MemoryStream();
BF.Serialize(MS, StartupParams);
Byte[] WriteInfoBytes = MS.ToArray();
Stream.Write(WriteInfoBytes, 0, WriteInfoBytes.Length);
}
Stream.Close();
Stream = null;
}
catch (Exception e)
{
try
{
EventLog.WriteEntry(Consts.SERVICE_CONTROL_NAME, e.Message, EventLogEntryType.Information);
if (Client != null && Stream != null)
{
WriteBytes[0] = 49;
WriteBytes[1] = 49;
Stream.Write(WriteBytes, 0, 2);
}
}
catch
{
// 如果返回错误代码也发生了异常则忽略异常,以保证服务不会因网络问题等而导致崩溃后停止
}
}
finally
{
try
{
if (Stream != null)
{
Stream.Close();
Stream = null;
}
if (Client != null)
{
Client.Close();
Client = null;
}
}
catch
{
// 如果关闭连接也发生了异常则忽略异常,以保证服务不会因网络问题等而导致崩溃后停止
}
}
}
}
#endregion