Exemple #1
0
        public void ReceiveNextMessage(bool skipConnectingEvents, ISocket connection, ISynchroMessage targetMessage)
        {
            try
            {
                Buffer.Resize(inBuf, sizeof(int));
                AweSock.ReceiveMessage(connection, inBuf);
                int messageLength = Buffer.Get <int>(inBuf);

                Buffer.Resize(inBuf, messageLength);
                int received = 0;

                do
                {
                    received += AweSock.ReceiveMessage(connection, inBuf, received);
                    if (received == 0)
                    {
                        throw new SocketException();
                    }
                } while (received < messageLength);

                targetMessage.Deserialize(inBuf);
            }
            catch (SocketException)
            {
                connections.Remove(connection);
            }
        }
        private bool ValidateResponse(Buffer receiveBuffer)
        {
            const float tolerance = 0.001F;

            return(Buffer.Get <int>(receiveBuffer) == 10 && Math.Abs(Buffer.Get <float>(receiveBuffer) - 20.0F) < tolerance && Math.Abs(Buffer.Get <double>(receiveBuffer) - 40.0) < tolerance && Buffer.Get <char>(receiveBuffer) == 'A' &&
                   Buffer.Get <string>(receiveBuffer) == "The quick brown fox jumped over the lazy dog" && Buffer.Get <byte>(receiveBuffer) == 255);
        }
Exemple #3
0
        private void ListenForMessages(ISocket socket)
        {
            var callbackFired = new ManualResetEventSlim(false);
            var receiveBuffer = ASBuffer.New();

            while (!CancelToken.IsCancellationRequested)
            {
                ASBuffer.ClearBuffer(receiveBuffer);
                Tuple <int, EndPoint> result = null;
                try
                {
                    AweSock.ReceiveMessage(socket, receiveBuffer, AwesomeSockets.Domain.SocketCommunicationTypes.NonBlocking, (b, endPoint) =>
                    {
                        result = new Tuple <int, EndPoint>(b, endPoint); callbackFired.Set();
                    });
                }
                catch (ArgumentOutOfRangeException)
                { // Swallow the exception caused by AweSock's construction of an invalid endpoint
                }
                try
                {
                    callbackFired.Wait(CancelToken);
                }
                catch (OperationCanceledException)
                {
                }
                if (!CancelToken.IsCancellationRequested)
                {
                    callbackFired.Reset();
                    ASBuffer.FinalizeBuffer(receiveBuffer);
                    if (result.Item1 == 0)
                    {
                        return;
                    }

                    var length = ASBuffer.Get <short>(receiveBuffer);
                    var bytes  = new byte[length];
                    ASBuffer.BlockCopy(ASBuffer.GetBuffer(receiveBuffer), sizeof(short), bytes, 0, length);
                    MessageReceived?.Invoke(this, new MessageReceivedEventArgs()
                    {
                        Message = bytes
                    });
                }
            }
        }
Exemple #4
0
        public void Deserialize(Buffer buffer)
        {
            int length = Buffer.Get <int>(buffer);

            for (int i = 0; i < length; i++)
            {
                StoredAxis axis = new StoredAxis();
                axis.id    = Buffer.Get <int>(buffer);
                axis.value = Buffer.Get <float>(buffer);
                axes.Add(axis);
            }

            length = Buffer.Get <int>(buffer);
            for (int i = 0; i < length; i++)
            {
                StoredInput input = new StoredInput();
                input.id        = Buffer.Get <int>(buffer);
                input.inputType = Buffer.Get <short>(buffer);
                buttons.Add(input);
            }
        }
Exemple #5
0
 private bool ValidateResponse(Buffer receiveBuffer)
 {
     return((Buffer.Get <int>(receiveBuffer) == 10) && (Buffer.Get <float>(receiveBuffer) == 20.0F) && (Buffer.Get <double>(receiveBuffer) == 40.0) && (Buffer.Get <char>(receiveBuffer) == 'A') &&
            (Buffer.Get <string>(receiveBuffer) == "The quick brown fox jumped over the lazy dog") && (Buffer.Get <byte>(receiveBuffer) == ((byte)255)));
 }
Exemple #6
0
        /// <summary>
        /// This method is called by the thread.
        /// </summary>
        public void Update()
        {
            while (this.mIsRunning)
            {
                // Check if the server must send data.
                if (this.mServer.PendingCommands.Any())
                {
                    // Clear all buffers.
                    foreach (var lNetBuffer in this.mNetBuffers)
                    {
                        AweBuffer.ClearBuffer(lNetBuffer.Value);
                    }

                    // Encode all commands.
                    foreach (var lCommand in this.mServer.PendingCommands)
                    {
                        if (string.IsNullOrWhiteSpace(lCommand.Value.ClientId))
                        {
                            foreach (var lClientView in this.mServer.Clients)
                            {
                                if (string.IsNullOrWhiteSpace(lClientView.Value.Id) == false)
                                {
                                    if (this.mNetBuffers.ContainsKey(lClientView.Value.Id) == false)
                                    {
                                        this.mNetBuffers.Add(lClientView.Value.Id, AweBuffer.New());
                                    }

                                    lCommand.Value.HasBeenEncoded = true;
                                    AweBuffer.Add(this.mNetBuffers[lClientView.Value.Id], lCommand.Value.Encode() + ";");
                                }
                            }
                        }
                        else
                        {
                            if (this.mNetBuffers.ContainsKey(lCommand.Value.ClientId) == false)
                            {
                                this.mNetBuffers.Add(lCommand.Value.ClientId, AweBuffer.New());
                            }
                            lCommand.Value.HasBeenEncoded = true;
                            AweBuffer.Add(this.mNetBuffers[lCommand.Value.ClientId], lCommand.Value.Encode() + ";");
                        }
                    }

                    foreach (var lNetBuffer in this.mNetBuffers)
                    {
                        AweBuffer.FinalizeBuffer(lNetBuffer.Value);
                    }

                    // Send data to each client.
                    bool lErrorOccured = false;
                    foreach (var lNetBuffer in this.mNetBuffers)
                    {
                        var lClient = this.mServer.Clients.FirstOrDefault(pClient => pClient.Value.Id == lNetBuffer.Key);
                        if (lClient.Value != null && lClient.Value.Status != Status.Lost)
                        {
                            try
                            {
                                AweSock.SendMessage(lClient.Value.Socket, lNetBuffer.Value);
                            }
                            catch
                            {
                                lErrorOccured = true;
                                Console.WriteLine("Lost client");
                                this.mServer.LostClient(lClient.Value);
                            }
                        }
                    }

                    // Notifies all commands.
                    if (lErrorOccured == false)
                    {
                        foreach (var lCommand in this.mServer.PendingCommands)
                        {
                            if (lCommand.Value.HasBeenEncoded)
                            {
                                this.mServer.NotifyCommandSent(lCommand.Value);
                            }
                        }
                    }
                }

                // Try to receive some data.

                foreach (var lClientView in this.mServer.Clients)
                {
                    try
                    {
                        StringBuilder lInBuffer = new StringBuilder();
                        if (lClientView.Value.Socket != null && lClientView.Value.Socket.GetBytesAvailable() != 0)
                        {
                            AweBuffer.ClearBuffer(this.mInBuffer);
                            Tuple <int, EndPoint> lReceived = AweSock.ReceiveMessage(lClientView.Value.Socket, this.mInBuffer);
                            if (lReceived.Item1 != 0)
                            {
                                lInBuffer.Append(AweBuffer.Get <string>(this.mInBuffer));

                                if (lInBuffer.Length != 0)
                                {
                                    this.mServer.Decode(lInBuffer.ToString(), lClientView.Value);
                                }
                            }
                        }
                    }
                    catch
                    {
                        Console.WriteLine("Lost client");
                        this.mServer.LostClient(lClientView.Value);
                    }
                }


                //Console.WriteLine("[SERVER] Sleep");
                Thread.Sleep(1000);
            }

            //Console.WriteLine("[SERVER] LeaveThread");
        }
Exemple #7
0
        /// <summary>
        /// This method is called by the thread.
        /// </summary>
        public void Update()
        {
            while (this.mIsRunning)
            {
                if (this.mClient.Socket != null)
                {
                    // Clear output buffers.
                    AweBuffer.ClearBuffer(this.mOutBuffer);
                    AweBuffer.ClearBuffer(this.mInBuffer);

                    // Encode all commands.
                    StringBuilder lBuffer = new StringBuilder();
                    foreach (var lCommand in this.mClient.PendingCommands)
                    {
                        lCommand.ClientId = this.mClient.Id;
                        AweBuffer.Add(this.mOutBuffer, lCommand.Encode());
                        lBuffer.AppendLine(";"); // End of command.
                        lCommand.HasBeenEncoded = true;
                    }

                    AweBuffer.FinalizeBuffer(this.mOutBuffer);

                    // Send data to the server.
                    try
                    {
                        AweSock.SendMessage(this.mClient.Socket, this.mOutBuffer);
                    }
                    catch (Exception lEx)
                    {
                        this.mClient.LostServer();
                        break;
                    }

                    // Notifies all commands.
                    foreach (var lCommand in this.mClient.PendingCommands)
                    {
                        if (lCommand.HasBeenEncoded)
                        {
                            this.mClient.NotifyCommandSent(lCommand);
                        }
                    }

                    this.mClient.PendingCommands.Clear();

                    // Try to receive some data.
                    StringBuilder lInBuffer = new StringBuilder();
                    try
                    {
                        if (this.mClient.Socket.GetBytesAvailable() != 0)
                        {
                            Tuple <int, EndPoint> lReceived = AweSock.ReceiveMessage(this.mClient.Socket, this.mInBuffer);
                            if (lReceived.Item1 == 0)
                            {
                                lInBuffer.Append(AweBuffer.Get <string>(this.mInBuffer));
                            }
                        }
                    }
                    catch (Exception lEx)
                    {
                        this.mClient.LostServer();
                        break;
                    }

                    if (lInBuffer.Length != 0)
                    {
                        this.mClient.Decode(lInBuffer.ToString());
                    }
                }


                Console.WriteLine("[" + this.mClient.Id + "] Sleep");
                Thread.Sleep(1000);
            }

            Console.WriteLine("[" + this.mClient.Id + "] LeaveThread");
        }