Пример #1
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            Logger.Debug("ReceiveCallback");

            // Retrieve the state object and the client socket
            // from the asynchronous state object.
            state  = (StateObject)ar.AsyncState;
            client = state.workSocket;

            // Read data from the remote device.
            Logger.Debug("ReceiveCallback-client.EndReceive");

            var bytesReceived = 0;

            try
            {
                bytesReceived = client.EndReceive(ar);

                //Logger.DebugFormat("Received Data String was [{0}] from {1}", receivedData, clientData.Client.RemoteEndPoint);
            }
            catch (Exception exception)
            {
                Logger.ErrorFormat("Receiving data {0} from socket failed from server exception {1} ", response, exception);
            }
            finally
            {
                try
                {
                    client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
                }
                catch (Exception exception)
                {
                    //Server.BeginAccept(AcceptConnection,this);
                    Logger.ErrorFormat("Disconnecting client {0}", exception);
                    client.Close();

                    Reconnect();
                }
            }

            try
            {
                if (bytesReceived > 0)
                {
                    receptionTimer.Stop();
                    Logger.Debug("Timer stopped");

                    // There might be more data, so store the data received so far.
                    state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesReceived));
                    response = state.sb.ToString().Trim();

                    // All the data has arrived; put it in response.
                    if (response.Length > 1 && response.EndsWith("|"))
                    {
                        Logger.DebugFormat("Received {0} ", response);
                        state.sb = new StringBuilder();

                        Logger.Debug("ReceiveCallback-parser");
                        var frames = Parser.Parse(response);

                        Logger.Debug("ReceiveCallback-translate");
                        var messages = Translator.Translate(frames);

                        foreach (IMessage message in messages)
                        {
                            if (message != null)
                            {
                                Logger.DebugFormat("IMessage callback : {0} for ", message.DeviceId);

                                Callback(message);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("Processing frames exception >> {0}", ex);
            }
        }
Пример #2
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            bool error = false;
            Logger.Debug("ReceiveCallback");

            // Retrieve the state object and the client socket 
            // from the asynchronous state object.
            state = (StateObject)ar.AsyncState;
            client = state.workSocket;

            // Read data from the remote device.
            Logger.Debug("ReceiveCallback-client.EndReceive");

            var bytesReceived = 0;

            try
            {
   //             Monitor.Enter(client);
        //        try
        //        {
                    bytesReceived = client.EndReceive(ar);
        //        }
         //       catch (Exception e)
         //       {
         //           throw e;
         //       }
         //       finally
         //       {
         //           Monitor.Exit(client);
         //       }
                //Logger.DebugFormat("Received Data String was [{0}] from {1}", receivedData, clientData.Client.RemoteEndPoint);
            }
            catch (Exception e)
            {
                Logger.ErrorFormat("Receiving data {0} from socket failed from server exception {1} ", response, e);
                currentIp = currentIp.Equals(Ip) ? Ip2 : Ip;
                currentIPStatic = currentIp;
                CheckReconnectionError(e, ipAddress, currentIp, Port, remoteEP, client, ConnectCallback, RunStatic);
                /*if (!Reconnecting && e.GetType().ToString().Equals("System.Net.Sockets.SocketException"))
                {
                    error = true;
                    bytesReceived = 0;
                    ThreadStart t = new ThreadStart(Run);
                    RunStatic = new Thread(t);
                    RunStatic.Start();
                    return;
                }*/
            }

            try
            {
                if (bytesReceived > 0)
                {
                   // receptionTimer.Stop();
                   // Logger.Debug("Timer stopped");

                    // There might be more data, so store the data received so far.
                    state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesReceived));
                    response = state.sb.ToString().Trim();

                    // All the data has arrived; put it in response.
                    if (response.Length > 1 && response.EndsWith("|"))
                    {
                        Logger.DebugFormat("Received {0} ", response);
                        state.sb = new StringBuilder();

                        Logger.Debug("ReceiveCallback-parser");
                        var frames = Parser.Parse(response);

                        Logger.Debug("ReceiveCallback-translate");
                        var messages = Translator.Translate(frames);

                        foreach (IMessage message in messages)
                        {
                            if (message != null)
                            {
                                Logger.DebugFormat("IMessage callback : {0} for ", message.DeviceId);

                                Callback(message);
                            }
                        }
                    }
                }                
            }
            catch (Exception e)
            {
                Logger.ErrorFormat("Processing frames exception >> {0}", e);
                currentIp = currentIp.Equals(Ip) ? Ip2 : Ip;
                currentIPStatic = currentIp;
                CheckReconnectionError(e, ipAddress, currentIp, Port, remoteEP, client, ConnectCallback, RunStatic);
                /*if (!Reconnecting && e.GetType().ToString().Equals("System.Net.Sockets.SocketException"))
                {
                    ThreadStart t = new ThreadStart(Run);
                    RunStatic = new Thread(t);
                    RunStatic.Start();
                    return;
                }*/
            }
            finally
            {
                Receive(client);
            }
        }