private void ReceiveCallback(IAsyncResult ar) { try { _lastTime = DateTime.Now; var ts = (Socket)ar.AsyncState; var rEnd = ts.EndReceive(ar); if (rEnd > 0) { var partBytes = new byte[rEnd]; Array.Copy(_mBuffer, 0, partBytes, 0, rEnd); //在此次可以对data进行按需处理 var mHeader = Encoding.UTF8.GetString(partBytes.Take(2).ToArray()); if (mHeader == SocketBufferReader.Header) { _reader = new SocketBufferReader(); partBytes = partBytes.Skip(2).ToArray(); _reader.MDataLength = BitConverter.ToInt32(partBytes.Take(4).ToArray(), 0); partBytes = partBytes.Skip(4).ToArray(); } if (_reader != null) { _reader.SocketBufferReaderAdd(partBytes); if (_reader.IsValid) { var data = _reader.ReadString(); Task.Run(() => { var msg = JsonConvert.DeserializeObject <NpcSocketMsg>(data); if (!_ignoreList.Contains(msg.MsgType)) { Send(new NpcSocketMsg { Guid = msg.Guid, MsgType = NpcSocketMsgType.ReceiveSuccess }); //Console.WriteLine($"{DateTime.Now} {msg.MsgType} -----------Receive Done"); } NpcSocketMsg responseMsg = null; List <DeviceInfo> devicesList; var dataErrResult = new DataErrResult(); switch (msg.MsgType) { case NpcSocketMsgType.Heart: //responseMsg = new NpcSocketMsg //{ // MsgType = NpcSocketMsgType.HeartSuccess, //}; break; case NpcSocketMsgType.HeartSuccess: //Console.WriteLine("连接正常"); if (_isNormal == false) { _isNormal = true; Log.Info("连接Gate正常"); } break; case NpcSocketMsgType.List: //设备列表 responseMsg = new NpcSocketMsg { MsgType = msg.MsgType, Body = ClientManager.GetDevices().ToJSON() }; break; case NpcSocketMsgType.Add: devicesList = JsonConvert.DeserializeObject <List <DeviceInfo> >(msg.Body); dataErrResult.datas.AddRange(ClientManager.AddClient(devicesList)); responseMsg = new NpcSocketMsg { Guid = msg.Guid, MsgType = msg.MsgType, Body = dataErrResult.ToJSON() }; break; case NpcSocketMsgType.Delete: devicesList = JsonConvert.DeserializeObject <List <DeviceInfo> >(msg.Body); dataErrResult.datas.AddRange(ClientManager.DelClient(devicesList)); responseMsg = new NpcSocketMsg { Guid = msg.Guid, MsgType = msg.MsgType, Body = dataErrResult.ToJSON() }; break; case NpcSocketMsgType.Update: devicesList = JsonConvert.DeserializeObject <List <DeviceInfo> >(msg.Body); dataErrResult.datas.AddRange(ClientManager.UpdateClient(devicesList)); responseMsg = new NpcSocketMsg { Guid = msg.Guid, MsgType = msg.MsgType, Body = dataErrResult.ToJSON() }; break; case NpcSocketMsgType.Storage: devicesList = JsonConvert.DeserializeObject <List <DeviceInfo> >(msg.Body); dataErrResult.datas.AddRange(ClientManager.SetStorage(devicesList)); responseMsg = new NpcSocketMsg { Guid = msg.Guid, MsgType = msg.MsgType, Body = dataErrResult.ToJSON() }; break; case NpcSocketMsgType.Frequency: devicesList = JsonConvert.DeserializeObject <List <DeviceInfo> >(msg.Body); dataErrResult.datas.AddRange(ClientManager.SetFrequency(devicesList)); responseMsg = new NpcSocketMsg { Guid = msg.Guid, MsgType = msg.MsgType, Body = dataErrResult.ToJSON() }; break; case NpcSocketMsgType.SendBack: devicesList = JsonConvert.DeserializeObject <List <DeviceInfo> >(msg.Body); var messageResult = new MessageResult(); messageResult.messages.AddRange(ClientManager.SendMessageBack(devicesList)); responseMsg = new NpcSocketMsg { Guid = msg.Guid, MsgType = msg.MsgType, Body = messageResult.ToJSON() }; break; case NpcSocketMsgType.UpgradeClient: var upgradeInfos = JsonConvert.DeserializeObject <UpgradeInfos>(msg.Body); Console.WriteLine($"{DateTime.Now} {msg.MsgType} {upgradeInfos.Infos.Select(x => x.DeviceId).ToJSON()} -----------{ServerId} Deal"); switch (upgradeInfos.Type) { case 1: dataErrResult.datas.AddRange(ClientManager.UpgradeScript(upgradeInfos.Infos)); break; case 2: dataErrResult.datas.AddRange(ClientManager.UpgradeFirmware(upgradeInfos.Infos)); break; case 3: dataErrResult.datas.AddRange(upgradeInfos.Infos.Select(x => new DeviceErr(x.DeviceId, Error.ParamError))); break; default: dataErrResult.datas.AddRange(upgradeInfos.Infos.Select(x => new DeviceErr(x.DeviceId, Error.ParamError))); break; } responseMsg = new NpcSocketMsg { Guid = msg.Guid, MsgType = NpcSocketMsgType.UpgradeClientBack, Body = dataErrResult.ToJSON() }; break; default: break; } if (responseMsg != null) { //Console.WriteLine($"{DateTime.Now} {msg.MsgType} -----------send done"); Send(responseMsg); } }); _reader = null; } ReceiveData(); //_socket.BeginReceive(_mBuffer, 0, _mBuffer.Length, 0, new AsyncCallback(ReceiveCallback), null); } } } catch (Exception ex) { Log.Error($"ReceiveCallback Error, {ex.Message}, {ex.StackTrace}"); _isNormal = false; SocketState = SocketState.Close; } }
private void ReceiveCallback(IAsyncResult ar) { try { var ts = (Socket)ar.AsyncState; var rEnd = ts.EndReceive(ar); if (rEnd > 0) { var partBytes = new byte[rEnd]; Array.Copy(_mBuffer, 0, partBytes, 0, rEnd); //在此次可以对data进行按需处理 var mHeader = Encoding.UTF8.GetString(partBytes.Take(2).ToArray()); if (mHeader == SocketBufferReader.Header) { _reader = new SocketBufferReader(); partBytes = partBytes.Skip(2).ToArray(); _reader.MDataLength = BitConverter.ToInt32(partBytes.Take(4).ToArray(), 0); partBytes = partBytes.Skip(4).ToArray(); } if (_reader != null) { _reader.SocketBufferReaderAdd(partBytes); if (_reader.IsValid) { var data = _reader.ReadString(); Task.Run(() => { var msg = JsonConvert.DeserializeObject <NpcSocketMsg>(data); if (!_ignoreList.Contains(msg.MsgType)) { Console.WriteLine($"{DateTime.Now} {msg.Guid} {msg.MsgType} -----------{ServerId} Receive"); } NpcSocketMsg responseMsg = null; switch (msg.MsgType) { case NpcSocketMsgType.ReceiveSuccess: if (_msgQueue.TryPeek(out var firstMsg) && firstMsg.Guid == msg.Guid) { _msgQueue.TryDequeue(out _); } break; case NpcSocketMsgType.Heart: responseMsg = new NpcSocketMsg { MsgType = NpcSocketMsgType.HeartSuccess, }; break; case NpcSocketMsgType.HeartSuccess: //Console.WriteLine("连接正常"); break; case NpcSocketMsgType.List: LastReceiveTime = DateTime.Now; DeviceInfos = JsonConvert.DeserializeObject <List <DeviceInfo> >(msg.Body); if (DeviceInfos.Any()) { ServerId = DeviceInfos.First().ServerId; } break; //case NpcSocketMsgType.Add: //case NpcSocketMsgType.Delete: //case NpcSocketMsgType.Storage: //case NpcSocketMsgType.Frequency: //case NpcSocketMsgType.SendBack: // break; default: ServerManager.NpcSocketMsgs.TryAdd(msg.Guid, msg); break; } if (responseMsg != null) { if (!_ignoreList.Contains(msg.MsgType)) { Console.WriteLine($"{DateTime.Now} {msg.Guid} {msg.MsgType} -----------{ServerId} Send"); } Send(responseMsg); } }); _reader = null; } ReceiveData(); //Socket.BeginReceive(_mBuffer, 0, _mBuffer.Length, 0, new AsyncCallback(ReceiveCallback), null); } } } catch (Exception ex) { Log.Error($"ReceiveCallback Error, {ex.Message}, {ex.StackTrace}"); SocketState = SocketState.Close; } }