/// <summary> /// 套接字异步回调 /// </summary> /// <param name="ar"></param> private void SocketAsyncCallBack(IAsyncResult ar) { Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId.ToString("00")} SocketAsyncCallBack"); if (ar.AsyncState is AppSession session) { try { int receiveCount = session.WorkSocket.EndReceive(ar); S7Message s7Message = new S7Message(); OperateResult <byte[]> read1 = ReceiveByMessage(session.WorkSocket, int.MaxValue, s7Message); // 500 时间延长 if (!read1.IsSuccess) { LogNet?.WriteDebug(ToString(), string.Format(StringResources.Language.ClientOfflineInfo, session.IpEndPoint)); RemoveClient(session); return; } ; byte[] receive = read1.Content; if (receive[17] == 0x04) { // 读数据 session.WorkSocket.Send(ReadByMessage(receive)); } else if (receive[17] == 0x05) { // 写数据 session.WorkSocket.Send(WriteByMessage(receive)); } else if (receive[17] == 0x00) { // 请求订货号 session.WorkSocket.Send(SoftBasic.HexStringToBytes("03 00 00 7D 02 F0 80 32 07 00 00 00 01 00 0C 00 60 00 01 12 08 12 84 01 01 00 00 00 00 FF" + " 09 00 5C 00 11 00 00 00 1C 00 03 00 01 36 45 53 37 20 32 31 35 2D 31 41 47 34 30 2D 30 58 42 30 20 00 00 00 06 20 20 00 06 36 45 53 37 20" + " 32 31 35 2D 31 41 47 34 30 2D 30 58 42 30 20 00 00 00 06 20 20 00 07 36 45 53 37 20 32 31 35 2D 31 41 47 34 30 2D 30 58 42 30 20 00 00 56 04 02 01")); } else { session.WorkSocket.Close(); } RaiseDataReceived(receive); session.WorkSocket.BeginReceive(new byte[0], 0, 0, SocketFlags.None, new AsyncCallback(SocketAsyncCallBack), session); } catch { // 关闭连接,记录日志 session.WorkSocket?.Close(); LogNet?.WriteDebug(ToString(), string.Format(StringResources.Language.ClientOfflineInfo, session.IpEndPoint)); RemoveClient(session); return; } } }
private void SocketAsyncCallBack(IAsyncResult ar) { if (ar.AsyncState is AppSession session) { try { int receiveCount = session.WorkSocket.EndReceive(ar); S7Message s7Message = new S7Message( ); OperateResult <S7Message> read1 = ReceiveMessage(session.WorkSocket, 5000, s7Message); if (!read1.IsSuccess) { LogNet?.WriteDebug(ToString( ), string.Format(StringResources.Language.ClientOfflineInfo, session.IpEndPoint)); return; } ; byte[] receive = SoftBasic.SpliceTwoByteArray(read1.Content.HeadBytes, read1.Content.ContentBytes); if (receive[17] == 0x04) { // 读数据 session.WorkSocket.Send(ReadByMessage(receive)); } else if (receive[17] == 0x05) { // 写数据 session.WorkSocket.Send(WriteByMessage(receive)); } else if (receive[17] == 0x00) { // 请求订货号 session.WorkSocket.Send(SoftBasic.HexStringToBytes("03 00 00 7D 02 F0 80 32 07 00 00 00 01 00 0C 00 60 00 01 12 08 12 84 01 01 00 00 00 00 FF" + " 09 00 5C 00 11 00 00 00 1C 00 03 00 01 36 45 53 37 20 32 31 35 2D 31 41 47 34 30 2D 30 58 42 30 20 00 00 00 06 20 20 00 06 36 45 53 37 20" + " 32 31 35 2D 31 41 47 34 30 2D 30 58 42 30 20 00 00 00 06 20 20 00 07 36 45 53 37 20 32 31 35 2D 31 41 47 34 30 2D 30 58 42 30 20 00 00 56 04 02 01")); } else { session.WorkSocket.Close( ); } RaiseDataReceived(receive); session.WorkSocket.BeginReceive(new byte[0], 0, 0, SocketFlags.None, new AsyncCallback(SocketAsyncCallBack), session); } catch { // 关闭连接,记录日志 session.WorkSocket?.Close( ); LogNet?.WriteDebug(ToString( ), string.Format(StringResources.Language.ClientOfflineInfo, session.IpEndPoint)); System.Threading.Interlocked.Decrement(ref onlineCount); RemoveClient(session); return; } } }
/// <summary> /// 当客户端登录后,进行Ip信息的过滤,然后触发本方法,也就是说之后的客户端需要,接收2次的握手协议 /// </summary> /// <param name="socket">网络套接字</param> /// <param name="endPoint">终端节点</param> protected override void ThreadPoolLoginAfterClientCheck(Socket socket, System.Net.IPEndPoint endPoint) { // 接收2次的握手协议 S7Message s7Message = new S7Message(); OperateResult <byte[]> read1 = ReceiveByMessage(socket, int.MaxValue, s7Message);//5000 时间延长,先接收第一条握手协议 Console.WriteLine($"接收:{Communication.BasicFramework.SoftBasic.ByteToHexString(read1.Content, ' ')}"); if (!read1.IsSuccess) { return; } OperateResult send1 = Send(socket, SoftBasic.HexStringToBytes("03 00 00 16 11 D0 00 01 00 0C 00 C0 01 0A C1 02 01 02 C2 02 01 00")); if (!send1.IsSuccess) { return; } OperateResult <byte[]> read2 = ReceiveByMessage(socket, int.MaxValue, s7Message);//5000 时间延长,再接收第二条握手协议 Console.WriteLine($"接收:{Communication.BasicFramework.SoftBasic.ByteToHexString(read2.Content, ' ')}"); if (!read1.IsSuccess) { return; } OperateResult send2 = Send(socket, SoftBasic.HexStringToBytes("03 00 00 1B 02 F0 80 32 03 00 00 04 00 00 08 00 00 00 00 F0 00 00 01 00 01 00 F0")); if (!read2.IsSuccess) { return; } // 开始接收数据信息 AppSession appSession = new AppSession(); appSession.IpEndPoint = endPoint; appSession.WorkSocket = socket; try { socket.BeginReceive(new byte[0], 0, 0, SocketFlags.None, new AsyncCallback(SocketAsyncCallBack), appSession);// 套接字异步回调 AddClient(appSession); } catch { socket.Close(); LogNet?.WriteDebug(ToString(), string.Format(StringResources.Language.ClientOfflineInfo, endPoint)); } }
/// <summary> /// 当客户端登录后,进行Ip信息的过滤,然后触发本方法,也就是说之后的客户端需要 /// </summary> /// <param name="socket">网络套接字</param> /// <param name="endPoint">终端节点</param> protected override void ThreadPoolLoginAfterClientCheck(Socket socket, System.Net.IPEndPoint endPoint) { // 接收2次的握手协议 S7Message s7Message = new S7Message( ); OperateResult <S7Message> read1 = ReceiveMessage(socket, 5000, s7Message); if (!read1.IsSuccess) { return; } OperateResult send1 = Send(socket, SoftBasic.HexStringToBytes("03 00 00 16 11 D0 00 01 00 0C 00 C0 01 0A C1 02 01 02 C2 02 01 00")); if (!send1.IsSuccess) { return; } OperateResult <S7Message> read2 = ReceiveMessage(socket, 5000, s7Message); if (!read1.IsSuccess) { return; } OperateResult send2 = Send(socket, SoftBasic.HexStringToBytes("03 00 00 1B 02 F0 80 32 03 00 00 04 00 00 08 00 00 00 00 F0 00 00 01 00 01 00 F0")); if (!read2.IsSuccess) { return; } // 开始接收数据信息 AppSession appSession = new AppSession( ); appSession.IpEndPoint = endPoint; appSession.WorkSocket = socket; try { socket.BeginReceive(new byte[0], 0, 0, SocketFlags.None, new AsyncCallback(SocketAsyncCallBack), appSession); System.Threading.Interlocked.Increment(ref onlineCount); AddClient(appSession); } catch { socket.Close( ); LogNet?.WriteDebug(ToString( ), string.Format(StringResources.Language.ClientOfflineInfo, endPoint)); } }