Esempio n. 1
0
        private void ReadCallbackStatic(IAsyncResult ar)
        {
            try
            {
                if (client.EndReceive(ar) > 0)
                {
                    int packetLength = BitConverter.ToInt32(buffer, 0);
                    buffer = new byte[packetLength];

                    int recv = 0;

                    if (buffer.Length > 0)
                    {
                        while (recv < packetLength)
                        {
                            recv = recv + client.Receive(buffer, recv, buffer.Length - recv, SocketFlags.Partial);
                        }

                        Type pck = packetProcessor.processPacket(buffer);
                        Trafic.ChangeTrafic(Trafic.TraficType.Received, buffer.Length);
                        if (pck != null)
                        {
                            R_Packet rbp = (R_Packet)Activator.CreateInstance(pck, this, buffer);
                            rbp.Run();
                        }
                        else
                        {
                            Disconnect();
                            return;
                        }

                        Read();

                        return;
                    }
                    else
                    {
                        Disconnect();
                        return;
                    }
                }
                else
                {
                    Disconnect();
                    return;
                }
            }
            catch (Exception ex)
            {
                if (!client.Connected)
                {
                    Disconnect();
                }
                return;
            }
        }
Esempio n. 2
0
        private void Client_BeginReceive(IAsyncResult ar)
        {
            try
            {
                if (Client.EndReceive(ar) > 0)
                {
                    int packetLength = BitConverter.ToInt32(buffer, 0);
                    buffer = new byte[packetLength];

                    int recv = 0;

                    if (buffer.Length > 0)
                    {
                        while (recv < packetLength)
                        {
                            recv = recv + Client.Receive(buffer, recv, buffer.Length, SocketFlags.Partial);
                        }

                        Type pck = packetProcessor.processPacket(buffer);
                        if (pck != null)
                        {
                            R_Packet rbp = (R_Packet)Activator.CreateInstance(pck, this, buffer);
                            rbp.Run();
                        }
                        Read();
                    }
                    else
                    {
                        Client.Disconnect(false);
                    }
                }
            }
            catch
            {
                if (!Client.Connected)
                {
                    Connect();
                }
            }
        }