Пример #1
0
        public void Send(Packet packet)
        {
            if (Sock != null && Sock.Connected)
            {
                using (MemoryStream Stream = new MemoryStream( ))
                using (BinaryWriter PacketBuilder = new BinaryWriter(Stream))
                {
                    //RaiseEvent if event is linked
                    if (PacketSent != null)
                    {
                        PacketSent(this, packet);
                    }

                    SendCounter++;
                    PacketBuilder.Write((byte)0x00);// Sec Bytes Count
                    PacketBuilder.Write((ushort)packet.Data.Length); // Data Bytes Count
                    PacketBuilder.Write(packet.Opcode); // Opcode
                    PacketBuilder.Write(SendCounter); // Counter
                    PacketBuilder.Write((byte)0x0E); // Header Length
                    PacketBuilder.Write(1);
                    PacketBuilder.Write(packet.Data);
                    Send(Stream.ToArray( ));
                }
            }
        }
Пример #2
0
        private void WaitForData(IAsyncResult DataAsync)
        {
            if (!isClosing)
            {
                Socket Worker = null;
                try
                {
                    Worker = (Socket)DataAsync.AsyncState;
                    int rcvdBytes = Worker.EndReceive(DataAsync);
                    if (rcvdBytes > 0)
                    {
                        int Length = 0;

                        byte[] newBuffer = SubArray(Buffer, 0, BitConverter.ToUInt16(Buffer, 1));
                        Packet packet = new Packet(newBuffer, false, out Length);

                        //RaiseEvent if event is linked
                        if (PacketReceived != null)
                        {
                            PacketReceived(this, packet);
                        }
                    }
                    else
                    {
                        isClosing = true;
                        //RaisEvent
                        if (Disconnected != null)
                        {
                            Disconnected(this);
                        }
                    }
                } catch (SocketException se)
                {
                    if (se.SocketErrorCode == SocketError.ConnectionReset || se.SocketErrorCode == SocketError.InvalidArgument) //Client Disconnected
                    {
                        isClosing = true;
                        //RaisEvent
                        if (Disconnected != null)
                        {
                            Disconnected(this);
                        }

                        //Mark worker as null to stop reciveing.
                        Worker = null;
                    }
                    else
                    {

                    }
                } finally
                {
                    if (Worker != null)
                    {
                        try
                        {
                            Buffer = new byte[8192];
                            Worker.BeginReceive(Buffer, 0, 8192, SocketFlags.None, new AsyncCallback(WaitForData), Worker);
                        } catch
                        {

                        }
                    }
                }
            }
        }