Beispiel #1
0
        public void ListenOnce()
        {
            if (!IsConnected())
            {
                return;
            }

            if (Available())
            {
                ReserveBufferStorage(ref receiveBuffer, connection.Available);

                connection.Client.Receive(receiveBuffer, 0, connection.Available, SocketFlags.None);

                var packets = PacketSerializer.Deserialize(receiveBuffer);

                for (var i = 0; i < packets.Length; i++)
                {
                    Received(this, packets[i]);
                }
            }
        }
Beispiel #2
0
        public void Send(params IPacket[] packets)
        {
            if (!IsConnected())
            {
                return;
            }

            var bytes = packets.Sum(p => Packet.GetSize(p.Type)) + Packet.HeaderSize + Packet.PacketTypeSize * packets.Length;

            ReserveBufferStorage(ref sendBuffer, bytes);

            PacketSerializer.Serialize(packets, ref sendBuffer);

            var socket = connection.Client;

            if (socket == null || !socket.Connected)
            {
                return;
            }

            socket.Send(sendBuffer, 0, bytes, SocketFlags.None);
        }
Beispiel #3
0
        public void Send(IPacket packet)
        {
            if (!IsConnected())
            {
                return;
            }

            var bytes = Packet.GetSize(packet.Type) + Packet.HeaderSize + Packet.PacketTypeSize;

            ReserveBufferStorage(ref sendBuffer, bytes);

            PacketSerializer.Serialize(packet, ref sendBuffer);

            var socket = connection.Client;

            if (socket == null || !socket.Connected)
            {
                return;
            }

            socket.Send(sendBuffer, 0, bytes, SocketFlags.None);
        }
Beispiel #4
0
        private void BeginReceiveCallback(IAsyncResult results)
        {
            if (!IsConnected())
            {
                return;
            }

            var bytes = connection.Available;

            ReserveBufferStorage(ref receiveBuffer, bytes);

            connection.Client.Receive(receiveBuffer, 4, bytes, SocketFlags.None);

            var packets = PacketSerializer.Deserialize(receiveBuffer);

            for (var i = 0; i < packets.Length; i++)
            {
                Received(this, packets[i]);
            }

            connection.Client.EndReceive(results);

            InternalBeginListen();
        }