Exemple #1
0
        /// <summary>
        /// Handles the received data
        /// </summary>
        private void Handle(State State)
        {
            if (State.Packet.Count == 0)
            {
                Logging.Info(this.GetType(), "We got disconnected by either the server or the client.");
            }
            else
            {
                if (State.Packet.Count >= 7)
                {
                    int MessageLength = BitConverter.ToInt32(new byte[1].Concat(State.Packet.Skip(2).Take(3)).Reverse().ToArray(), 0);

                    // Logging.Info(this.GetType(), "Received a message with a length of " + MessageLength + " bytes.");

                    if (State.Packet.Count >= MessageLength)
                    {
                        if (State.GetType() == typeof(ClientState))
                        {
                            this.ServerSocket.Send(new Packet(State.Packet.Take(MessageLength + 7).ToArray(), Destination.FROM_CLIENT, this.ClientSocket).RebuiltEncrypted);
                        }
                        else if (State.GetType() == typeof(ServerState))
                        {
                            this.ClientSocket.Send(new Packet(State.Packet.Take(MessageLength + 7).ToArray(), Destination.FROM_SERVER, this.ClientSocket).RebuiltEncrypted);
                        }

                        State.Packet.RemoveRange(0, MessageLength + 7);

                        if (State.Packet.Count > 0)
                        {
                            // Logging.Info(this.GetType(), "We can continue to process the packet, we have some bytes left in the buffer.");

                            if (State.Packet.Count >= 7)
                            {
                                this.Handle(State);
                            }
                            else
                            {
                                // Logging.Info(this.GetType(), "We don't have enough bytes to continue to process the packet.");
                            }
                        }
                        else
                        {
                            // Logging.Info(this.GetType(), "We successfully processed the whole buffer.");
                        }
                    }
                    else
                    {
                        // Logging.Info(this.GetType(), "We received a packet, but the buffer length is inferior to the message length.");
                    }
                }
                else
                {
                    // Logging.Info(this.GetType(), "We received a packet, but length is inferior to 7.");
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// DataReceive callback
        /// </summary>
        private void DataReceived(IAsyncResult AsyncResult)
        {
            try
            {
                State State    = (State)AsyncResult.AsyncState;
                int   Received = State.Socket.EndReceive(AsyncResult);

                State.Offset += Received;

                // Logging.Info(this.GetType(), "Received " + Received + " bytes from " + AsyncResult.AsyncState.GetType().Name + ", for a total of " + State.Offset + " bytes !");

                if (Received > 0)
                {
                    byte[] Data = new byte[State.Offset];
                    Array.Copy(State.Buffer, 0, Data, 0, State.Offset);
                    State.Offset = 0;

                    State.Packet.AddRange(Data);

                    if (State.Socket.Available == 0)
                    {
                        this.Handle(State);
                    }
                    else
                    {
                        // Logging.Info(this.GetType(), "We have nothing to receive again, aborting.");
                    }

                    State.Socket.BeginReceive(State.Buffer, State.Offset, State.BufferSize - State.Offset, 0, this.DataReceived, State);
                }
                else
                {
                    Logging.Info(this.GetType(), "We got disconnected by either the server or the client, aborting.");
                }
            }
            catch (Exception Exception)
            {
                Logging.Error(this.GetType(), Exception.GetType().Name + ", " + Exception.Message);
            }
        }