/// <summary> /// 处理推送数据返回结果 /// </summary> /// <param name="state"></param> private void ProcessRequestForPush(StateObject state) { try { if (state.Package.ST == 0) { //支持推送数据 state.IsSupported = true; state.ResultBytes.Clear(); //开始接受数据 ReceiveHead(state); } else { //登录失败,关闭此次会话 var package = SocketHelper.BytesToStruct <ErrorPackage>(state.ResultBytes.ToArray()); package.ERR = SocketHelper.NetworkToHost(package.ERR); package.RV = SocketHelper.NetworkToHost(package.RV); state.IsSupported = false; _asyncSocketServer.CloseAndDisposeConnection(state, new Exception("不支持推送数据,错误代码为" + package.ERR), ErrorLevel.Level1); } } catch (SocketException e) { _asyncSocketServer.CloseAndDisposeConnection(state, e, ErrorLevel.Level1); } catch (Exception e) { _asyncSocketServer.CloseAndDisposeConnection(state, e, ErrorLevel.Level1); } }
/// <summary> /// 处理接收到的数据项 /// </summary> /// <param name="state"></param> private void ProcessPushData(StateObject state) { try { // All the data has arrived; put it in response. var package = SocketHelper.BytesToStruct <PushHeadPackage>(state.ResultBytes.ToArray()); package.Count = SocketHelper.NetworkToHost(package.Count); _logManager.Info($"{MessageType.ResponseDataItem}{state.Machine.MachineCode} => 推送数据项{package.Count}个"); state.CollectDataCount = package.Count; state.ResultBytes.Clear(); if (package.Count == 0) { state.Package.CID = FuncCode.PushDataReply; Send(state); } else { state.WorkSocket.BeginReceive(state.Buffer, 0, 8, 0, ReceiveRecordHeadCallback, state); } } catch (SocketException e) { _asyncSocketServer.CloseAndDisposeConnection(state, e, ErrorLevel.Level1); } catch (Exception e) { _asyncSocketServer.CloseAndDisposeConnection(state, e, ErrorLevel.Level1); } }
/// <summary> /// 处理登录返回信息 /// </summary> /// <param name="state"></param> private void ProcessLogin(StateObject state) { try { if (state.Package.ST == 0) { //asyncSocketServer.RemoveDisconnectedSession(state); _logManager.Info($"{MessageType.Login}{state.Machine.MachineCode} => 登录成功"); state.IsLogined = true; state.ResultBytes.Clear(); state.Package.CID = FuncCode.EnumAllData; Send(state); } else { //登录失败,关闭此次会话 _logManager.Info($"{MessageType.Login}{state.Machine.MachineCode} => 登录失败,关闭此次会话"); LoginResultPackage package = SocketHelper.BytesToStruct <LoginResultPackage>(state.ResultBytes.ToArray()); package.ERR = SocketHelper.NetworkToHost(package.ERR); package.RV = SocketHelper.NetworkToHost(package.RV); state.IsLogined = false; } } catch (SocketException e) { _asyncSocketServer.CloseAndDisposeConnection(state, e, ErrorLevel.Level1); } catch (Exception e) { _asyncSocketServer.CloseAndDisposeConnection(state, e, ErrorLevel.Level1); } }
/// <summary> /// 解析枚举数据明细记录 /// </summary> /// <param name="ar"></param> private void ReceiveEnumDataCallback(IAsyncResult ar) { var state = (StateObject)ar.AsyncState; try { if (state == null || !state.WorkSocket.Connected) { return; } var bytesRead = state.WorkSocket.EndReceive(ar); if (bytesRead <= 0) { return; } state.ResultBytes = state.ResultBytes.Concat(state.Buffer.Take(bytesRead).ToArray()).ToList(); if (state.ResultBytes.Count < PackgeSize.EnumDataPackageSize) { state.WorkSocket.BeginReceive(state.Buffer, 0, PackgeSize.EnumDataPackageSize - state.ResultBytes.Count, 0, ReceiveEnumDataCallback, state); } else { var package = SocketHelper.BytesToStruct <EnumDataRecordPackage>(state.ResultBytes.ToArray()); package.DID = SocketHelper.NetworkToHost(package.DID); _logManager.Info($"{MessageType.ResponseDataItem}{state.Machine.MachineCode} => 枚举数据项编号{package.DID},内容为{package.NAME}"); state.Machine.Map.MapArray.Add(new MapArray() { Id = package.DID.ToString(), Value = package.NAME }); state.InsertedCount++; state.ResultBytes.Clear(); if (state.InsertedCount < state.MappedFieldCount) { state.WorkSocket.BeginReceive(state.Buffer, 0, PackgeSize.EnumDataPackageSize, 0, ReceiveEnumDataCallback, state); } else { state.DbHelper.InsertMappedFiled(state.Machine); state.InsertedCount = 0; state.Package.CID = FuncCode.RequestForPush; Send(state); } } } catch (SocketException e) { _asyncSocketServer.CloseAndDisposeConnection(state, e, ErrorLevel.Level1); } catch (Exception e) { _asyncSocketServer.CloseAndDisposeConnection(state, e, ErrorLevel.Level1); } }
private void ReceiveHeadCallback(IAsyncResult ar) { var state = (StateObject)ar.AsyncState; state.ActiveDateTime = DateTime.Now; try { if (!state.WorkSocket.Connected) { return; } var bytesRead = state.WorkSocket.EndReceive(ar); if (bytesRead <= 0) { return; } state.ResultBytes = state.ResultBytes.Concat(state.Buffer.Take(bytesRead).ToArray()).ToList(); if (state.ResultBytes.Count < 8) { state.WorkSocket.BeginReceive(state.Buffer, 0, 8 - state.ResultBytes.Count, 0, ReceiveHeadCallback, state); } else { _logManager.Info($"{MessageType.Receive} => {PrintReceivebytes(state.ResultBytes.ToArray())}"); var package = SocketHelper.BytesToStruct <BasicPackage>(state.ResultBytes.ToArray()); package.PID = SocketHelper.NetworkToHost(package.PID); package.CID = SocketHelper.NetworkToHost(package.CID); package.VER = SocketHelper.NetworkToHost(package.VER); if (package.VER == ALPVERSION) { state.Package = package; state.ResultBytes.Clear(); //解析命令 ParseCommand(state); } else { _asyncSocketServer.CloseAndDisposeConnection(state, new Exception($"协议版本错误,当前版为:{package.VER},需求版本为:{ALPVERSION}"), ErrorLevel.Level1); } } } catch (SocketException e) { _asyncSocketServer.CloseAndDisposeConnection(state, e, ErrorLevel.Level1); } catch (Exception e) { _asyncSocketServer.CloseAndDisposeConnection(state, e, ErrorLevel.Level1); } }
private void ReceiveRecordHeadCallback(IAsyncResult ar) { var state = (StateObject)ar.AsyncState; try { if (state == null || !state.WorkSocket.Connected) { return; } var bytesRead = state.WorkSocket.EndReceive(ar); if (bytesRead <= 0) { return; } state.ResultBytes = state.ResultBytes.Concat(state.Buffer.Take(bytesRead).ToArray()).ToList(); if (state.ResultBytes.Count < 8) { state.WorkSocket.BeginReceive(state.Buffer, 0, 8 - state.ResultBytes.Count, 0, ReceiveRecordHeadCallback, state); } else { var package = SocketHelper.BytesToStruct <PushBodyPackage>(state.ResultBytes.Take(8).ToArray()); package.DID = SocketHelper.NetworkToHost(package.DID); package.DLEN = SocketHelper.NetworkToHost(package.DLEN); state.Did = package.DID; state.Length = package.DLEN; state.Dt = package.DT; state.ResultBytes.Clear(); state.WorkSocket.BeginReceive(state.Buffer, 0, package.DLEN, 0, ReceiveRecordBodyCallback, state); } } catch (SocketException e) { _asyncSocketServer.CloseAndDisposeConnection(state, e, ErrorLevel.Level1); } catch (Exception e) { _asyncSocketServer.CloseAndDisposeConnection(state, e, ErrorLevel.Level1); } }
/// <summary> /// 处理枚举数据项返回结果 /// </summary> /// <param name="state"></param> private void ProcessEnumAllData(StateObject state) { try { _logManager.Info($"{MessageType.Info}{state.Machine.MachineCode} => " + PrintReceivebytes(state.ResultBytes.ToArray())); EnumPackage package = SocketHelper.BytesToStruct <EnumPackage>(state.ResultBytes.ToArray()); _logManager.Info($"{MessageType.Info}{state.Machine.MachineCode} => 枚举数据项{package.Count}个"); package.Count = SocketHelper.NetworkToHost(package.Count); _logManager.Info($"{MessageType.Info}{state.Machine.MachineCode} => 枚举数据项{package.Count}个"); state.ResultBytes.Clear(); //如果机器没有枚举数据项则直接发送推送数据请求 if (package.Count == 0) { //发送推送数据请求 state.Package.CID = FuncCode.RequestForPush; Send(state); } else { state.MappedFieldCount = package.Count; state.Size = PackgeSize.EnumDataPackageSize * state.MappedFieldCount; if (state.Size > 1024 * 10) { _logManager.Info($"{MessageType.Error}{state.Machine.MachineCode} => 非法数据包(大小{state.Size})"); _asyncSocketServer.CloseAndDisposeConnection(state, new Exception("非法数据包,大小为:" + state.Size + ""), ErrorLevel.Level1); } else { state.WorkSocket.BeginReceive(state.Buffer, 0, PackgeSize.EnumDataPackageSize, 0, ReceiveEnumDataCallback, state); } } } catch (SocketException e) { _asyncSocketServer.CloseAndDisposeConnection(state, e, ErrorLevel.Level1); } catch (Exception e) { _asyncSocketServer.CloseAndDisposeConnection(state, e, ErrorLevel.Level1); } }
/// <summary> /// 处理设备自举 /// </summary> /// <param name="state"></param> private void ProcessBootstrap(StateObject state) { try { _logManager.Info($"{MessageType.Receive} => {PrintReceivebytes(state.ResultBytes.ToArray())}"); var package = SocketHelper.BytesToStruct <MacBootstrapPackage>(state.ResultBytes.ToArray()); //根据自举得设备Id获取数据库里存储的登录用户名和密码 state.Machine.MachineCode = Encoding.ASCII.GetString(package.DID); GetMacLoginInfo(state); _logManager.Info($"{MessageType.Info}处理{state.Machine.MachineCode}自举数据"); state.ResultBytes.Clear(); //发送登录包 state.Package.CID = FuncCode.Login; Send(state); } catch (SocketException e) { _asyncSocketServer.CloseAndDisposeConnection(state, e, ErrorLevel.Level1); } catch (Exception e) { _asyncSocketServer.CloseAndDisposeConnection(state, e, ErrorLevel.Level1); } }