Пример #1
0
 protected override void Worker()
 {
     while (!this._closing)
     {
         int available = this.Port.Available;
         if (available > 0)
         {
             byte[]   numArray = new byte[available];
             EndPoint remoteEP = (EndPoint) new IPEndPoint(IPAddress.Any, 0);
             this.Port.ReceiveFrom(numArray, ref remoteEP);
             this.Protocol.OnIncommingData(numArray);
             ServerCommData serverCommData = new ServerCommData(this.Protocol);
             serverCommData.IncomingData = new ByteArrayReader(numArray);
             ServerCommData data = serverCommData;
             if (this.Protocol.Codec.ServerDecode((CommDataBase)data).Status == 3)
             {
                 this.OnServeCommand(data);
                 this.Protocol.Codec.ServerEncode((CommDataBase)data);
                 byte[] array = data.OutgoingData.ToArray();
                 this.Port.SendTo(array, remoteEP);
                 this.Protocol.OnOutgoingData(array);
             }
         }
         Thread.Sleep(100);
     }
 }
Пример #2
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();
        }
Пример #3
0
        protected virtual void OnServeCommand(ServerCommData data)
        {
            var handler = ServeCommand;

            if (handler != null)
            {
                handler(this, new ServeCommandEventArgs(data));
            }
        }
Пример #4
0
        protected virtual void OnServeCommand(ServerCommData data)
        {
            var handler = ServeCommand;

            if (handler != null)
            {
                handler(this, new ServeCommandEventArgs(data));
            }
        }
Пример #5
0
        protected virtual void OnServeCommand(ServerCommData data)
        {
            ServeCommandHandler serveCommand = this.ServeCommand;

            if (serveCommand == null)
            {
                return;
            }
            serveCommand((object)this, new ServeCommandEventArgs(data));
        }
Пример #6
0
        /// <summary>
        /// Running thread handler
        /// </summary>
        protected override void Worker()
        {
            //loop, until the host closes
            while (_closing == false)
            {
                //look for incoming data
                int length = Port.Available;

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

                    //read the data from the physical port
                    Port.ReceiveFrom(
                        buffer,
                        ref remote);
                    Protocol.OnIncommingData(buffer, length);
                    //try to decode the incoming data
                    var data = new ServerCommData(Protocol)
                    {
                        IncomingData = new ByteArrayReader(buffer)
                    };

                    var 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.SendTo(
                            outgoing,
                            remote);
                        Protocol.OnOutgoingData(outgoing);
                    }
                }

                Thread.Sleep(100);
            }
        }
Пример #7
0
        /// <summary>
        /// Running thread handler
        /// </summary>
        protected override void Worker()
        {
            //loop, until the host closes
            while (_closing == false)
            {
                //look for incoming data
                int length = Port.Available;

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

                    //read the data from the physical port
                    Port.ReceiveFrom(
                        buffer,
                        ref remote);
                    Protocol.OnIncommingData(buffer);
                    //try to decode the incoming data
                    var data = new ServerCommData(Protocol) {IncomingData = new ByteArrayReader(buffer)};

                    var 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.SendTo(
                            outgoing,
                            remote);
                        Protocol.OnOutgoingData(outgoing); 
                    }
                }

                Thread.Sleep(100);
            }
        }
Пример #8
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);
            }
        }
Пример #9
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");
        }
Пример #10
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);
            }
        }
Пример #11
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");
        }
Пример #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);
            }
        }
Пример #13
0
 public ServeCommandEventArgs(ServerCommData data)
 {
     Data = data;
 }
Пример #14
0
 public ServeCommandEventArgs(ServerCommData data)
 {
     Data = data;
 }
Пример #15
0
 public ServeCommandEventArgs(ServerCommData data) => this.Data = data;