Esempio n. 1
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);
        }
Esempio n. 2
0
        /// <summary>
        /// Uses defagger to receve packets.
        /// </summary>
        private void ReceiveWithDefragger()
        {
            NetworkSocket.BeginReceive(Defragger.ReceveBuffer, Defragger.BufferIndex, Defragger.BytesToReceve, SocketFlags.None, out SocketError SE, InternalReceve, null);

            if (SE != SocketError.Success)
            {
                Disconnect(new SocketException());
            }
        }
Esempio n. 3
0
        public void StartRetrieve()
        {
            if (RetrieveCallback != null)
            {
                return;
            }

            RetrieveCallback = new AsyncCallback(HandleReceve);
            NetworkSocket.BeginReceive(PacketBuffer, 0, PacketBuffer.Length, SocketFlags.None, RetrieveCallback, null);
        }