Example #1
0
    private void Update()
    {
        if (!isStarted)
        {
            return;
        }

        int recHostId;
        int connectionId;
        int channelId;

        byte[]           recBuffer  = new byte[1024];
        int              bufferSize = 1024;
        int              dataSize;
        byte             error;
        NetworkEventType recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);

        switch (recData)
        {
        //case NetworkEventType.Nothing: break;

        case NetworkEventType.ConnectEvent:
        {
            //Debug.Log("Player " + connectionId + " has connected");
            serverConsole.AddToConsole("Player " + connectionId + " has connected");
            OnConnection(connectionId);
        }
        break;

        case NetworkEventType.DataEvent:
        {
            NetworkStructs.MessageTypes type = (NetworkStructs.MessageTypes)recBuffer[0];

            //string msg = Encoding.Unicode.GetString(recBuffer, 0, dataSize);
            //Debug.Log("Receiving from " + connectionId + " : " + msg);
            //serverConsole.AddToConsole("Receiving from " + connectionId + " : " + msg);

            byte[] packet = new byte[1024];
            Array.Copy(recBuffer, 1, packet, 0, bufferSize - 1);

            switch (type)
            {
            case NetworkStructs.MessageTypes.NAMEIS:
            {
                NetworkStructs.StringData data = NetworkStructs.fromBytes <NetworkStructs.StringData>(packet);
                OnNameIs(connectionId, data.str);
            }
            break;

            case NetworkStructs.MessageTypes.ROT:
            {
                NetworkStructs.RotationData data = NetworkStructs.fromBytes <NetworkStructs.RotationData>(packet);
                OnRotationChange(connectionId, data);
            }
            break;

            case NetworkStructs.MessageTypes.MOVE:
            {
                NetworkStructs.PositionVelocityData data = NetworkStructs.fromBytes <NetworkStructs.PositionVelocityData>(packet);
                OnMovement(connectionId, data);
            }
            break;

            case NetworkStructs.MessageTypes.SETUPAI:
            {
                NetworkStructs.AIInitialMoveData data = NetworkStructs.fromBytes <NetworkStructs.AIInitialMoveData>(packet);
                OnAISetup(data);
            }
            break;

            case NetworkStructs.MessageTypes.MESSAGE:
                Send(recBuffer, reliableChannel);
                break;

            case NetworkStructs.MessageTypes.ADMIN:
            {
                NetworkStructs.StringData data = NetworkStructs.fromBytes <NetworkStructs.StringData>(packet);
                adminCommands(data.str, connectionId);
            }
            break;

            default:             //invalid key case
                //Debug.Log("INVALID SERVER MESSAGE : ");
                serverConsole.AddToConsole("INVALID SERVER MESSAGE : ");
                break;
            }
        }
        break;

        case NetworkEventType.DisconnectEvent:
        {
            //Debug.Log("Player " + connectionId + " has disconnected");
            serverConsole.AddToConsole("Player " + connectionId + " has disconnected");
            OnDisconnection(connectionId);
        }
        break;

        case NetworkEventType.BroadcastEvent:
        {
        }
        break;
        }

        AIPositionUpdate();
    }
Example #2
0
    private void Update()
    {
        //make sure the player is connected to the server and if they are reveal the chatbox
        if (!isConnected)
        {
            return;
        }
        else
        {
            chatBox.SetActive(true);
        }

        //message event handlers
        int recHostId;
        int connectionId;
        int channelId;

        byte[]           recBuffer  = new byte[1024];
        int              bufferSize = 1024;
        int              dataSize;
        byte             error;
        NetworkEventType recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);

        switch (recData)
        {
        case NetworkEventType.DataEvent:
        {
            NetworkStructs.MessageTypes type = (NetworkStructs.MessageTypes)recBuffer[0];

            byte[] packet = new byte[1024];
            Array.Copy(recBuffer, 1, packet, 0, bufferSize - 1);

            switch (type)
            {
            case NetworkStructs.MessageTypes.ASKNAME:             //get username
            {
                NetworkStructs.NameRequestData data = NetworkStructs.fromBytes <NetworkStructs.NameRequestData>(packet);
                OnAskName(data);
            }
            break;

            case NetworkStructs.MessageTypes.CNN:             //connection
            {
                NetworkStructs.NameRequestData data = NetworkStructs.fromBytes <NetworkStructs.NameRequestData>(packet);
                SpawnPlayer(data.str, data.id);
                //CallAIs();
                SetupAIs();
            }
            break;

            case NetworkStructs.MessageTypes.DC:             //disconnection
            {
                NetworkStructs.IntData data = NetworkStructs.fromBytes <NetworkStructs.IntData>(packet);
                PlayerDisconnected(data.data);
            }
            break;

            case NetworkStructs.MessageTypes.MOVE:
            {
                NetworkStructs.PositionVelocityData data = NetworkStructs.fromBytes <NetworkStructs.PositionVelocityData>(packet);
                MovementDetected(data);
            }
            break;

            case NetworkStructs.MessageTypes.ROT:
            {
                NetworkStructs.RotationData data = NetworkStructs.fromBytes <NetworkStructs.RotationData>(packet);
                RotationgDetected(data);
            }
            break;

            case NetworkStructs.MessageTypes.MOVEAI:
            {
                NetworkStructs.AIMoveData data = NetworkStructs.fromBytes <NetworkStructs.AIMoveData>(packet);
                AIMoveDetected(data);
            }
            break;

            case NetworkStructs.MessageTypes.MESSAGE:
            {
                NetworkStructs.StringData data = NetworkStructs.fromBytes <NetworkStructs.StringData>(packet);
                recieveMessage(data);
            }
            break;

            case NetworkStructs.MessageTypes.ADMIN:
            {
                NetworkStructs.StringData data = NetworkStructs.fromBytes <NetworkStructs.StringData>(packet);
                adminCommands(data.str, ourClientId);
            }
            break;

            default:             //invalid key case
                //Debug.Log("INVALID CLIENT MESSAGE : " + msg);
                break;
            }
        }
        break;

        case NetworkEventType.BroadcastEvent:
        {
        }
        break;
        }
    }