Exemple #1
0
        private void InternalReceive(IAsyncResult ar)
        {
            SocketError se    = SocketError.SocketError;
            int         bytes = NetworkSocket?.EndReceive(ar, out se) ?? 0;

            if (se != SocketError.Success)
            {
                Disconnect(new SocketException());
                return;
            }

            byte[] packet = _defragger.Process(bytes);

            ReceiveWithDefragger();

            if (packet == null)
            {
                return;
            }
            try {
                IPacket pack = Packager.Unpack(packet, PackagingConfiguration);
                _receiveCallback(this, pack);
            } catch (Exception ex) {
                Disconnect(ex);
                return;
            }
        }
Exemple #2
0
        private void InternalReceve(IAsyncResult at)
        {
            var se    = SocketError.SocketError;
            var bytes = NetworkSocket?.EndReceive(at, out se) ?? 0;

            if (se != SocketError.Success)
            {
                Disconnect(new SocketException());
                return;
            }

            var packet = Defragger.Process(bytes);

            ReceiveWithDefragger();

            if (packet != null)
            {
                try
                {
                    var pack = Packager.Unpack(packet, PackagingConfiguration);
                    ReceveCallback(this, pack);
                }
                catch (Exception ex)
                {
                    Disconnect(ex);
                    return;
                }
            }
        }
Exemple #3
0
        private void InternalReceve(IAsyncResult AR)
        {
            SocketError SE    = SocketError.SocketError;
            int         bytes = NetworkSocket?.EndReceive(AR, out SE) ?? 0;

            if (SE != SocketError.Success)
            {
                Disconnect(new SocketException());
                return;
            }

            byte[] packet = Defragger.Process(bytes);

            ReceveWithDefragger();

            if (packet != null)
            {
                try {
                    IPacket pack = Packager.Unpack(packet, PackagingConfiguration);
                    ReceveCallback(this, pack);
                } catch (Exception ex) {
                    Disconnect(ex);
                    return;
                }
            }
        }
Exemple #4
0
        private void HandleReceve(IAsyncResult AR)
        {
            int packetSize = 0;

            try
            {
                packetSize = NetworkSocket.EndReceive(AR);
            }
            catch
            {
                OnDisconnect?.Invoke(this);
                return;
            }
            using (MemoryStream ms = new MemoryStream(PacketBuffer, 0, packetSize))
                using (BinaryReader br = new BinaryReader(ms))
                {
                    while (ms.Position != ms.Length)
                    {
                        int len = br.ReadInt32();

                        if (CurrentPacketLength == 0)
                        {
                            if (len <= 0)
                            {
                                NetworkSocket.BeginReceive(PacketBuffer, 0, PacketBuffer.Length, SocketFlags.None, RetrieveCallback, null);
                                return;
                            }

                            int cur = (int)(ms.Length - ms.Position);
                            if (cur >= len)
                            {
                                OnDataRetrieve?.Invoke(br.ReadBytes(len));
                            }
                            else
                            {
                                CurrentPacketLength = len;
                                PacketConstructor.Write(br.ReadBytes(cur), 0, cur);
                            }
                        }
                        else
                        {
                            int cur           = (int)(ms.Length - ms.Position);
                            int neededpackets = (int)(CurrentPacketLength - PacketConstructor.Length);
                            if (cur >= neededpackets)
                            {
                                PacketConstructor.Write(br.ReadBytes(neededpackets), 0, neededpackets);
                                OnDataRetrieve?.Invoke(PacketConstructor.ToArray());
                                PacketConstructor = new MemoryStream();
                            }
                            else
                            {
                                PacketConstructor.Write(br.ReadBytes(cur), 0, cur);
                            }
                        }
                    }
                }

            NetworkSocket.BeginReceive(PacketBuffer, 0, PacketBuffer.Length, SocketFlags.None, RetrieveCallback, null);
        }