Beispiel #1
0
 public void Send(string sessionId, ScadaTCPMsg msg, bool closeClient)
 {
     try
     {
         // Convert the string data to byte data using ASCII encoding.
         //            byte[] byteData = Encoding.ASCII.GetBytes(data);
         if (string.IsNullOrEmpty(sessionId))
         {
             throw new Exception("Session id is null!");
         }
         ScadaClient client = ScadaTCPClients.GetClient(sessionId);
         if (client == null)
         {
             throw new Exception("Not found client by session:" + sessionId);
         }
         LOG.DebugFormat("({2}) Send {0} bytes to {1}", msg.MsgBytes.Length, client.socket.RemoteEndPoint, Name);
         if (PrintSendHex)
         {
             PrintUtils.PrintHex(msg.MsgBytes);
         }
         SendState state = new SendState();
         state.CloseClient  = closeClient;
         state.RemoteSocket = client.socket;
         state.Client       = client;
         state.Msg          = msg;
         // Begin sending the data to the remote device.
         client.socket.BeginSend(msg.MsgBytes, 0, msg.MsgBytes.Length, 0,
                                 new AsyncCallback(SendCallback), state);
     }
     catch (Exception e)
     {
         ReadException(e, null);
     }
 }
Beispiel #2
0
        public void Send(string sessionId, string msg, bool closeClient)
        {
            if (string.IsNullOrEmpty(sessionId))
            {
                throw new Exception("Session id is null!");
            }
            ScadaClient client = ScadaTCPClients.GetClient(sessionId);

            if (client == null)
            {
                throw new Exception("Not found client by session:" + sessionId);
            }
            LOG.DebugFormat("({2}) Send {0} bytes to {1}", msg.Length, client.socket.RemoteEndPoint, Name);
            if (PrintSendHex)
            {
                PrintUtils.PrintHex(msg);
            }
            SendState state = new SendState();

            state.CloseClient  = closeClient;
            state.RemoteSocket = client.socket;
            state.Client       = client;
            state.MsgBits      = Encoding.ASCII.GetBytes(msg);
            // Begin sending the data to the remote device.
            client.socket.BeginSend(state.MsgBits, 0, state.MsgBits.Length, 0,
                                    new AsyncCallback(SendCallback), state);
        }
Beispiel #3
0
        public void ReadHeadCallback(IAsyncResult ar)
        {
            ReadState readState = (ReadState)ar.AsyncState;

            try
            {
                Socket handler = readState.workSocket;
                if (!handler.Connected)
                {
                    LOG.InfoFormat("({0}) Connection closed!", Name);
                    return;
                }
                int bytesRead = handler.EndReceive(ar);
                if (bytesRead > 0)
                {
                    ScadaTCPHeader header = InstanceHeader?.Invoke();
                    if (header == null)
                    {
                        LOG.ErrorFormat("({0}) Not found instance ScadaHeader class", Name);
                        return;
                    }
                    header.bytes        = readState.HeaderBytes;
                    header.RemoteSocket = handler;
                    header.Decode();
                    header.SessionId = readState.sessionId;
                    readState.header = header;
                    header.Debug();
                    header.Info();
                    LOG.InfoFormat("({2}) Read header from:{0},body len:{1}", handler.RemoteEndPoint, header.bodyLength, Name);
                    if (PrintReceiveHex)
                    {
                        PrintUtils.PrintHex(header.bytes);
                    }

                    if (header.bodyLength > 0)
                    {
                        readState.BodyBytes = new byte[header.bodyLength];
                        handler.BeginReceive(readState.BodyBytes, 0, header.bodyLength, SocketFlags.None, new AsyncCallback(ReadCallback), readState);
                    }
                    else
                    {
                        MainNotify?.Invoke(header, null);
                        ResetState(handler, header);
                    }
                }
            }
            catch (Exception e)
            {
                ReadException(e, readState);
            }
        }
Beispiel #4
0
        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);
            }
        }