static void Main(string[] args)
        {
            SslConfig sslConfig;
            TcpConfig tcpConfig = new TcpConfig(true, 5000, 20000);

            Console.WriteLine("Setting up secure server");
            sslConfig  = new SslConfig(true, "cert.pfx", "", SslProtocols.Tls12);
            _webServer = new SimpleWebServer(10000, tcpConfig, 16 * 1024, 3000, sslConfig);
            _webServer.Start(Constants.GAME_PORT);
            Console.WriteLine("Server started");

            _webServer.onConnect    += WebServerOnConnect;
            _webServer.onData       += WebServerOnData;
            _webServer.onDisconnect += WebServerOnDisconnect;

            Timer stateUpdateTimer = new Timer(1f / Constants.SERVER_TICKRATE * 1000);

            stateUpdateTimer.Elapsed  += StateUpdateTimerOnElapsed;
            stateUpdateTimer.AutoReset = true;
            stateUpdateTimer.Enabled   = true;

            while (!Console.KeyAvailable)
            {
                _webServer.ProcessMessageQueue();
            }

            Console.WriteLine("Closing server");
            _webServer.Stop();
        }
        static void Main(string[] args)
        {
            SslConfig sslConfig;
            TcpConfig tcpConfig = new TcpConfig(true, 5000, 20000);

            Console.WriteLine("Setting up secure server");
            sslConfig  = new SslConfig(true, "cert.pfx", "", SslProtocols.Tls12);
            _webServer = new SimpleWebServer(10000, tcpConfig, 16 * 1024, 3000, sslConfig);
            _webServer.Start(Constants.GAME_PORT);
            Console.WriteLine("Server started");

            _webServer.onConnect += delegate(int i) {
                Console.WriteLine($"Client {i} joined, sending hello");
                byte[] buff = System.Text.Encoding.UTF8.GetBytes("Hello from server");
                _webServer.SendOne(i, new ArraySegment <byte>(buff));
            };

            _webServer.onData += delegate(int i, ArraySegment <byte> data) {
                string msg = System.Text.Encoding.UTF8.GetString(data.Array);
                Console.WriteLine($"Client {i} send: {msg}");
            };

            while (!Console.KeyAvailable)
            {
                _webServer.ProcessMessageQueue();
            }

            Console.WriteLine("Closing server");
            _webServer.Stop();
        }
        static void Main(string[] args)
        {
            _rand = new Random(Environment.TickCount);

            SslConfig sslConfig;
            TcpConfig tcpConfig = new TcpConfig(true, 5000, 20000);

            Console.WriteLine("Setting up secure server");
            sslConfig  = new SslConfig(true, "cert.pfx", "", SslProtocols.Tls12);
            _webServer = new SimpleWebServer(10000, tcpConfig, 16 * 1024, 3000, sslConfig);
            _webServer.Start(Constants.GAME_PORT);
            Console.WriteLine("Server started");

            _webServer.onConnect    += WebServerOnConnect;
            _webServer.onData       += WebServerOnData;
            _webServer.onDisconnect += WebServerOnDisconnect;

            Timer stateUpdateTimer = new Timer(1f / Constants.SERVER_TICKRATE * 1000);

            stateUpdateTimer.Elapsed  += StateUpdateTimerOnElapsed;
            stateUpdateTimer.AutoReset = true;
            stateUpdateTimer.Enabled   = true;

            while (!Console.KeyAvailable)
            {
                _webServer.ProcessMessageQueue();

                switch (_currentState)
                {
                case GameState.Waiting:
                {
                    if (_connectedIds.Count >= 2)
                    {
                        _currentState = GameState.Begin;
                        SendStateUpdate(_currentState);
                    }

                    break;
                }

                case GameState.Begin:
                {
                    // Set timer to go to builder state
                    Timer beginTimer = new Timer(SECONDS_WAITING_IN_BEGIN * 1000);
                    beginTimer.Elapsed += delegate(Object source, ElapsedEventArgs e) {
                        _currentState = GameState.Builder;
                        SendStateUpdate(_currentState);
                    };
                    break;
                }

                case GameState.Builder:
                {
                }
                }
            }

            Console.WriteLine("Closing server");
            _webServer.Stop();
        }
Example #4
0
    // Update is called once per frame
    void LateUpdate()
    {
        _webServer.ProcessMessageQueue(this);

        if (Time.time - _timeSinceLastSend > SendInterval)
        {
            _timeSinceLastSend = Time.time;
            ushort validCount = 0; //incremented every loop where player transform isnt null

            _bitBuffer.Clear();
            _bitBuffer.AddUShort(1); // state message
            _bitBuffer.AddUShort((ushort)_playerTransforms.Count);
            foreach (var player in _playerTransforms)
            {
                if (player.Value != null)
                {
                    validCount++;
                    _bitBuffer.AddUShort(player.Key);
                    for (int i = 0; i < 8; i++)
                    {
                        _bitBuffer.AddUInt(player.Value[i]);
                    }
                }
            }

            _bitBuffer.ToArray(_bigBuffer);
            _webServer.SendAll(_connectionIds, new ArraySegment <byte>(_bigBuffer, 0, 6 + 30 * validCount));
        }
    }
Example #5
0
    private async Task ServerServer()
    {
        try
        {
            // create server instance
            var tcpConfig = new TcpConfig(noDelay: false, sendTimeout: 5000, receiveTimeout: 20000);
            var server    = new SimpleWebServer(5000, tcpConfig, ushort.MaxValue, 5000, new SslConfig());

            // listen for events
            server.onConnect    += (id) => Debug.Log($"New Client connected, id:{id}");
            server.onDisconnect += (id) => Debug.Log($"Client disconnected, id:{id}");
            server.onData       += (id, data) =>
            {
                Debug.Log($"Data from Client, id:{id}, {BitConverter.ToString(data.Array, data.Offset, data.Count)}");
                // pong
                server.SendOne(id, data);
            };
            server.onError += (id, exception) => Debug.Log($"Error because of Client, id:{id}, Error:{exception}");

            // start server listening on port 7777
            server.Start(7777);

            while (true)
            {
                server.ProcessMessageQueue();
                await Task.Yield();

                if (token.stop)
                {
                    break;
                }
            }

            token = null;
        }
        catch (Exception e)
        {
            Debug.LogException(e);
        }
    }
        static void Main(string[] args)
        {
            SslConfig sslConfig;
            TcpConfig tcpConfig = new TcpConfig(true, 5000, 20000);

            Console.WriteLine("Setting up secure server");
            sslConfig  = new SslConfig(true, "cert.pfx", "", SslProtocols.Tls12);
            _webServer = new SimpleWebServer(10000, tcpConfig, 16 * 1024, 3000, sslConfig);
            _webServer.Start(Constants.GAME_PORT);
            Console.WriteLine("Server started");

            _webServer.onConnect += WebServerOnConnect;

            _webServer.onData += WebServerOnData;

            while (!Console.KeyAvailable)
            {
                _webServer.ProcessMessageQueue();
            }

            Console.WriteLine("Closing server");
            _webServer.Stop();
        }
        static void Main(string[] args)
        {
            _rand = new Random(Environment.TickCount);

            SslConfig sslConfig;
            TcpConfig tcpConfig = new TcpConfig(true, 5000, 20000);

            Console.WriteLine("Setting up secure server");
            sslConfig  = new SslConfig(true, "cert.pfx", "", SslProtocols.Tls12);
            _webServer = new SimpleWebServer(10000, tcpConfig, 16 * 1024, 3000, sslConfig);
            _webServer.Start(Constants.GAME_PORT);
            Console.WriteLine("Server started");

            _webServer.onConnect    += WebServerOnConnect;
            _webServer.onData       += WebServerOnData;
            _webServer.onDisconnect += WebServerOnDisconnect;

            Timer stateUpdateTimer = new Timer(1f / Constants.SERVER_TICKRATE * 1000);

            stateUpdateTimer.Elapsed  += StateUpdateTimerOnElapsed;
            stateUpdateTimer.AutoReset = true;
            stateUpdateTimer.Enabled   = true;

            while (!Console.KeyAvailable)
            {
                _webServer.ProcessMessageQueue();

                // GUARD, DONT DO STATE STUFF IF WE ARE WAITING
                if (_waitingOnStateTimer)
                {
                    continue;
                }
                switch (_currentState)
                {
                case GameState.Waiting:
                {
                    if (_connectedIds.Count >= 2)
                    {
                        _currentState = GameState.Begin;
                        SendStateUpdate(_currentState);
                    }

                    break;
                }

                case GameState.Begin:
                {
                    // Set timer to go to builder state
                    Timer beginTimer = new Timer(SECONDS_WAITING_IN_BEGIN * 1000);
                    beginTimer.AutoReset = false;
                    beginTimer.Start();
                    _waitingOnStateTimer = true;

                    beginTimer.Elapsed += delegate(Object source, ElapsedEventArgs e) {
                        _waitingOnStateTimer = false;

                        _movedObjects = new List <ushort>();
                        _currentState = GameState.Builder;
                        SendStateUpdate(_currentState);
                    };
                    break;
                }

                case GameState.Builder:
                {
                    // Set timer to go to builder state
                    Timer buildTimer = new Timer(SECONDS_WAITING_IN_BUILD * 1000);
                    buildTimer.AutoReset = false;
                    buildTimer.Start();
                    _waitingOnStateTimer = true;

                    buildTimer.Elapsed += delegate(Object source, ElapsedEventArgs e) {
                        _waitingOnStateTimer = false;

                        // Reset everyones guesses
                        foreach (PlayerData playerData in _playerDatas.Values)
                        {
                            playerData.guesses.Clear();
                        }
                        _currentState = GameState.Search;
                        SendStateUpdate(_currentState);
                    };
                    break;
                }

                case GameState.Search:
                {
                    // Set timer to go to scoring state
                    Timer searchTimer = new Timer(SECONDS_WAITING_IN_SEARCH * 1000);
                    searchTimer.AutoReset = false;
                    searchTimer.Start();
                    _waitingOnStateTimer = true;

                    searchTimer.Elapsed += delegate(Object source, ElapsedEventArgs e) {
                        _waitingOnStateTimer = false;

                        _currentState = GameState.Scoring;
                        SendStateUpdate(_currentState);
                    };
                    break;
                }

                case GameState.Scoring:
                {
                    foreach (var playerPair in _playerDatas)
                    {
                    }
                }
                }
            }

            Console.WriteLine("Closing server");
            _webServer.Stop();
        }
Example #8
0
        static void Main(string[] args)
        {
            _rand = new Random(Environment.TickCount);

            SslConfig sslConfig;
            TcpConfig tcpConfig = new TcpConfig(true, 5000, 20000);

            Console.WriteLine("Setting up secure server");
            sslConfig  = new SslConfig(true, "cert.pfx", "", SslProtocols.Tls12);
            _webServer = new SimpleWebServer(10000, tcpConfig, 16 * 1024, 3000, sslConfig);
            _webServer.Start(Constants.GAME_PORT);
            Console.WriteLine("Server started");

            _webServer.onConnect    += WebServerOnConnect;
            _webServer.onData       += WebServerOnData;
            _webServer.onDisconnect += WebServerOnDisconnect;

            Timer stateUpdateTimer = new Timer(1f / Constants.SERVER_TICKRATE * 1000);

            stateUpdateTimer.Elapsed  += StateUpdateTimerOnElapsed;
            stateUpdateTimer.AutoReset = true;
            stateUpdateTimer.Enabled   = true;

            while (!Console.KeyAvailable)
            {
                _webServer.ProcessMessageQueue();

                // GUARD, DONT DO STATE STUFF IF WE ARE WAITING
                if (_waitingOnStateTimer)
                {
                    continue;
                }
                switch (_currentState)
                {
                case GameState.Waiting:
                {
                    if (_handshakenClientCount >= 2 && _connectedIds.Count >= 2)
                    {
                        _currentState = GameState.Begin;
                        SendStateUpdate(_currentState);
                    }

                    break;
                }

                case GameState.Begin:
                {
                    // Set timer to go to builder state
                    beginTimer           = new Timer(SECONDS_WAITING_IN_BEGIN * 1000);
                    beginTimer.AutoReset = false;
                    beginTimer.Start();
                    _waitingOnStateTimer = true;

                    beginTimer.Elapsed += delegate(Object source, ElapsedEventArgs e) {
                        _waitingOnStateTimer = false;

                        _movedObjects = new Dictionary <ushort, Tuple <ushort, ushort> >();
                        _currentState = GameState.Builder;
                        SendStateUpdate(_currentState);
                    };
                    break;
                }

                case GameState.Builder:
                {
                    // Set timer to go to builder state
                    buildTimer           = new Timer(SECONDS_WAITING_IN_BUILD * 1000);
                    buildTimer.AutoReset = false;
                    buildTimer.Start();
                    _waitingOnStateTimer = true;

                    buildTimer.Elapsed += delegate(Object source, ElapsedEventArgs e) {
                        _waitingOnStateTimer = false;

                        // Reset everyones guesses
                        foreach (PlayerData playerData in _playerDatas.Values)
                        {
                            playerData.guesses.Clear();
                        }

                        // Make sure builder actually moved something,
                        // if he didnt, goto scoring and give everyone else a point
                        if (_movedObjects.Count > 0)
                        {
                            _currentState = GameState.Search;
                        }
                        else
                        {
                            foreach (PlayerData data in _playerDatas.Values)
                            {
                                if (data.id != _builderId)
                                {
                                    data.points += 1;
                                }
                                _playerDatas[_builderId].points -= (ushort)_handshakenClientCount;
                            }
                            _currentState = GameState.Scoring;
                        }

                        SendStateUpdate(_currentState);
                    };
                    break;
                }

                case GameState.Search:
                {
                    // Set timer to go to scoring state
                    searchTimer           = new Timer(SECONDS_WAITING_IN_SEARCH * 1000);
                    searchTimer.AutoReset = false;
                    searchTimer.Start();
                    _waitingOnStateTimer = true;

                    searchTimer.Elapsed += delegate(Object source, ElapsedEventArgs e) {
                        _waitingOnStateTimer = false;

                        _currentState = GameState.Scoring;
                        SendStateUpdate(_currentState);
                    };
                    break;
                }

                case GameState.Scoring:
                {
                    // Set timer to wait for points to come in from clients
                    scoringTimer           = new Timer(SECONDS_WAITING_IN_SCORING * 1000);
                    scoringTimer.AutoReset = false;
                    scoringTimer.Start();
                    _waitingOnStateTimer = true;

                    scoringTimer.Elapsed += delegate(Object source, ElapsedEventArgs e) {
                        _waitingOnStateTimer = false;

                        // Tell everyone everyones scores
                        _bitBuffer.Clear();
                        _bitBuffer.AddByte(7);
                        _bitBuffer.AddUShort((ushort)_playerDatas.Count);

                        foreach (PlayerData data in _playerDatas.Values)
                        {
                            _bitBuffer.AddUShort(data.id);
                            _bitBuffer.AddShort(data.points);
                        }

                        _bitBuffer.ToArray(_buffer);
                        _webServer.SendAll(_connectedIds, new ArraySegment <byte>(_buffer, 0, 3 + 4 * _playerDatas.Count));

                        if (_handshakenClientCount >= 2 && _connectedIds.Count >= 2)
                        {
                            _currentState = GameState.Begin;
                        }
                        else
                        {
                            _currentState = GameState.Waiting;
                        }
                        SendStateUpdate(_currentState);
                    };

                    break;
                }
                }
            }

            Console.WriteLine("Closing server");
            _webServer.Stop();
        }
Example #9
0
        static void Main(string[] args)
        {
            _rand = new Random(Environment.TickCount);

            SslConfig sslConfig;
            TcpConfig tcpConfig = new TcpConfig(true, 5000, 20000);

            Console.WriteLine("Setting up secure server");
            sslConfig  = new SslConfig(true, "cert.pfx", "", SslProtocols.Tls12);
            _webServer = new SimpleWebServer(10000, tcpConfig, 16 * 1024, 3000, sslConfig);
            _webServer.Start(Constants.GAME_PORT);
            Console.WriteLine("Server started");

            _webServer.onConnect    += WebServerOnConnect;
            _webServer.onData       += WebServerOnData;
            _webServer.onDisconnect += WebServerOnDisconnect;

            Timer stateUpdateTimer = new Timer(1f / Constants.SERVER_TICKRATE * 1000);

            stateUpdateTimer.Elapsed  += StateUpdateTimerOnElapsed;
            stateUpdateTimer.AutoReset = true;
            stateUpdateTimer.Enabled   = true;

            while (!Console.KeyAvailable)
            {
                _webServer.ProcessMessageQueue();

                // GUARD, DONT DO STATE STUFF IF WE ARE WAITING
                if (_waitingOnStateTimer)
                {
                    continue;
                }
                switch (_currentState)
                {
                case GameState.Waiting:
                {
                    if (_connectedIds.Count >= 2)
                    {
                        _currentState = GameState.Begin;
                        SendStateUpdate(_currentState);
                    }

                    break;
                }

                case GameState.Begin:
                {
                    // Set timer to go to builder state
                    beginTimer           = new Timer(SECONDS_WAITING_IN_BEGIN * 1000);
                    beginTimer.AutoReset = false;
                    beginTimer.Start();
                    _waitingOnStateTimer = true;

                    beginTimer.Elapsed += delegate(Object source, ElapsedEventArgs e) {
                        _waitingOnStateTimer = false;

                        _movedObjects = new List <ushort>();
                        _currentState = GameState.Builder;
                        SendStateUpdate(_currentState);
                    };
                    break;
                }

                case GameState.Builder:
                {
                    // Set timer to go to builder state
                    buildTimer           = new Timer(SECONDS_WAITING_IN_BUILD * 1000);
                    buildTimer.AutoReset = false;
                    buildTimer.Start();
                    _waitingOnStateTimer = true;

                    buildTimer.Elapsed += delegate(Object source, ElapsedEventArgs e) {
                        _waitingOnStateTimer = false;

                        // Reset everyones guesses
                        foreach (PlayerData playerData in _playerDatas.Values)
                        {
                            playerData.guesses.Clear();
                        }
                        _currentState = GameState.Search;
                        SendStateUpdate(_currentState);
                    };
                    break;
                }

                case GameState.Search:
                {
                    // Set timer to go to scoring state
                    searchTimer           = new Timer(SECONDS_WAITING_IN_SEARCH * 1000);
                    searchTimer.AutoReset = false;
                    searchTimer.Start();
                    _waitingOnStateTimer = true;

                    searchTimer.Elapsed += delegate(Object source, ElapsedEventArgs e) {
                        _waitingOnStateTimer = false;

                        _currentState = GameState.Scoring;
                        SendStateUpdate(_currentState);
                    };
                    break;
                }

                case GameState.Scoring:
                {
                    short builderPoints = 0;

                    _bitBuffer.Clear();
                    _bitBuffer.AddByte(7);
                    _bitBuffer.AddUShort((ushort)_playerDatas.Count);


                    foreach (var playerData in _playerDatas.Values)
                    {
                        // GUARD, DON'T SCORE THE BUILDER THIS WAY
                        if (playerData.id == _builderId)
                        {
                            continue;
                        }

                        // Free points for objects builder couldnt move
                        // A point for a correct guess, minus point for a wrong guess
                        int numCorrect = _movedObjects.Distinct().Intersect(playerData.guesses).Count();
                        int newPoints  = (numCorrect * 2) - playerData.guesses.Count + (NUMBER_OF_MOVEABLE_OBJECTS - _movedObjects.Count);
                        playerData.points += (short)newPoints;

                        // Builder gets a point for each player who couldnt find any differences
                        if (numCorrect == 0)
                        {
                            builderPoints += 1;
                        }

                        _bitBuffer.AddUShort(playerData.id);
                        _bitBuffer.AddShort(playerData.points);
                    }

                    _playerDatas[_builderId].points += builderPoints;
                    _bitBuffer.AddUShort((ushort)_builderId);
                    _bitBuffer.AddShort(_playerDatas[_builderId].points);

                    _bitBuffer.ToArray(_buffer);
                    _webServer.SendAll(_connectedIds, new ArraySegment <byte>(_buffer, 0, 3 + 2 * _playerDatas.Count));

                    if (_connectedIds.Count >= 2)
                    {
                        _currentState = GameState.Begin;
                    }
                    else
                    {
                        _currentState = GameState.Waiting;
                    }
                    SendStateUpdate(_currentState);

                    break;
                }
                }
            }

            Console.WriteLine("Closing server");
            _webServer.Stop();
        }
Example #10
0
 private void Update()
 {
     server?.ProcessMessageQueue();
 }