private void process() { var ns = client.GetStream(); while (client.Connected && Running) { try { if (ns.DataAvailable) { byte[] dl = new byte[4]; ns.Read(dl, 0, dl.Length); byte[] d = new byte[dl.ToInt()]; ns.Read(d, 0, d.Length); var request = d.ToObject <RequestMessage>(); ICommandProcess cmdProcess = null; if (insideCommand.HasCmd(request.Cmd)) { cmdProcess = insideCommand[request.Cmd]; } else { cmdProcess = config.Commands[request.Cmd].Processor; } if (cmdProcess != null) { var response = cmdProcess.Execute(request); var data = response.ToBytes(); var datal = data.Length.GetBytes(); ns.Write(datal, 0, datal.Length); ns.Write(data, 0, data.Length); } else { Logger.WriteLog(LogType.INFO, "没有找到命令为" + request.Cmd + "的处理器"); } } else { Thread.Sleep(10); } } catch (ThreadAbortException) { Logger.WriteLog(LogType.INFO, "服务器关闭,连接停止"); } catch (Exception ex) { Logger.WriteLog(LogType.ERROR, "接受数据时出错:", ex); } } Logger.WriteLog(LogType.INFO, "连接关闭"); ns.Close(); client.Close(); }
public void Register(string cmd, ICommandProcess process) { dic.Add(cmd, process); }