Exemplo n.º 1
0
        private void TryJoinToSession(ServerSideMsgPipe pipe, string nickName)
        {
            var joiningSession = _sessions.FirstOrDefault(s => s.NeedClient);

            switch (joiningSession)
            {
            case null when _sessions.Count >= _maxSessions:
                ClosePipeWithError(pipe, ServerErrorId.ServerIsBusy);
                _log?.Error($"Failed to add client {pipe.Id} into session: server is busy");
                return;

            case null:
                var sendBufferSize = _sessionPlayers * 512;
                joiningSession = new ServerSession(_random, _lastSessionId++, _sessionPlayers, _tps, sendBufferSize, _log);
                _sessions.Add(joiningSession);
                break;

            case {} session when session.HasClientWithName(nickName):
                ClosePipeWithError(pipe, ServerErrorId.NickIsBusy);

                _log?.Error($"Failed to add client {pipe.Id} into session: nickname is busy");
                return;
            }

            try {
                pipe.SendMessageUsingBuffer(new ServerMsgJoined(), _sendBuffer);
                joiningSession.AddClient(pipe, nickName);
            }
            catch (Exception e) {
                ClosePipeWithError(pipe, ServerErrorId.InternalError);
                _log?.Error($"Failed to add client {pipe.Id} into session: internal error");
                _log?.Exception(e);
            }
        }
Exemplo n.º 2
0
 private void ClosePipeWithError(ServerSideMsgPipe pipe, ServerErrorId error)
 {
     try {
         pipe.SendMessageUsingBuffer(new ServerMsgError(error), _sendBuffer);
     }
     finally {
         pipe.Close();
     }
 }
Exemplo n.º 3
0
        private void AcceptNewClients()
        {
            if (!_serverSocket.Poll(1000, SelectMode.SelectRead))
            {
                return;
            }

            var pipe = new ServerSideMsgPipe(_serverSocket.Accept(), 1024);

            _joiningPool.Add(pipe);

            _log?.Info($"Add client {pipe.Id} to join pool");
        }
Exemplo n.º 4
0
        public MsgPipeTransferTests()
        {
            var ip = IPAddress.Loopback;

            var serverSocket = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            serverSocket.Bind(new IPEndPoint(ip, 0));
            serverSocket.Listen(1);

            var clientSocket = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            clientSocket.Connect(serverSocket.LocalEndPoint);

            _serverSidePipe = new ServerSideMsgPipe(serverSocket.Accept(), 64);
            _clientSidePipe = new ClientSideMsgPipe(clientSocket, 64);
        }