//处理机器消息 public byte[] HandleHexByte(byte[] byteInfo, AsyncSocketUserToken m_asyncSocketUserToken, AsyncSocketServer m_asyncSocketServer) { //Program.Logger.InfoFormat("receive message is {0}, machine id is {1}", ByteHelper.byteToHexStr(byteInfo), m_asyncSocketUserToken.MachineId); //包头 string infoHead = byteInfo[0].ToString(); //保活 if (infoHead == "57") { //Program.Logger.InfoFormat("machine id is {0}", m_asyncSocketUserToken.MachineId); if (string.IsNullOrEmpty(m_asyncSocketUserToken.MachineId) || !MachineHelper.IsOnline(m_asyncSocketUserToken.MachineId)) { return(new byte[0]); } return(byteInfo); } if (infoHead == "72") { //大小 int infoSize = int.Parse(ByteHelper.GenerateRealityData(byteInfo.Skip(1).Take(2).ToArray(), "intval")); byte[] deencryData = byteInfo;//解密算法 //验证码 string infoVerify = byteInfo[3].ToString(); //数据 byte[] data = ByteHelper.Deencryption(infoSize, byteInfo.Skip(4).Take(infoSize).ToArray()); //验证是否为有效包 if (!IsValidPackage(infoVerify, data)) { return(new byte[0]); } //不签到不回复 string commandStr = ByteHelper.Ten2Hex(data[0].ToString()).ToUpper(); if (commandStr != "41" && commandStr != "30") { if (string.IsNullOrEmpty(m_asyncSocketUserToken.MachineId)) { return(new byte[0]); } } try { //处理机器上传指令的逻辑 return(new MachinePush().PushLogic(commandStr, byteInfo, data, m_asyncSocketUserToken)); } catch (Exception e) { //MessageBox.Show(e.Message); return(new byte[0]); } } else if (infoHead == "73") //推送 { int sendLength = byteInfo.Length - 2; string ip = string.Empty; byte[] sendInfo = new byte[byteInfo.Length]; byteInfo.CopyTo(sendInfo, 0); int size73 = int.Parse(ByteHelper.GenerateRealityData(byteInfo.Skip(3).Take(2).ToArray(), "intval")); ByteHelper.Deencryption(size73, byteInfo.Skip(6).Take(size73).ToArray()).CopyTo(byteInfo, 6); string machineId10 = ByteHelper.GenerateRealityData(byteInfo.Skip(7).Take(12).ToArray(), "stringval"); ip = MachineHelper.GetIp(machineId10); if (sendLength > 0 && !string.IsNullOrEmpty(ip)) { sendToTerminal(m_asyncSocketServer, machineId10, ip, sendInfo, sendLength, 3); /* * SetTimeout(5000, delegate { * Program.Logger.InfoFormat("loop machineId is {0}, loop ip is {1}, message is {2}", machineId10, ip, ByteHelper.byteToHexStr(sendInfo)); * sendToTerminal(m_asyncSocketServer,machineId10,ip, sendInfo, sendLength, 1); * }, machineId10, byteInfo); */ } //x[0].co return(new byte[0]); } else { try { string ipAndMessage = ByteHelper.GenerateRealityData(byteInfo, "stringval"); string ip = ipAndMessage.Split("~")[0]; byte[] sendByte = ByteHelper.strToToHexByte(ipAndMessage.Split("~")[1]); AsyncSocketUserToken[] list = null; m_asyncSocketServer.AsyncSocketUserTokenList.CopyList(ref list); for (int i = 0; i < list.Length; i++) { if (list[i].ConnectSocket.RemoteEndPoint.ToString() == ip) { list[i].SendEventArgs.SetBuffer(sendByte, 0, sendByte.Length); bool willRaiseEvent = list[i].ConnectSocket.SendAsync(list[i].SendEventArgs); break; } } } catch { //Program.Logger.InfoFormat("the first by sent was {0}", byteInfo[0]); return(new byte[0]); } return(new byte[0]); } }
public ThroughputSocketProtocol(AsyncSocketServer asyncSocketServer, AsyncSocketUserToken asyncSocketUserToken) : base(asyncSocketServer, asyncSocketUserToken) { m_socketFlag = "Throughput"; }
static void Main(string[] args) { try { DateTime currentTime = DateTime.Now; //log4net.GlobalContext.Properties["LogDir"] = currentTime.ToString("yyyyMM"); //log4net.GlobalContext.Properties["LogFileName"] = "_SocketAsyncServer" + currentTime.ToString("yyyyMMdd"); ILoggerRepository repository = LogManager.CreateRepository("Fycn.Sockets"); XmlConfigurator.Configure(repository, new FileInfo("log4net.config")); Logger = log4net.LogManager.GetLogger(repository.Name, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); FileDirectory = ConfigurationManager.AppSettings["FileDirectory"]; if (FileDirectory == "") { FileDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Files"); } if (!Directory.Exists(FileDirectory)) { Directory.CreateDirectory(FileDirectory); } int port = 0; if (!(int.TryParse(ConfigurationManager.AppSettings["Port"], out port))) { port = 9999; } int parallelNum = 0; if (!(int.TryParse(ConfigurationManager.AppSettings["ParallelNum"], out parallelNum))) { parallelNum = 8000; } int socketTimeOutMS = 0; if (!(int.TryParse(ConfigurationManager.AppSettings["SocketTimeOutMS"], out socketTimeOutMS))) { socketTimeOutMS = 5 * 60 * 1000; } AsyncSocketSvr = new AsyncSocketServer(parallelNum); AsyncSocketSvr.SocketTimeOutMS = socketTimeOutMS; AsyncSocketSvr.Init(); string HostName = Dns.GetHostName(); //得到主机名 IPHostEntry IpEntry = Dns.GetHostEntry(HostName); string ipV4 = "0.0.0.0"; /* * for (int i = 0; i < IpEntry.AddressList.Length; i++) * { * //从IP地址列表中筛选出IPv4类型的IP地址 * //AddressFamily.InterNetwork表示此IP为IPv4, * //AddressFamily.InterNetworkV6表示此地址为IPv6类型 * if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork) * { * ipV4 = IpEntry.AddressList[i].ToString(); * } * } */ IPEndPoint listenPoint = new IPEndPoint(IPAddress.Parse(ipV4), port); AsyncSocketSvr.Start(listenPoint); InitTimer(socketTimeOutMS); Console.WriteLine("Press any key to terminate the server process...."); Console.Read(); } catch (Exception e) { Console.WriteLine(e.Message); } }