Пример #1
0
        protected void BeginReceiving(Socket socket, int socketIndex)
        {
            // Continue the attempts to receive data so long as the connection is open.
            while (this.GetIsConnected(socket))
            {
                try
                {
                    // Stores our packet header length.
                    int pLength = PACKET_HEADER_LENGTH;
                    // Stores the current amount of bytes that have been read.
                    int curRead = 0;
                    // Stores the bytes that we have read from the socket.
                    var data = new byte[pLength];

                    // Attempt to read from the socket.
                    curRead = socket.Receive(data, 0, pLength, SocketFlags.None);

                    // Read any remaining bytes.
                    while (curRead < pLength)
                        curRead += socket.Receive(data, curRead, pLength - curRead, SocketFlags.None);

                    // Set the current read to 0.
                    curRead = 0;
                    // Get the packet length (32 bit integer).
                    pLength = BitConverter.ToInt32(data, 0);
                    // Set the data (byte-buffer) to the size of the packet -- determined by pLength.
                    data = new byte[pLength];

                    // Attempt to read from the socket.
                    curRead = socket.Receive(data, 0, pLength, SocketFlags.None);

                    // Read any remaining bytes.
                    while (curRead < pLength)
                        curRead += socket.Receive(data, curRead, pLength - curRead, SocketFlags.None);

                    var dataBuffer = new DataBuffer();

                    // Fill our DataBuffer.
                    dataBuffer.FillBuffer(data);

                    // Get the unique packetID.
                    int packetID = dataBuffer.ReadInteger();

                    // Create a new Packet instance by finding the unique packet in our registered packets by using the packetID.

                    var packet = (from p in _registeredPackets
                                  where p.PacketID == packetID
                                  select p).FirstOrDefault();

                    // Create a new instance of Packet based on the registered packet that matched our unique packet id.
                    if (packet != null)
                    {
                        var execPacket = Activator.CreateInstance(packet.GetType()) as Packet;
                        // Fill the packet's DataBuffer.
                        execPacket.DataBuffer.FillBuffer(data);
                        // Set the DataBuffer's offset to the value of readOffset.
                        execPacket.DataBuffer.SetOffset(4);
                        execPacket.SocketIndex = socketIndex;

                        // Execute the packet.
                        if (this.Handle_Packet != null)
                            this.Handle_Packet.Invoke(execPacket);
                        else
                            execPacket.Execute(this);
                    }
                    else
                    {
                        Console.WriteLine("Invalid packet with ID: " + packetID + " received.");
                        continue;
                    }
                }

                catch (ObjectDisposedException)
                {
                    break;
                }

                catch (SocketException)
                {
                    break;
                }
            }

            this.HandleDisconnectedSocket(socket);
        }
Пример #2
0
        protected void SendPacket(Socket socket, Packet packet)
        {
            try
            {
                var dataBuffer = new DataBuffer();
                dataBuffer.WriteInteger(packet.PacketID);
                dataBuffer.WriteBytes(packet.DataBuffer.ReadBytes());

                byte[] data = dataBuffer.ReadBytes();

                byte[] packetHeader = BitConverter.GetBytes(data.Length);

                int sent = socket.Send(packetHeader);

                while (sent < packetHeader.Length)
                    sent += socket.Send(packetHeader, sent, packetHeader.Length - sent, SocketFlags.None);

                sent = socket.Send(data);

                while (sent < data.Length)
                    sent += socket.Send(data, sent, data.Length - sent, SocketFlags.None);
            }
            catch (NullReferenceException)
            {

            }
            catch (SocketException)
            {
            }
            catch (ObjectDisposedException)
            {
            }
        }