コード例 #1
0
ファイル: ScadaTCPMsg.cs プロジェクト: nxzzkj/lazyiot
 public ScadaTCPMsg(ScadaTCPHeader header, ScadaTCPBody body)
 {
     this.header = header;
     this.body   = body;
     this.body.Encode();
     bodyBytes         = this.body.GetBytes();
     header.bodyLength = bodyBytes.Length;
     this.header.Encode();
     headerBytes = this.header.GetBytes();
     MsgBytes    = new byte[headerBytes.Length + bodyBytes.Length];
     headerBytes.CopyTo(MsgBytes, 0);
     bodyBytes.CopyTo(MsgBytes, headerBytes.Length);
 }
コード例 #2
0
ファイル: ScadaTCPAsyncServer.cs プロジェクト: nxzzkj/lazyiot
        public void ReadCallback(IAsyncResult ar)
        {
            // Retrieve the state object and the handler socket
            // from the asynchronous state object.
            ReadState readState = (ReadState)ar.AsyncState;

            try
            {
                Socket handler = readState.workSocket;

                // Read data from the client socket.
                int            bytesRead = handler.EndReceive(ar);
                ScadaTCPHeader header    = readState.header;

                if (bytesRead > 0)
                {
                    if (PrintReceiveHex)
                    {
                        PrintUtils.PrintHex(readState.BodyBytes);
                    }

                    ScadaTCPBody body = header.InstanceBody();
                    body.BodyBytes = readState.BodyBytes;
                    body.Decode();
                    body.Debug();
                    body.Info();

                    MainNotify?.Invoke(header, body);
                    ScadaTCPMsg sendMsg = body.GetSendMsg();
                    if (sendMsg != null)
                    {
                        Send(header.SessionId, sendMsg, sendMsg.CloseClient);
                    }

                    ResetState(handler, header);
                }
            }
            catch (Exception e)
            {
                ReadException(e, readState);
            }
        }