コード例 #1
0
ファイル: Server.cs プロジェクト: Stickman26/NetworkingFinal
    //if theres a change in rotation
    private void OnRotationChange(int cnnID, NetworkStructs.RotationData data)
    {
        //NetworkStructs.DataPacket packet = new NetworkStructs.DataPacket(NetworkStructs.MessageTypes.CNN, NetworkStructs.getBytes(data));

        //send it to all players except the original sender of this message
        Send(NetworkStructs.AddTag(NetworkStructs.MessageTypes.ROT, NetworkStructs.getBytes(data)), unreliableChannel, clients, cnnID);
    }
コード例 #2
0
ファイル: Server.cs プロジェクト: Stickman26/NetworkingFinal
    //when the player responds with their username
    private void OnNameIs(int cnnId, string playerName)
    {
        //Link name to connection ID
        clients.Find(x => x.connectionID == cnnId).playerName = playerName;

        NetworkStructs.NameRequestData msg = new NetworkStructs.NameRequestData(cnnId, playerName);

        //Tell everyone about new player connection
        Send(NetworkStructs.AddTag(NetworkStructs.MessageTypes.CNN, NetworkStructs.getBytes(msg)), reliableChannel, clients);
    }
コード例 #3
0
ファイル: Server.cs プロジェクト: Stickman26/NetworkingFinal
    //when a player disconnects
    private void OnDisconnection(int cnnId)
    {
        //Remove this player from client list
        clients.Remove(clients.Find(x => x.connectionID == cnnId));

        NetworkStructs.IntData msg = new NetworkStructs.IntData(cnnId);

        //Tell everyone that somebody else has disconnected
        Send(NetworkStructs.AddTag(NetworkStructs.MessageTypes.DC, NetworkStructs.getBytes(msg)), reliableChannel, clients);
    }
コード例 #4
0
ファイル: Client.cs プロジェクト: Stickman26/NetworkingFinal
    //send chat messages and admin commands
    public void sendMessage(string msg, int channelID)
    {
        if (msg.StartsWith("/"))
        {
            NetworkStructs.StringData message = new NetworkStructs.StringData(msg);
            Send(NetworkStructs.AddTag(NetworkStructs.MessageTypes.ADMIN, NetworkStructs.getBytes(message)), channelID);
        }
        else
        {
            msg = "[" + playerName + "]: " + msg;
            NetworkStructs.StringData message = new NetworkStructs.StringData(msg);

            Send(NetworkStructs.AddTag(NetworkStructs.MessageTypes.MESSAGE, NetworkStructs.getBytes(message)), channelID);
        }
    }
コード例 #5
0
ファイル: Server.cs プロジェクト: Stickman26/NetworkingFinal
    //update the position of the AI
    private void AIPositionUpdate()
    {
        //iterate through our dictionary to make sure we don't have any duplicates and to make sure each AI moves
        foreach (AI a in AIList.Values)
        {
            if (!a.switchBack)
            {
                //tells the AI to keep moving
                a.current = Vector3.MoveTowards(a.current, a.pos1, 0.01f);
                if (Vector3.Magnitude(a.current - a.pos1) < 0.1f)
                {
                    //tells the AI to swap directions
                    a.switchBack = true;
                }
            }
            else
            {
                //tells the AI to keep moving in the opposite direction of the above movement
                a.current = Vector3.MoveTowards(a.current, a.pos2, 0.01f);
                if (Vector3.Magnitude(a.current - a.pos2) < 0.1f)
                {
                    //tells the AI to swap directions again
                    a.switchBack = false;
                }
            }

            //send delay so we aren't sending it a shit ton of times per second
            if (sendDelay <= lastSent && clients.Count != 0)
            {
                lastSent = 0;

                NetworkStructs.AIMoveData msg = new NetworkStructs.AIMoveData(a.id, a.current);

                Send(NetworkStructs.AddTag(NetworkStructs.MessageTypes.MOVEAI, NetworkStructs.getBytes(msg)), unreliableChannel);
            }
            else
            {
                lastSent += Time.deltaTime;
            }
        }
    }
コード例 #6
0
ファイル: Server.cs プロジェクト: Stickman26/NetworkingFinal
    //handles the admin commands
    private void adminCommands(string msg, int cnnID)
    {
        if (msg.Contains("/setjump"))
        {
            //actually set the jump and output the change to the server log
            string number = msg.Substring(8);
            NetworkStructs.StringData data = new NetworkStructs.StringData("Setting player jump height to: " + number);
            serverConsole.AddToConsole("Setting player jump height to: " + number);
            Send(NetworkStructs.getBytes(data), reliableChannel, cnnID);

            //adds a number to the front of the message to function as a tag rather than setting up a whole new message type
            string messageToSend = "1";
            messageToSend += number;
            data           = new NetworkStructs.StringData(messageToSend);
            Send(NetworkStructs.AddTag(NetworkStructs.MessageTypes.ADMIN, NetworkStructs.getBytes(data)), reliableChannel);
        }
        else if (msg.Contains("/setspeed"))
        {
            //actually change the player speed and output the change to the server log
            string number = msg.Substring(9);
            NetworkStructs.StringData data = new NetworkStructs.StringData("Setting player move speed to: " + number);
            serverConsole.AddToConsole("Setting player move speed to: " + number);
            Send(NetworkStructs.getBytes(data), reliableChannel, cnnID);

            //adds a number to the front of the message to function as a tag rather than setting up a whole new message type
            string messageToSend = "2";
            messageToSend += number;
            data           = new NetworkStructs.StringData(messageToSend);
            Send(NetworkStructs.AddTag(NetworkStructs.MessageTypes.ADMIN, NetworkStructs.getBytes(data)), reliableChannel);
        }
        else
        {
            //send message to the server logs that the command doesn't exist, along with what the passed in command was
            NetworkStructs.StringData data = new NetworkStructs.StringData("Command: " + msg + " :Doesn't exist!");
            serverConsole.AddToConsole("Command: " + msg + " :Doesn't exist!");

            //this will send to just the player who sent the commnad (i think)
            Send(NetworkStructs.getBytes(data), reliableChannel, cnnID);
        }
    }
コード例 #7
0
ファイル: Client.cs プロジェクト: Stickman26/NetworkingFinal
    //when the server pings the player for their name
    private void OnAskName(NetworkStructs.NameRequestData data)
    {
        //set this clients ID
        ourClientId = data.id;

        NetworkStructs.StringData msg = new NetworkStructs.StringData(playerName);

        //Send our name to the server
        Send(NetworkStructs.AddTag(NetworkStructs.MessageTypes.NAMEIS, NetworkStructs.getBytes(msg)), reliableChannel);

        string[] connectionList = data.str.Split('|');

        //Create all the other players
        for (int i = 0; i < connectionList.Length; ++i)
        {
            string[] d = connectionList[i].Split('%');
            if (int.Parse(d[1]) != ourClientId)
            {
                SpawnPlayer(d[0], int.Parse(d[1]));
            }
        }
    }
コード例 #8
0
ファイル: Server.cs プロジェクト: Stickman26/NetworkingFinal
    //when a player connects
    private void OnConnection(int cnnId)
    {
        // add connection to list
        ServerClient c = new ServerClient();

        c.connectionID = cnnId;
        c.playerName   = "TEMP";
        clients.Add(c);

        // when player joins give ID
        // request name and send name of other players
        string list = "";

        foreach (ServerClient sc in clients)
        {
            list += sc.playerName + '%' + sc.connectionID + "|";
        }
        list = list.Trim('|');

        NetworkStructs.NameRequestData msg = new NetworkStructs.NameRequestData(cnnId, list);

        Send(NetworkStructs.AddTag(NetworkStructs.MessageTypes.ASKNAME, NetworkStructs.getBytes(msg)), reliableChannel, cnnId);
    }
コード例 #9
0
ファイル: Server.cs プロジェクト: Stickman26/NetworkingFinal
    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();
    }
コード例 #10
0
ファイル: Server.cs プロジェクト: Stickman26/NetworkingFinal
 //if theres a change in position
 private void OnMovement(int cnnID, NetworkStructs.PositionVelocityData data)
 {
     //send it to all players except the original sender of this message
     Send(NetworkStructs.AddTag(NetworkStructs.MessageTypes.MOVE, NetworkStructs.getBytes(data)), unreliableChannel, clients, cnnID);
 }
コード例 #11
0
ファイル: Client.cs プロジェクト: Stickman26/NetworkingFinal
    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;
        }
    }
コード例 #12
0
ファイル: Client.cs プロジェクト: Stickman26/NetworkingFinal
    //setup the AI that are running on the local machine
    public void SetupAI(int id, Vector3 pos1, Vector3 pos2)
    {
        NetworkStructs.AIInitialMoveData msg = new NetworkStructs.AIInitialMoveData(id, pos1, pos2);

        Send(NetworkStructs.AddTag(NetworkStructs.MessageTypes.SETUPAI, NetworkStructs.getBytes(msg)), reliableChannel);
    }
コード例 #13
0
ファイル: Client.cs プロジェクト: Stickman26/NetworkingFinal
    //send the local player's movement
    public void SendMovement(Vector3 pos, Vector3 vel)
    {
        NetworkStructs.PositionVelocityData msg = new NetworkStructs.PositionVelocityData(ourClientId, pos, vel);

        Send(NetworkStructs.AddTag(NetworkStructs.MessageTypes.MOVE, NetworkStructs.getBytes(msg)), unreliableChannel);
    }
コード例 #14
0
ファイル: Client.cs プロジェクト: Stickman26/NetworkingFinal
    //send the local player's rotation
    public void SendRotation(float xRot, float yRot)
    {
        NetworkStructs.RotationData msg = new NetworkStructs.RotationData(ourClientId, xRot, yRot);

        Send(NetworkStructs.AddTag(NetworkStructs.MessageTypes.ROT, NetworkStructs.getBytes(msg)), unreliableChannel);
    }