Example #1
0
        protected bool EndRead(IAsyncResult ar)
        {
            try
            {
                int currentlyRead = Stream.EndRead(ar);
                if (currentlyRead == 0)
                {
                    Disconnect();
                    return(false);
                }

                int size = BitConverter.ToUInt16(InMessage.Buffer, 0) + 2;
                if (size <= 0 || size >= Constants.NetworkMessageErrorSizeMax)
                {
                    Disconnect();
                    return(false);
                }

                //TODO: MAX PACKETS PER SECOND CHECK

                while (currentlyRead < size)
                {
                    if (Stream.CanRead)
                    {
                        currentlyRead += Stream.Read(InMessage.Buffer, currentlyRead, size - currentlyRead);
                    }
                    else
                    {
                        Disconnect();
                        return(false);
                    }
                }

                InMessage.Reset(2, size);
                return(true);
            }
            catch (Exception)
            {
                Disconnect();
                return(false);
            }
        }
Example #2
0
 private void BeginReceive()
 {
     message.Reset();
     socket.BeginReceive(message.Buffer, 0, 2, SocketFlags.None, ReceiveCallback, null);
 }