Exemplo n.º 1
0
    void CalculateAndSendMovement(bool upPressed, bool rightPressed, bool leftPressed, bool downPressed)
    {
        if (!(upPressed || rightPressed || leftPressed || downPressed))
        {
            return;
        }

        ClientUpdateMessage msg = new ClientUpdateMessage();

        msg.speedScale  = upPressed ? 1 : 0;
        msg.speedScale -= downPressed ? 1 : 0;

        msg.relativeRotation = true;
        msg.rotateTo         = 0;
        if (rightPressed)
        {
            msg.rotateTo -= rotationIncrement;
        }
        if (leftPressed)
        {
            msg.rotateTo += rotationIncrement;
        }

        client.SendClientUpdateMessage(msg);
    }
Exemplo n.º 2
0
    void HandleClientUpdateMessage(MessageBase msgBase)
    {
        Debug.Log("Receiving client update");
        ClientUpdateMessage message = (ClientUpdateMessage)msgBase;

        players[message.connectionId].UpdateFromClient(message);
    }
Exemplo n.º 3
0
        public void processClientInput(ClientUpdateMessage message)
        {
            int n = -1;

            foreach (Commands commands in (message).Commands)
            {
                foreach (var cube in serverCubes)
                {
                    if (cube.Id.Equals(message.GetId))
                    {
                        cube.GameObject.transform.rotation = Quaternion.Euler(commands.rotation_x, commands.rotation_y, commands.rotation_z);
                        Vector3 move = cube.GameObject.transform.forward * commands.y
                                       + cube.GameObject.transform.right * commands.x;
                        cube.GameObject.GetComponent <CharacterController>().
                        Move(Constants.speed * Time.deltaTime * move);

                        cube.LastCommandProcessed = commands.number;
                        break;
                    }
                }

                n = commands.number;
            }
            sendClientCommandACK(n, message.GetId);
        }
Exemplo n.º 4
0
 public void UpdateFromClient(ClientUpdateMessage msg)
 {
     motor.RotateTo(msg.rotateTo);
     motor.AddVelocity(msg.speedScale);
     if (msg.firing)
     {
         gun.Fire();
     }
 }
Exemplo n.º 5
0
    // generate clientupdatemessage and send it to server
    void onNetworkTimerElapsed()
    {
        //Debug.Log("timer fired");
        if (!client || !joystick)
        {
            return;
        }

        ClientUpdateMessage message = new ClientUpdateMessage();

        message.rotateTo   = joystick.getAngle();
        message.speedScale = Mathf.Clamp01(joystick.getDistance() / joystick.BaseRadius);
        message.firing     = joystick.Firing;

        // send a message every 100ms
        client.SendClientUpdateMessage(message);
    }
Exemplo n.º 6
0
        protected override void client_UpdateEvent(MessageBusClient client)
        {
            base.client_UpdateEvent(client);

            // Also send this notification to clients.
            int[] keys;
            lock (_syncRoot)
            {
                keys = GeneralHelper.EnumerableToArray <int>(_remoteClientsNetIds.Keys);
            }

            ClientUpdateMessage message = new ClientUpdateMessage()
            {
                ClientId = client.Id, MessageId = PendingMessageId, RequestResponse = false
            };

            foreach (int key in keys)
            {
                ToClient(key, message, null);
            }
        }
Exemplo n.º 7
0
        void _messageClient_MessageReceivedEvent(SocketCommunicator helper, object message)
        {
            if (message is EnvelopeMessage)
            {
                EnvelopeMessage envelopeMessage = (EnvelopeMessage)message;

                // Remove the remote message bus index association.
                envelopeMessage.Sender.LocalMessageBusIndex = ClientId.InvalidMessageBusClientIndex;

                foreach (ClientId id in envelopeMessage.Receivers)
                {
                    // Decode the id.
                    id.LocalMessageBusIndex = base.GetClientIndexByGuid(id.Guid);

                    if (id.IsMessageBusIndexValid)
                    {
                        // Assign as a part of the local bus.
                        id.MessageBus = this;
                        if (DoSendToClient(envelopeMessage.Sender, id, envelopeMessage.Envelope, null) != SendToClientResultEnum.Success)
                        {
#if Matrix_Diagnostics
                            InstanceMonitor.OperationError(string.Format("Failed to accept envelope message [{0}].", envelopeMessage.ToString()));
#endif
                        }
                    }
                    else
                    {
#if Matrix_Diagnostics
                        InstanceMonitor.OperationError(string.Format("Failed to accept envelope message [{0}] due to unrecognized receiver id.", envelopeMessage.ToString()));
#endif
                    }
                }
            }
            else if (message is ClientsListMessage)
            {// Received client update from server.
                ClientsListMessage listMessage = (ClientsListMessage)message;

                int jef = 0;
                foreach (var client in listMessage.Ids)
                {
                    Console.WriteLine("Incoming client id: " + client + " Source type: " + listMessage.SourcesTypes[jef]);
                    jef++;
                }

                List <ClientId> existingIds = new List <ClientId>();
                lock (_syncRoot)
                {
                    existingIds.AddRange(_originalServerClientsHotSwap.Values);

                    _originalServerClientsHotSwap.Clear();
                    _originalServerClientsTypesHotSwap.Clear();
                    _originalServerClientsSourcesTypesHotNamesSwap.Clear();

                    // Preprocess Ids, by assigning them new indeces and adding to the local message bus register.
                    for (int i = 0; i < listMessage.Ids.Count; i++)
                    {
                        // Add an original copy to the list.
                        _originalServerClientsHotSwap.Add(listMessage.Ids[i].Guid, listMessage.Ids[i]);

                        _originalServerClientsTypesHotSwap.Add(listMessage.Ids[i].Guid, listMessage.Types[i]);
                        _originalServerClientsSourcesTypesHotNamesSwap.Add(listMessage.Ids[i].Guid, listMessage.SourcesTypes[i]);

                        // Add the client to a new spot.
                        //_clientsHotSwap.Add(null);
                        //int messageBusIndex = _clientsHotSwap.Count - 1;

                        // This type of assignment will also work with multiple entries.
                        // This performs an internal hotswap.
                        //_guidToIndexHotSwap[id.Guid] = messageBusIndex;

                        // Also add to this classes collection.
                        //_localToRemoteId[messageBusIndex] = id;
                    }
                }

                foreach (ClientId id in listMessage.Ids)
                {
                    existingIds.Remove(id);
                    RaiseClientAddedEvent(id);
                }

                // Raise for any that were removed.
                foreach (ClientId id in existingIds)
                {
                    RaiseClientRemovedEvent(id, true);
                }
            }
            else if (message is RequestClientListUpdateMessage)
            {
                SendClientsUpdate();
            }
            else if (message is ClientUpdateMessage)
            {
                ClientUpdateMessage updateMessage = (ClientUpdateMessage)message;

                if (_originalServerClientsHotSwap.ContainsKey(updateMessage.ClientId.Guid))
                {
                    RaiseClientUpdateEvent(updateMessage.ClientId);
                }
                else
                {
#if Matrix_Diagnostics
                    InstanceMonitor.OperationError(string.Format("Failed to raise update event for client [{0}], since client not found.", updateMessage.ClientId.ToString()));
#endif
                }
            }
            else if (message is StateUpdateMessage)
            {
                RaiseCounterPartyUpdateEvent("Server", ((StateUpdateMessage)message).State.ToString());
            }
            else
            {
#if Matrix_Diagnostics
                InstanceMonitor.Warning(string.Format("Message [{0}] not recognized.", message.GetType().Name));
#endif
            }
        }
Exemplo n.º 8
0
        void _server_ClientMessageReceivedEvent(SocketMessageServer server, SocketCommunicatorEx client, object message)
        {
            ServerAccessControl accessControl = AccessControl;

            // Check security first.
            if (accessControl != null && message is AccessMessage == false)
            {
                if (accessControl.IsAllowed(ObtainClientAccessControl(client.Id)) == false)
                {
#if Matrix_Diagnostics
                    InstanceMonitor.Info("Message [" + message.ToString() + "] from client [" + client.ToString() + "] not allowed due to access control.", TracerItem.PriorityEnum.Medium);
#endif
                    return;
                }
            }

            if (message is EnvelopeMessage)
            {// Envelope user message.
                EnvelopeMessage envelopeMessage = (EnvelopeMessage)message;

                // Remove the remote message bus index association.
                envelopeMessage.Sender.LocalMessageBusIndex = ClientId.InvalidMessageBusClientIndex;

                foreach (ClientId id in envelopeMessage.Receivers)
                {
                    // Assign the id as local id, if it is, otherwise skip it.
                    id.LocalMessageBusIndex = base.GetClientIndexByGuid(id.Guid);
                    if (id.IsMessageBusIndexValid)
                    {
                        id.MessageBus = this;
                        if (DoSendToClient(envelopeMessage.Sender, id, envelopeMessage.Envelope, null) != SendToClientResultEnum.Success)
                        {
#if Matrix_Diagnostics
                            InstanceMonitor.OperationError(string.Format("Failed to accept envelope message [{0}].", envelopeMessage.ToString()));
#endif
                        }
                    }
                    else
                    {
#if Matrix_Diagnostics
                        InstanceMonitor.OperationError(string.Format("Failed to accept envelope message [{0}] due unrecognized receiver id.", envelopeMessage.ToString()));
#endif
                    }
                }
            }
            else if (message is ClientsListMessage)
            {// Message bus system message.
                ClientsListMessage updateMessage = (ClientsListMessage)message;
                for (int i = 0; i < updateMessage.Ids.Count; i++)
                {
                    RegisterClientId(client.Id, updateMessage.Ids[i], updateMessage.Types[i], updateMessage.SourcesTypes[i]);
                }
            }
            else if (message is RequestClientListUpdateMessage)
            {
                SendClientsUpdate(client.Id);
            }
            else if (message is ClientUpdateMessage)
            {
                ClientUpdateMessage updateMessage = (ClientUpdateMessage)message;

                bool validClient;
                lock (_syncRoot)
                {
                    validClient = _remoteClientNetId.ContainsKey(updateMessage.ClientId);
                }

                if (validClient)
                {
                    RaiseClientAddedEvent(updateMessage.ClientId);
                }
                else
                {
#if Matrix_Diagnostics
                    InstanceMonitor.OperationError(string.Format("Failed to raise update event for client [{0}], since client not found.", updateMessage.ClientId.ToString()));
#endif
                }
            }
            else if (message is AccessMessage)
            {
                ClientAccessControl control = ObtainClientAccessControl(client.Id);
                if (control != null)
                {
                    control.Update(message as AccessMessage);
                }
            }
            else if (message is StateUpdateMessage)
            {
                RaiseCounterPartyUpdateEvent("Client:" + client.Id.ToString(), ((StateUpdateMessage)message).State.ToString());
            }
            else
            {
#if Matrix_Diagnostics
                InstanceMonitor.Warning(string.Format("Message [{0}] not recognized.", message.GetType().Name));
#endif
            }
        }
Exemplo n.º 9
0
 public void SendClientUpdateMessage(ClientUpdateMessage message)
 {
     message.connectionId = myConnectionId;
     NetworkUtility.Send(EMessageType.ClientUpdate, message, localHostId, unreliableChannel, myConnectionId);
 }