Esempio n. 1
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);
        }
Esempio n. 2
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);
     }
 }
Esempio n. 3
0
        public void AcceptCallback(IAsyncResult ar)
        {
            // Create the state object.
            ReadState readState = new ReadState();

            try
            {
                // Signal the main thread to continue.
                allDone.Set();

                // Get the socket that handles the client request.
                Socket listener = (Socket)ar.AsyncState;
                Socket handler  = listener.EndAccept(ar);

                string sessionId = ScadaTCPClients.AssignSessionId();
                LOG.InfoFormat("({2}) New client:{0},sessionId:{1}", handler.RemoteEndPoint, sessionId, Name);

                ScadaClient client = new ScadaClient();
                client.socket    = handler;
                client.SessionId = sessionId;
                ScadaTCPClients.Add(client.SessionId, client);
                LOG.InfoFormat("({1}) Clients count:{0}", ScadaTCPClients.ClientCount, Name);

                readState.workSocket = handler;
                readState.sessionId  = client.SessionId;

                readState.HeaderBytes = new byte[HeaderLength];
                handler.BeginReceive(readState.HeaderBytes, 0, HeaderLength, SocketFlags.None, new AsyncCallback(ReadHeadCallback), readState);
            }
            catch (Exception e)
            {
                ReadException(e, readState);
            }
        }
Esempio n. 4
0
        private void SendCallback(IAsyncResult ar)
        {
            // Retrieve the socket from the state object.
            SendState handler = (SendState)ar.AsyncState;

            try
            {
                Socket socket = handler.RemoteSocket;
                // Complete sending the data to the remote device.
                int bytesSent = socket.EndSend(ar);
                LOG.DebugFormat("({2}) Sent {0} bytes to {1} finish!", bytesSent, socket.RemoteEndPoint, Name);
                SendFinish?.Invoke(handler);
                if (handler.CloseClient)
                {
                    ScadaTCPClients.Close(handler.Client.SessionId);
                    LOG.InfoFormat("({1}) Client :{0} closed!", handler.Client.SessionId, Name);
                }
            }
            catch (Exception e)
            {
                WriteException(e, handler);
            }
        }