Пример #1
0
        private void Loop()
        {
            while (true)
            {
                try
                {
                    byte[] DataLength = new byte[2];
                    Sock.Receive(DataLength);
                    short Length = System.BitConverter.ToInt16(DataLength, 0);

                    byte[] PacketIdentifier = new byte[1];
                    Sock.Receive(PacketIdentifier);

                    byte[] Packet = new byte[Length];
                    Sock.Receive(Packet);

                    if (PacketIdentifier[0] == 0xFF)
                    {
                        //get some shit
                        switch (Packet[0])
                        {
                        case 1:
                            //handshake
                            Handshake();
                            break;

                        default: throw new Exception("Got protocol ID, but is malformed.");
                        }
                    }
                    else
                    {
                        ISComm.EventArgs.OnMessageArgs args = new ISComm.EventArgs.OnMessageArgs();
                        args.ID = PacketIdentifier[0];
                        if (args.ID == 0xFF)
                        {
                            args.IsProtocolPacket = true;
                        }
                        else
                        {
                            args.IsProtocolPacket = false;
                        }
                        args.Data   = Packet;
                        args.Length = (short)(3 + Packet.Length);

                        //now call event handler
                        OnMessage(this, args);
                    }
                }
                catch (ObjectDisposedException)
                {
                    return;
                }
                catch (SocketException)
                {
                    return;
                }
            }
        }
Пример #2
0
        private void Loop()
        {
            while (true)
            {
                try
                {
                    byte[] DataLength = new byte[2];
                    int    bytes      = Client.Receive(DataLength);
                    short  Length     = System.BitConverter.ToInt16(DataLength, 0);

                    byte[] PacketIdentifier = new byte[1];
                    Client.Receive(PacketIdentifier);

                    byte[] Packet = new byte[Length];
                    Client.Receive(Packet);

                    if (PacketIdentifier[0] == 0xFF)
                    {
                        //get some shit
                        switch (Packet[0])
                        {
                        default: throw new Exception("Got protocol ID, but is malformed.");
                        }
                    }
                    else
                    {
                        ISComm.EventArgs.OnMessageArgs args = new ISComm.EventArgs.OnMessageArgs();
                        args.ID = PacketIdentifier[0];
                        if (args.ID == 0xFF)
                        {
                            args.IsProtocolPacket = true;
                        }
                        else
                        {
                            args.IsProtocolPacket = false;
                        }
                        args.Data   = Packet;
                        args.Length = (short)(3 + Packet.Length);

                        //now call event handler
                        OnMessage(this, args);
                    }
                }
                catch (ObjectDisposedException)
                {
                    //socket has closed, call OnDisconnect function and return.
                    OnDisconnect(this, System.EventArgs.Empty);
                    Connection = false;
                    return;
                }
                catch (SocketException)
                {
                    OnDisconnect(this, System.EventArgs.Empty);
                    Connection = false;
                    return;
                }
            }
        }
        private void Loop()
        {
            while (true)
            {
                try
                {
                    byte[] DataLength = new byte[2];
                    int bytes = Client.Receive(DataLength);
                    short Length = System.BitConverter.ToInt16(DataLength, 0);

                    byte[] PacketIdentifier = new byte[1];
                    Client.Receive(PacketIdentifier);

                    byte[] Packet = new byte[Length];
                    Client.Receive(Packet);

                    if (PacketIdentifier[0] == 0xFF)
                    {
                        //get some shit
                        switch (Packet[0])
                        {
                            default: throw new Exception("Got protocol ID, but is malformed.");
                        }
                    }
                    else
                    {

                        ISComm.EventArgs.OnMessageArgs args = new ISComm.EventArgs.OnMessageArgs();
                        args.ID = PacketIdentifier[0];
                        if (args.ID == 0xFF)
                        {
                            args.IsProtocolPacket = true;
                        }
                        else
                        {
                            args.IsProtocolPacket = false;
                        }
                        args.Data = Packet;
                        args.Length = (short)(3 + Packet.Length);

                        //now call event handler
                        OnMessage(this, args);
                    }
                }
                catch (ObjectDisposedException)
                {
                    //socket has closed, call OnDisconnect function and return.
                    OnDisconnect(this, System.EventArgs.Empty);
                    Connection = false;
                    return;
                }
                catch (SocketException)
                {
                    OnDisconnect(this, System.EventArgs.Empty);
                    Connection = false;
                    return;
                }
            }
        }