void Start()
    {
        Valve.Sockets.Library.Initialize();

        Instance = this;

        NetworkingUtils utils = new NetworkingUtils();

        utils.SetDebugCallback(DebugType.Everything, DebugMessageCallback);
    }
示例#2
0
    void Awake()
    {
        utils.SetDebugCallback(DebugType.Everything, debug);
        client  = new NetworkingSockets();
        address = new Address();
        address.SetAddress("::1", clientPort); //ipv6 localhost
        connection = client.Connect(address);

        InitCallbacks();
        StartCoroutine(HandleClient());
    }
        // Called when player connects to server
        private void ConnectionCallback()
        {
            this.debugWriter.WriteLine("Successfully connected to server");
            isConnected = true;

            debugCallbackDelegate = (type, message) => {
                if (this.debugWriter != null)
                {
                    this.debugWriter.WriteLine("Valve Debug - Type: {0}, Message: {1}", type, message);
                }
            };

            NetworkingUtils utils = new NetworkingUtils();

            utils.SetDebugCallback(DebugType.Important, debugCallbackDelegate);

            Main.utilityMenu.chat = "";
            Main.utilityMenu.previousMessageCount = 0;

            unsafe {
#if DEBUG
                // From data I saw a typical example of udp packets over the internet would be 0.3% loss, 25% reorder
                // For testing I'll use 25% reorder, 30ms delay on reorder, 150ms ping, and 0.2% loss

                // Re-order 25% of packets and add 30ms delay on reordered packets
                //float reorderPercent = 25f;
                //int reorderTime = 30;
                //utils.SetConfiguratioValue(ConfigurationValue.FakePacketReorderSend, ConfigurationScope.Global, IntPtr.Zero, ConfigurationDataType.Float, new IntPtr(&reorderPercent));
                //utils.SetConfiguratioValue(ConfigurationValue.FakePacketReorderTime, ConfigurationScope.Global, IntPtr.Zero, ConfigurationDataType.Int32, new IntPtr(&reorderTime));

                //// Fake 150ms ping
                //int pingTime = 150;
                //utils.SetConfiguratioValue(ConfigurationValue.FakePacketLagSend, ConfigurationScope.Global, IntPtr.Zero, ConfigurationDataType.Int32, new IntPtr(&pingTime));

                //// Simulate 0.2% packet loss
                //float lossPercent = 0.2f;
                //utils.SetConfiguratioValue(ConfigurationValue.FakePacketLossSend, ConfigurationScope.Global, IntPtr.Zero, ConfigurationDataType.Float, new IntPtr(&lossPercent));
#endif

                int sendRateMin    = 0;
                int sendRateMax    = 209715200;
                int sendBufferSize = 10485760;
                utils.SetConfigurationValue(ConfigurationValue.SendRateMin, ConfigurationScope.Global, IntPtr.Zero, ConfigurationDataType.Int32, new IntPtr(&sendRateMin));
                utils.SetConfigurationValue(ConfigurationValue.SendRateMax, ConfigurationScope.Global, IntPtr.Zero, ConfigurationDataType.Int32, new IntPtr(&sendRateMax));
                utils.SetConfigurationValue(ConfigurationValue.SendBufferSize, ConfigurationScope.Global, IntPtr.Zero, ConfigurationDataType.Int32, new IntPtr(&sendBufferSize));
            }
        }
    void  Start()
    {
        Library.Initialize();

        ushort port = 8080;

        this.client = new NetworkingSockets();
        Address address = new Address();

        address.SetAddress("::1", port);

        this.connection = client.Connect(ref address);

        this.status = (info, context) => {
            switch (info.connectionInfo.state)
            {
            case ConnectionState.None:
                break;

            case ConnectionState.Connected:
                Debug.Log("Client connected to server - ID: " + connection);
                break;

            case ConnectionState.ClosedByPeer:
                client.CloseConnection(connection);
                Debug.Log("Client disconnected from server");
                break;

            case ConnectionState.ProblemDetectedLocally:
                client.CloseConnection(connection);
                Debug.Log("Client unable to connect");
                break;
            }
        };

        DebugCallback debug = (type, message) => {
            Debug.Log("Debug - Type: " + type + ", Message: " + message);
        };

        NetworkingUtils utils = new NetworkingUtils();

        utils.SetDebugCallback(DebugType.Everything, debug);
    }
示例#5
0
    void StartServer()
    {
        Debug.Log("Starting server...");

        utils = new NetworkingUtils();
        utils.SetDebugCallback(DebugType.Message, debug);

        status = OnServerStatusUpdate;
        utils.SetStatusCallback(status);

        server = new NetworkingSockets();

        Address address = new Address();

        address.SetAddress("::0", port);

        listenSocket = server.CreateListenSocket(ref address);

        //connectedPollGroup = server.CreatePollGroup();
    }
示例#6
0
    public void ConnectToServer(string username, OnConnect onConnect, OnDisconnect onDisconnect, OnReject onReject)
    {
        this.onConnect    = onConnect;
        this.onDisconnect = onDisconnect;
        this.onReject     = onReject;

        utils = new NetworkingUtils();
        utils.SetDebugCallback(DebugType.Message, debug);

        Reset();

        client = new NetworkingSockets();

        Address address = new Address();

        address.SetAddress(serverIP, serverport);

        status = OnClientStatusUpdate;
        utils.SetStatusCallback(status);

        connection = client.Connect(ref address);
    }
示例#7
0
        public static void ServerLoop()
        {
            Library.Initialize();

            server = new NetworkingSockets();
            Address address = new Address();

            NetworkingUtils utils = new NetworkingUtils();

            utils.SetDebugCallback(DebugType.Important, (type, message) => {
                Console.WriteLine("Valve Debug - Type: {0}, Message: {1}", type, message);
            });

            unsafe {
                int sendRateMin    = 5 * 1024 * 1024;
                int sendRateMax    = MAX_UPLOAD;
                int sendBufferSize = MAX_BUFFER;
                utils.SetConfigurationValue(ConfigurationValue.SendRateMin, ConfigurationScope.Global, IntPtr.Zero, ConfigurationDataType.Int32, new IntPtr(&sendRateMin));
                utils.SetConfigurationValue(ConfigurationValue.SendRateMax, ConfigurationScope.Global, IntPtr.Zero, ConfigurationDataType.Int32, new IntPtr(&sendRateMax));
                utils.SetConfigurationValue(ConfigurationValue.SendBufferSize, ConfigurationScope.Global, IntPtr.Zero, ConfigurationDataType.Int32, new IntPtr(&sendBufferSize));
            }

            address.SetAddress("::0", port);

            uint listenSocket = server.CreateListenSocket(ref address);
            uint pollGroup    = server.CreatePollGroup();

            Console.WriteLine($"Server {SERVER_NAME} started Listening on port {port} for maximum of {MAX_PLAYERS} players\nEnforcing maps is {ENFORCE_MAPS}");

            StartAnnouncing();

            StatusCallback status = (ref StatusInfo info, IntPtr context) => {
                switch (info.connectionInfo.state)
                {
                case ConnectionState.None:
                    break;

                case ConnectionState.Connecting:
                    server.AcceptConnection(info.connection);
                    server.SetConnectionPollGroup(pollGroup, info.connection);
                    break;

                case ConnectionState.Connected:
                    Console.WriteLine("Client connected - IP: " + info.connectionInfo.address.GetIP());

                    bool openSlot = false;

                    for (byte i = 0; i < MAX_PLAYERS; i++)
                    {
                        if (players[i] == null)
                        {
                            players[i] = new Player(i, info.connection, info.connectionInfo.address);

                            byte[] versionNumber  = ASCIIEncoding.ASCII.GetBytes(VERSION_NUMBER);
                            byte[] versionMessage = new byte[versionNumber.Length + 1];

                            versionMessage[0] = (byte)OpCode.VersionNumber;
                            Array.Copy(versionNumber, 0, versionMessage, 1, versionNumber.Length);
                            server.SendMessageToConnection(players[i].connection, versionMessage, SendFlags.Reliable | SendFlags.NoNagle);

                            if (ENFORCE_MAPS)
                            {
                                server.SendMessageToConnection(players[i].connection, mapListBytes, SendFlags.Reliable);
                                server.SendMessageToConnection(players[i].connection, GetCurrentMapHashMessage(), SendFlags.Reliable);
                            }

                            foreach (Player player in players)
                            {
                                if (player != null && player != players[i])
                                {
                                    server.SendMessageToConnection(players[i].connection, new byte[] { (byte)OpCode.Connect, player.playerID }, SendFlags.Reliable);
                                    server.SendMessageToConnection(player.connection, new byte[] { (byte)OpCode.Connect, i }, SendFlags.Reliable);

                                    if (player.usernameMessage != null)
                                    {
                                        server.SendMessageToConnection(players[i].connection, player.usernameMessage, SendFlags.Reliable);
                                    }
                                    if (player.allGearUploaded)
                                    {
                                        foreach (KeyValuePair <string, byte[]> value in player.gear)
                                        {
                                            server.SendMessageToConnection(players[i].connection, value.Value, SendFlags.Reliable);
                                        }
                                    }
                                }
                            }

                            server.FlushMessagesOnConnection(players[i].connection);

                            openSlot = true;
                            break;
                        }
                    }

                    if (!openSlot)
                    {
                        server.CloseConnection(info.connection);
                    }
                    break;

                case ConnectionState.ClosedByPeer:
                    RemovePlayer(info.connection);
                    break;
                }
            };

#if VALVESOCKETS_SPAN
            MessageCallback messageCallback = (in NetworkingMessage netMessage) => {
                byte[] messageData = new byte[netMessage.length];
                netMessage.CopyTo(messageData);

                Player sendingPlayer = null;
                foreach (Player player in players)
                {
                    if (player != null && player.connection == netMessage.connection)
                    {
                        sendingPlayer = player;
                        break;
                    }
                }

                if (sendingPlayer != null)
                {
                    ProcessMessage(messageData, sendingPlayer.playerID, server);
                }
            };
#else
            const int maxMessages = 256;

            NetworkingMessage[] netMessages = new NetworkingMessage[maxMessages];
#endif
            while (RUNNING)
            {
                server.DispatchCallback(status);

#if VALVESOCKETS_SPAN
                server.ReceiveMessagesOnPollGroup(pollGroup, messageCallback, 256);
#else
                int netMessagesCount = server.ReceiveMessagesOnConnection(listenSocket, netMessages, maxMessages);

                if (netMessagesCount > 0)
                {
                    for (int i = 0; i < netMessagesCount; i++)
                    {
                        ref NetworkingMessage netMessage = ref netMessages[i];

                        byte[] messageData = new byte[netMessage.length];
                        netMessage.CopyTo(messageData);

                        Player sendingPlayer = null;
                        foreach (Player player in players)
                        {
                            if (player != null && player.connection == netMessage.connection)
                            {
                                sendingPlayer = player;
                                break;
                            }
                        }

                        //Console.WriteLine("Recieved packet from connection {0}, sending player null: {1}", netMessage.connection, sendingPlayer == null);

                        if (sendingPlayer != null)
                        {
                            ProcessMessage(messageData, sendingPlayer.playerID, server);
                        }

                        netMessage.Destroy();
                    }
                }
#endif

                mapVotes.Clear();
                total_players = 0;
                foreach (Player player in players)
                {
                    if (player != null)
                    {
                        total_players++;

                        if (player.timeoutWatch.ElapsedMilliseconds > 15000)
                        {
                            Console.WriteLine($"{player.playerID} has been timed out for not responding for 15 seconds");

                            RemovePlayer(player.connection, player.playerID, true);
                        }

                        if (!mapVotes.ContainsKey(player.currentVote))
                        {
                            mapVotes.Add(player.currentVote, 1);
                        }
                        else
                        {
                            mapVotes[player.currentVote]++;
                        }
                    }
                }

                // Handle map voting and map enforcement
                if (ENFORCE_MAPS)
                {
                    if (total_players == 0)
                    {
                        currentMapHash = "1";
                    }

                    bool startNewTimer = false;
                    if (mapVotes.ContainsKey("current"))
                    {
                        if (mapVotes["current"] < (int)Math.Ceiling((float)total_players / 2))
                        {
                            if (!mapVoteTimer.IsRunning)
                            {
                                startNewTimer = true;
                            }
                        }
                    }
                    else if (!mapVoteTimer.IsRunning && total_players > 0)
                    {
                        startNewTimer = true;
                    }

                    if (startNewTimer)
                    {
                        mapVoteTimer.Restart();

                        byte[] mapVoteMsg = new byte[] { (byte)OpCode.MapVote, 0, 0 };

                        foreach (Player player in players)
                        {
                            if (player != null)
                            {
                                server.SendMessageToConnection(player.connection, mapVoteMsg, SendFlags.Reliable);
                            }
                        }
                    }

                    if (mapVoteTimer.IsRunning && mapVoteTimer.ElapsedMilliseconds > 30000 && total_players > 0)
                    {
                        mapVoteTimer.Stop();

                        Tuple <string, int> mostVoted = null;

                        foreach (var item in mapVotes)
                        {
                            if (!item.Key.Equals("current"))
                            {
                                if (mostVoted == null || mostVoted.Item2 < item.Value)
                                {
                                    mostVoted = Tuple.Create <string, int>(item.Key, item.Value);
                                }
                            }
                        }

                        currentMapHash = mostVoted.Item1;

                        byte[] newMapMessage = GetCurrentMapHashMessage();

                        foreach (Player player in players)
                        {
                            if (player != null)
                            {
                                server.SendMessageToConnection(player.connection, newMapMessage, SendFlags.Reliable);

                                player.currentVote = "current";
                            }
                        }
                    }
                    else if (total_players == 0)
                    {
                        mapVoteTimer.Stop();
                    }
                }
            }