Exemplo n.º 1
0
        protected override void Worker()
        {
            int counter = this.IdleTimeout;

            using (new Timer((TimerCallback)(_ => -- counter), (object)null, 1000, 1000))
            {
                ByteArrayWriter byteArrayWriter = (ByteArrayWriter)null;
                byte[]          numArray        = new byte[300];
                while (!this._closing)
                {
                    if (counter > 0)
                    {
                        int num = this.Port.Available;
                        if (num > 0)
                        {
                            if (num > 300)
                            {
                                num = 300;
                            }
                            this.Port.Receive(numArray, num, SocketFlags.None);
                            this.Protocol.OnIncommingData(numArray);
                            if (byteArrayWriter == null)
                            {
                                byteArrayWriter = new ByteArrayWriter();
                            }
                            byteArrayWriter.WriteBytes(numArray, 0, num);
                            ServerCommData data = new ServerCommData(this.Protocol);
                            data.IncomingData = byteArrayWriter.ToReader();
                            switch (this.Protocol.Codec.ServerDecode((CommDataBase)data).Status)
                            {
                            case 1:
                                byteArrayWriter = (ByteArrayWriter)null;
                                break;

                            case 3:
                                this.OnServeCommand(data);
                                this.Protocol.Codec.ServerEncode((CommDataBase)data);
                                if (data.OutgoingData != null)
                                {
                                    byte[] array = data.OutgoingData.ToArray();
                                    this.Port.Send(array);
                                    this.Protocol.OnOutgoingData(array);
                                }
                                counter         = this.IdleTimeout;
                                byteArrayWriter = (ByteArrayWriter)null;
                                break;
                            }
                        }
                        Thread.Sleep(100);
                    }
                    else
                    {
                        break;
                    }
                }
            }
            this.Port.Close();
        }
Exemplo n.º 2
0
        private void OnReceive(IAsyncResult result)
        {
            if (result.IsCompleted == false)
            {
                CallException(0xFF, 0xFF, ModbusCommand.ExceptionConnectionLost);
            }

            //create a writer for accumulate the incoming data
            var incoming = new ByteArrayWriter();

            //append data to the writer
            incoming.WriteBytes(tcpAsyClBuffer, 0, tcpAsyClBuffer[8] + 9);
            var data = (ClientCommData)result.AsyncState;

            //try to decode the stream
            data.IncomingData = incoming.ToReader();
            var resp = data.OwnerProtocol.Codec.ClientDecode(data);

            if (OnResponseData != null)
            {
                OnResponseData(resp);
            }
        }
Exemplo n.º 3
0
        private void Worker()
        {
            ByteArrayWriter byteArrayWriter = (ByteArrayWriter)null;

            while (!this._closing)
            {
                int bytesToRead = this.Port.BytesToRead;
                if (bytesToRead > 0)
                {
                    byte[] numArray = new byte[bytesToRead];
                    this.Port.Read(numArray, 0, bytesToRead);
                    this.Protocol.OnIncommingData(numArray);
                    if (byteArrayWriter == null)
                    {
                        byteArrayWriter = new ByteArrayWriter();
                    }
                    byteArrayWriter.WriteBytes(numArray, 0, bytesToRead);
                    ServerCommData data = new ServerCommData(this.Protocol);
                    data.IncomingData = byteArrayWriter.ToReader();
                    CommResponse commResponse = this.Protocol.Codec.ServerDecode((CommDataBase)data);
                    if (commResponse.Status == 3)
                    {
                        this.OnServeCommand(data);
                        this.Protocol.Codec.ServerEncode((CommDataBase)data);
                        byte[] array = data.OutgoingData.ToArray();
                        this.Port.Write(array, 0, array.Length);
                        this.Protocol.OnOutgoingData(array);
                        byteArrayWriter = (ByteArrayWriter)null;
                    }
                    else if (commResponse.Status == 1)
                    {
                        byteArrayWriter = (ByteArrayWriter)null;
                    }
                }
                Thread.Sleep(100);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Entry-point for submitting a query to the remote device
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public CommResponse Query(ClientCommData data)
        {
            lock (Port)
            {
                //convert the request data as an ordinary byte array
                byte[] outgoing = data.OutgoingData.ToArray();

                //create a writer for accumulate the incoming data
                var incoming = new ByteArrayWriter();

                const int tempSize = 256;
                var temp = new byte[tempSize];

                //retries loop
                for (int attempt = 0, retries = data.Retries; attempt < retries; attempt++)
                {
                    //phyiscal writing
                    Port.Send(outgoing);
                    incoming.Reset();

                    //start the local timer
                    bool timeoutExpired;
                    int totalTimeout = Latency + data.Timeout;

                    using (new Timer(_ => timeoutExpired = true, null, totalTimeout, Timeout.Infinite))
                    {
                        //reception loop, until a valid response or timeout
                        timeoutExpired = false;
                        while (timeoutExpired == false)
                        {
                            var length = Port.Available;

                            if (length > 0)
                            {
                                if (length > tempSize)
                                    length = tempSize;

                                //read the incoming data from the physical port
                                Port.Receive(temp, length, SocketFlags.None);

                                //append data to the writer
                                incoming.WriteBytes(temp, 0, length);

                                //try to decode the stream
                                data.IncomingData = incoming.ToReader();

                                CommResponse result = data.OwnerProtocol.Codec.ClientDecode(data);

                                //exit whether any concrete result: either good or bad
                                if (result.Status == CommResponse.Ack)
                                {
                                    return result;
                                }
                                if (result.Status == CommResponse.Critical)
                                {
                                    return result;
                                }
                                if (result.Status != CommResponse.Unknown)
                                {
                                    break;
                                }
                            }

                            Thread.Sleep(0);

                            //stop immediately if the host asked to abort

                            //TODO
                        }
                    }   //using (timer)
                }       //for

                //no attempt was successful
                return new CommResponse(
                    data,
                    CommResponse.Critical);
            }   //lock
        }
Exemplo n.º 5
0
        private void OnReceive(IAsyncResult result)
        {
            if (result.IsCompleted == false) CallException(0xFF, 0xFF, ModbusCommand.ExceptionConnectionLost);

            //create a writer for accumulate the incoming data
            var incoming = new ByteArrayWriter();
            //append data to the writer
            incoming.WriteBytes(tcpAsyClBuffer, 0, tcpAsyClBuffer[8]+9);
            var data = (ClientCommData)result.AsyncState;
            //try to decode the stream
            data.IncomingData = incoming.ToReader();
            var resp = data.OwnerProtocol.Codec.ClientDecode(data);
            if (OnResponseData != null) OnResponseData(resp);
        }
Exemplo n.º 6
0
 /// <summary>
 /// Add the content of another writer to the writer
 /// </summary>
 /// <remarks>
 /// This functionality is not allowed when the writer has been sealed
 /// </remarks>
 public void WriteBytes(ByteArrayWriter writer)
 {
     WriteBytes(((IByteArray)writer).Data);
 }
Exemplo n.º 7
0
        /// <summary>
        /// Entry-point for submitting a query to the remote device
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public CommResponse Query(ClientCommData data)
        {
            lock (Port)
            {
                //convert the request data as an ordinary byte array
                byte[] outgoing = data
                                  .OutgoingData
                                  .ToArray();

                //create a writer for accumulate the incoming data
                var incoming = new ByteArrayWriter();

                const int tempSize = 256;
                var       temp     = new byte[tempSize];

                //retries loop
                for (int attempt = 0, retries = data.Retries; attempt < retries; attempt++)
                {
                    //flush any residual content
                    Port
                    .DiscardInBuffer();

                    Port
                    .DiscardOutBuffer();

                    //phyiscal writing
                    Port
                    .Write(outgoing, 0, outgoing.Length);

                    incoming.Reset();
                    Thread.Sleep(100);
                    //start the local timer
                    bool timeoutExpired;
                    int  totalTimeout = Latency + data.Timeout;

                    using (Timer timer = new Timer(
                               _ => timeoutExpired = true,
                               state: null,
                               dueTime: totalTimeout,
                               period: Timeout.Infinite))
                    {
                        //reception loop, until a valid response or timeout
                        timeoutExpired = false;
                        while (timeoutExpired == false)
                        {
                            int length = Port.BytesToRead;

                            if (length > 0)
                            {
                                if (length > tempSize)
                                {
                                    length = tempSize;
                                }

                                //read the incoming data from the physical port
                                Port
                                .Read(temp, 0, length);

                                //append data to the writer
                                incoming.WriteBytes(
                                    temp,
                                    0,
                                    length);

                                //try to decode the stream
                                data.IncomingData = incoming.ToReader();

                                CommResponse result = data
                                                      .OwnerProtocol
                                                      .Codec
                                                      .ClientDecode(data);

                                //exit whether any concrete result: either good or bad
                                if (result.Status == CommResponse.Ack)
                                {
                                    return(result);
                                }
                                else if (result.Status == CommResponse.Critical)
                                {
                                    return(result);
                                }
                                else if (result.Status != CommResponse.Unknown)
                                {
                                    break;
                                }
                            }

                            Thread.Sleep(100);

                            //stop immediately if the host asked to abort

                            //TODO
                        }
                    }   //using (timer)
                }       //for

                //no attempt was successful
                return(new CommResponse(
                           data,
                           CommResponse.Critical));
            }   //lock
        }
Exemplo n.º 8
0
        /// <summary>
        /// Running thread handler
        /// </summary>
        protected override void Worker()
        {
            Debug.Print("start");
            //start the local timer, which gets the session dying
            int counter = IdleTimeout;

            using (var timer = new Timer(_ => counter--, null, 1000, 1000))
            {
                //create a writer for the incoming data
                ByteArrayWriter writer = null;
                var             buffer = new byte[CacheSize];
                //loop, until the host closes, or the timer expires
                while (_closing == false && counter > 0)
                {
                    //look for incoming data
                    int length = Port.Available;
                    if (length > 0)
                    {
                        if (length > CacheSize)
                        {
                            length = CacheSize;
                        }
                        //read the data from the physical port
                        Port.Receive(buffer, length, SocketFlags.None);
                        Protocol.OnIncommingData(buffer, length);
                        //append the data to the writer
                        if (writer == null)
                        {
                            writer = new ByteArrayWriter();
                        }
                        writer.WriteBytes(buffer, 0, length);
                        //try to decode the incoming data
                        var data = new ServerCommData(Protocol);
                        data.IncomingData = writer.ToReader();
                        var result = Protocol.Codec.ServerDecode(data);
                        switch (result.Status)
                        {
                        case CommResponse.Ack:
                        {
                            //the command is recognized, so call the host back
                            OnServeCommand(data);
                            //encode the host data
                            Protocol.Codec.ServerEncode(data);
                            //return the resulting data to the remote caller
                            if (data.OutgoingData != null)
                            {
                                byte[] outgoing = data.OutgoingData.ToArray();
                                Port.Send(outgoing);
                                Protocol.OnOutgoingData(outgoing);
                            }

                            //reset the timer
                            counter = IdleTimeout;
                            writer  = null;
                        }
                        break;

                        case CommResponse.Ignore:
                            writer = null;
                            break;
                        }
                    }
                    Thread.Sleep(100);
                }
            }
            Port.Close();
            Debug.Print("close");
        }
Exemplo n.º 9
0
        /// <summary>
        /// Running thread handler
        /// </summary>
        private void Worker()
        {
            //create a writer for the incoming data
            ByteArrayWriter writer = null;

            //loop, until the host closes
            while (_closing == false)
            {
                //look for incoming data
                int length = Port.BytesToRead;
                if (length > 0)
                {
                    var buffer = new byte[length];

                    //read the data from the physical port
                    Port.Read(
                        buffer,
                        0,
                        length);
                    Protocol.OnIncommingData(buffer);
                    //append the data to the writer
                    if (writer == null)
                        writer = new ByteArrayWriter();

                    writer.WriteBytes(
                        buffer,
                        0,
                        length);

                    //try to decode the incoming data
                    var data = new ServerCommData(Protocol);
                    data.IncomingData = writer.ToReader();

                    CommResponse result = Protocol
                        .Codec
                        .ServerDecode(data);

                    if (result.Status == CommResponse.Ack)
                    {
                        //the command is recognized, so call the host back
                        OnServeCommand(data);

                        //encode the host data
                        Protocol
                            .Codec
                            .ServerEncode(data);

                        //return the resulting data to the remote caller
                        byte[] outgoing = data
                            .OutgoingData
                            .ToArray();

                        Port.Write(
                            outgoing,
                            0,
                            outgoing.Length);
                        Protocol.OnOutgoingData(outgoing); 
                        writer = null;
                    }
                    else if (result.Status == CommResponse.Ignore)
                    {
                        writer = null;
                    }
                }

                Thread.Sleep(100);
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Running thread handler
        /// </summary>
        protected override void Worker()
        {
            Debug.Print("start");
            //start the local timer, which gets the session dying
            int counter = IdleTimeout;

            using (var timer = new Timer(_ => counter--, null, 1000, 1000))
            {
                //create a writer for the incoming data
                ByteArrayWriter writer = null;
                var buffer = new byte[CacheSize];
                //loop, until the host closes, or the timer expires
                while (_closing == false && counter > 0)
                {
                    //look for incoming data
                    int length = Port.Available;
                    if (length > 0)
                    {
                        if (length > CacheSize)
                            length = CacheSize;
                        //read the data from the physical port
                        Port.Receive(buffer, length, SocketFlags.None);
                        Protocol.OnIncommingData(buffer);
                        //append the data to the writer
                        if (writer == null)
                            writer = new ByteArrayWriter();
                        writer.WriteBytes(buffer, 0, length);
                        //try to decode the incoming data
                        var data = new ServerCommData(Protocol);
                        data.IncomingData = writer.ToReader();
                        var result = Protocol.Codec.ServerDecode(data);
                        switch (result.Status)
                        {
                            case CommResponse.Ack:
                                {
                                    //the command is recognized, so call the host back
                                    OnServeCommand(data);
                                    //encode the host data
                                    Protocol.Codec.ServerEncode(data);
                                    //return the resulting data to the remote caller
                                    if (data.OutgoingData != null)
                                    {
                                        byte[] outgoing = data.OutgoingData.ToArray();
                                        Port.Send(outgoing);
                                        Protocol.OnOutgoingData(outgoing);                                        
                                    }

                                    //reset the timer
                                    counter = IdleTimeout;
                                    writer = null;
                                }
                                break;
                            case CommResponse.Ignore:
                                writer = null;
                                break;
                        }
                    }
                    Thread.Sleep(100);
                }
            }
            Port.Close();
            Debug.Print("close");
        }
Exemplo n.º 11
0
 /// <summary>
 /// Add the content of another writer to the writer
 /// </summary>
 /// <remarks>
 /// This functionality is not allowed when the writer has been sealed
 /// </remarks>
 public void WriteBytes(ByteArrayWriter writer)
 {
     WriteBytes(((IByteArray)writer).Data);
 }
Exemplo n.º 12
0
        /// <summary>
        /// Running thread handler
        /// </summary>
        private void Worker()
        {
            //create a writer for the incoming data
            ByteArrayWriter writer = null;

            //loop, until the host closes
            while (_closing == false)
            {
                //look for incoming data
                int length = Port.BytesToRead;
                if (length > 0)
                {
                    var buffer = new byte[length];

                    //read the data from the physical port
                    Port.Read(
                        buffer,
                        0,
                        length);
                    Protocol.OnIncommingData(buffer, length);
                    //append the data to the writer
                    if (writer == null)
                    {
                        writer = new ByteArrayWriter();
                    }

                    writer.WriteBytes(
                        buffer,
                        0,
                        length);

                    //try to decode the incoming data
                    var data = new ServerCommData(Protocol);
                    data.IncomingData = writer.ToReader();

                    CommResponse result = Protocol
                                          .Codec
                                          .ServerDecode(data);

                    if (result.Status == CommResponse.Ack)
                    {
                        //the command is recognized, so call the host back
                        OnServeCommand(data);

                        //encode the host data
                        Protocol
                        .Codec
                        .ServerEncode(data);

                        //return the resulting data to the remote caller
                        byte[] outgoing = data
                                          .OutgoingData
                                          .ToArray();

                        Port.Write(
                            outgoing,
                            0,
                            outgoing.Length);
                        Protocol.OnOutgoingData(outgoing);
                        writer = null;
                    }
                    else if (result.Status == CommResponse.Ignore)
                    {
                        writer = null;
                    }
                }

                Thread.Sleep(100);
            }
        }
Exemplo n.º 13
0
 public void WriteBytes(ByteArrayWriter writer) => this.WriteBytes(((IByteArray)writer).Data);