예제 #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();
    }