Example #1
0
        protected virtual void OnServeCommand(ServerCommData data)
        {
            var handler = this.ServeCommand;

            if (handler != null)
            {
                handler(
                    this,
                    new ServeCommandEventArgs(data));
            }
        }
Example #2
0
        /// <summary>
        /// Running thread handler
        /// </summary>
        protected override void Worker()
        {
            //loop, until the host closes
            while (this._closing == false)
            {
                //look for incoming data
                int length = this.Port.Available;

                if (length > 0)
                {
                    var      buffer = new byte[length];
                    EndPoint remote = new IPEndPoint(IPAddress.Any, 0);

                    //read the data from the physical port
                    this.Port.ReceiveFrom(
                        buffer,
                        ref remote);

                    //try to decode the incoming data
                    var data = new ServerCommData(this.Protocol);
                    data.IncomingData = new ByteArrayReader(buffer);

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

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

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

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

                        this.Port.SendTo(
                            outgoing,
                            remote);
                    }
                }

                Thread.Sleep(0);
            }
        }
Example #3
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 (Timer timer = new Timer(
                       _ => counter--,
                       state: null,
                       dueTime: 1000,
                       period: 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 (
                    this._closing == false &&
                    counter > 0)
                {
                    //look for incoming data
                    int length = this.Port.Available;

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

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

                        //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(this.Protocol);
                        data.IncomingData = writer.ToReader();

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

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

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

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

                            this.Port.Send(outgoing);

                            //reset the timer
                            counter = IdleTimeout;
                            writer  = null;
                        }
                        else if (result.Status == CommResponse.Ignore)
                        {
                            writer = null;
                        }
                    }

                    Thread.Sleep(0);
                }
            }

            this.Port.Close();
            Debug.Print("close");
        }
Example #4
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 (this._closing == false)
            {
                //look for incoming data
                int length = this.Port.BytesToRead;

                if (length > 0)
                {
                    var buffer = new byte[length];

                    //read the data from the physical port
                    this.Port.Read(
                        buffer,
                        0,
                        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(this.Protocol);
                    data.IncomingData = writer.ToReader();

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

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

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

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

                        this.Port.Write(
                            outgoing,
                            0,
                            outgoing.Length);

                        writer = null;
                    }
                    else if (result.Status == CommResponse.Ignore)
                    {
                        writer = null;
                    }
                }

                Thread.Sleep(0);
            }
        }
 public ServeCommandEventArgs(ServerCommData data)
 {
     this.Data = data;
 }