Exemplo n.º 1
0
        /// <summary>
        /// Enables the socket to accept packets
        /// </summary>
        public void Enable()
        {
            if (this.packetProcessor == null) packetProcessor = new PacketProcessor();
            packetProcessor.onProcessPacket += this.OnProcessPacket;
            packetProcessor.StartProcessing();

            lock (SyncRecv)
            {
                Console.Out.WriteLine("Starting receiving packets..");
                while (Receiving)
                {
                    if (Sock.Connected)
                    {
                        int size = 0;
                        try
                        {
                            size = Sock.Receive(buff);
                        }
                        catch (Exception e)
                        {
                            this.Disable();
                            break;
                        }
                        if (size == 0)
                        {
                            Disable();
                            Show("Will not accept 'byte[i <= 0]'");
                            Receiving = false;
                            break;
                        }
                        if (size < 1000)
                        {
                            byte[] data = new byte[size];
                            Array.Copy(buff, data, size);
                            //TODO: Should go to a SocketProcessor here.

                            byte[] headerlessData = new byte[data.Length - 1];
                            for (int i = 1; i < data.Length; i++)
                            {
                                headerlessData[i - 1] = data[i];
                            }
                            Packet p = new Packet(data[0], headerlessData);
                            this.packetProcessor.QueuePacket(p);
                        }
                        else
                        {
                            Disable();
                            Show("Cannot accept 'bytes[i > 1000]'");
                            Receiving = false;
                            break;
                        }
                    }
                }
                if (onDisconnectListeners != null) onDisconnectListeners();
                Console.Out.WriteLine("Ending receiving packets..");
            }
            packetProcessor.StopProcessing();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Enables the socket to accept packets
        /// </summary>
        public void Enable()
        {
            if (this.packetProcessor == null) packetProcessor = new PacketProcessor();
            packetProcessor.onProcessPacket += this.OnProcessPacket;
            packetProcessor.StartProcessing();

            lock (SyncRecv)
            {
                Console.Out.WriteLine("Starting receiving packets..");
                while (Receiving)
                {
                    if (Sock.Connected)
                    {
                        int size = 0;
                        try
                        {
                            size = Sock.Receive(buff);
                        }
                        catch (Exception e)
                        {
                            this.Disable();
                            break;
                        }
                        if (size == 0)
                        {
                            // Disable();
                            Show("Will not accept 'byte[i <= 0]'");
                            // Receiving = false;
                            // break;
                        }
                        else if (size < 131071)
                        {
                            byte[] data = new byte[size];
                            Array.Copy(buff, data, size);
                            this.packetProcessor.QueuePacket(data, this);
                        }
                        else
                        {
                            // Disable();
                            Show("Cannot accept 'bytes[i > 131071]'");
                            // Receiving = false;
                            // break;
                        }
                    }
                }
                if (onDisconnectListeners != null) onDisconnectListeners();
                Console.Out.WriteLine("Ending receiving packets..");
            }
            packetProcessor.StopProcessing();
        }