Esempio n. 1
0
        public EditorConnectionServer(int serverPort, int broadcastPort)
        {
            var service = new NetworkUtilities();

            _scheduler            = new CommandScheduler();
            _server               = new TcpConnectionServer(service.GetLocalIPAddress(), serverPort);
            _server.DataReceived += PublishReceivedData;
            _broadcastPort        = broadcastPort;
            SetupBroadcastCommand();
        }
Esempio n. 2
0
        private async void ConnectionServer_OnConnection(IConnectionServer connectionServer, IConnection connection)
        {
            TaskCompletionSource <ConnectedClient> loginTaskCompletionSource = new TaskCompletionSource <ConnectedClient>();

            connection.OnDisconnection += DisconnectionHandler;
            connection.OnRequest       += RequestHandler;
            connection.Start();

            ConnectedClient client = await loginTaskCompletionSource.Task;

            connection.OnDisconnection -= DisconnectionHandler;
            connection.OnRequest       -= RequestHandler;

            if (client == null)
            {
                connection.Stop();
            }
            else
            {
                clients.Add(connection.ID, client);

                connection.OnDisconnection += Connection_OnDisconnection;
            }

            Message RequestHandler(IConnection connection, Message message)
            {
                Message         result = null;
                ConnectedClient client = null;

                if (message is LoginMessage loginMessage)
                {
                    if (clients.Any(c => c.Value.Player.Username == loginMessage.Username))
                    {
                        result = LoginResponseMessage.FromError("User already connected");
                    }
                    else
                    {
                        client = new ConnectedClient(connection, loginMessage.Username);
                        result = LoginResponseMessage.FromSucccess(client.Player);
                    }
                }

                loginTaskCompletionSource.TrySetResult(client);
                return(result);
            }

            void DisconnectionHandler(IConnection connection, bool userDisconnection)
            {
                loginTaskCompletionSource.TrySetResult(null);
            }
        }
Esempio n. 3
0
 public virtual async Task BroadcastToAllAuthorizedUsersAsync <S>(S packet, IConnectionServer connectionSending) where S : IPacket
 {
     if (_handler != null &&
         _handler.IsServerRunning)
     {
         foreach (var identity in _connectionManager.GetAllIdentities())
         {
             foreach (var connection in identity.Connections.ToList())
             {
                 if (connection.Client.GetHashCode() != connection.Client.GetHashCode())
                 {
                     await SendToConnectionAsync(packet, connection);
                 }
             }
         }
     }
 }
Esempio n. 4
0
        public virtual async Task <bool> DisconnectConnectionAsync(IConnectionServer connection)
        {
            try
            {
                _numberOfConnections--;

                if (connection != null)
                {
                    if (connection.Client != null)
                    {
                        connection.Client.Close();
                        connection.Client.Dispose();
                    }

                    if (connection.Writer != null)
                    {
                        connection.Writer.Dispose();
                    }

                    if (connection.Reader != null)
                    {
                        connection.Reader.Dispose();
                    }

                    await FireEventAsync(this, new TcpConnectionServerEventArgs
                    {
                        ConnectionEventType = ConnectionEventType.Disconnect,
                        Connection          = connection
                    });
                }
                return(true);
            }
            catch (Exception ex)
            {
                await FireEventAsync(this, new TcpErrorServerEventArgs
                {
                    Exception  = ex,
                    Message    = ex.Message,
                    Connection = connection
                });
            }

            return(false);
        }
Esempio n. 5
0
        public virtual async Task <bool> SendToConnectionRawAsync(string message, IConnectionServer connection)
        {
            try
            {
                if (_handler != null &&
                    _handler.IsServerRunning &&
                    _connectionManager.IsConnectionOpen(connection))
                {
                    if (!await _handler.SendRawAsync(message, connection))
                    {
                        return(false);
                    }

                    await FireEventAsync(this, new TcpMessageServerEventArgs
                    {
                        Message          = message,
                        MessageEventType = MessageEventType.Sent,
                        Connection       = connection,
                        Packet           = new Packet
                        {
                            Data      = message,
                            Timestamp = DateTime.UtcNow
                        },
                    });

                    return(true);
                }
            }
            catch (Exception ex)
            {
                await FireEventAsync(this, new TcpErrorServerEventArgs
                {
                    Connection = connection,
                    Exception  = ex,
                    Message    = ex.Message
                });
            }

            return(false);
        }
Esempio n. 6
0
        public virtual async Task <bool> SendRawAsync(string message, IConnectionServer connection)
        {
            try
            {
                if (!_isRunning)
                {
                    return(false);
                }

                await connection.Writer.WriteLineAsync(message);

                await FireEventAsync(this, new TcpMessageServerEventArgs
                {
                    MessageEventType = MessageEventType.Sent,
                    Message          = message,
                    Connection       = connection,
                    Packet           = new Packet
                    {
                        Data      = message,
                        Timestamp = DateTime.UtcNow
                    },
                });

                return(true);
            }
            catch (Exception ex)
            {
                await FireEventAsync(this, new TcpErrorServerEventArgs
                {
                    Exception  = ex,
                    Message    = ex.Message,
                    Connection = connection
                });

                await DisconnectConnectionAsync(connection);
            }

            return(false);
        }
Esempio n. 7
0
        public virtual async Task <bool> SendAsync <T>(T packet, IConnectionServer connection) where T : IPacket
        {
            try
            {
                if (!_isRunning)
                {
                    return(false);
                }

                var message = JsonConvert.SerializeObject(packet);

                await connection.Writer.WriteLineAsync(message);

                await FireEventAsync(this, new TcpMessageServerEventArgs
                {
                    MessageEventType = MessageEventType.Sent,
                    Message          = message,
                    Packet           = packet,
                    Connection       = connection
                });

                return(true);
            }
            catch (Exception ex)
            {
                await FireEventAsync(this, new TcpErrorServerEventArgs
                {
                    Exception  = ex,
                    Message    = ex.Message,
                    Connection = connection
                });

                await DisconnectConnectionAsync(connection);
            }

            return(false);
        }
Esempio n. 8
0
        public virtual async Task <bool> SendToConnectionAsync <S>(S packet, IConnectionServer connection) where S : IPacket
        {
            if (_handler.IsServerRunning)
            {
                if (_connectionManager.IsConnectionOpen(connection))
                {
                    try
                    {
                        if (!await _handler.SendAsync(packet, connection))
                        {
                            return(false);
                        }

                        await FireEventAsync(this, new TcpMessageServerAuthEventArgs <T>
                        {
                            Message          = JsonConvert.SerializeObject(packet),
                            MessageEventType = MessageEventType.Sent,
                            Connection       = connection,
                            Packet           = packet,
                            UserId           = default
                        });

                        return(true);
                    }
Esempio n. 9
0
        public virtual async Task <bool> SendToConnectionAsync <S>(S packet, IConnectionServer connection) where S : IPacket
        {
            try
            {
                if (_handler != null &&
                    _handler.IsServerRunning &&
                    _connectionManager.IsConnectionOpen(connection))
                {
                    if (!await _handler.SendAsync(packet, connection))
                    {
                        return(false);
                    }

                    await FireEventAsync(this, new TcpMessageServerEventArgs
                    {
                        Message          = JsonConvert.SerializeObject(packet),
                        MessageEventType = MessageEventType.Sent,
                        Packet           = packet,
                        Connection       = connection,
                    });

                    return(true);
                }
            }
            catch (Exception ex)
            {
                await FireEventAsync(this, new TcpErrorServerEventArgs
                {
                    Connection = connection,
                    Exception  = ex,
                    Message    = ex.Message
                });
            }

            return(false);
        }
        public void RunBeforeEveryTest()
        {
            _dummyConnection = Substitute.For <IConnectionServer>();

            _testServer = new EditorConnectionServer(_dummyConnection, Substitute.For <ICommandScheduler>(), 15000);
        }
Esempio n. 11
0
        public PlebWorldServer(IConnectionServer connectionServer)
        {
            this.connectionServer = connectionServer;

            connectionServer.OnConnection += ConnectionServer_OnConnection;
        }
Esempio n. 12
0
 public virtual async Task <bool> DisconnectConnectionAsync(IConnectionServer connection)
 {
     return(await _handler.DisconnectConnectionAsync(connection));
 }
Esempio n. 13
0
 public virtual bool IsConnectionOpen(IConnectionServer connection)
 {
     return(_connections.TryGetValue(connection.Client.GetHashCode(), out var instance) ? instance.Client.Connected : false);
 }
Esempio n. 14
0
 public virtual void RemoveConnection(IConnectionServer connection)
 {
     _connections.TryRemove(connection.Client.GetHashCode(), out var instance);
 }
Esempio n. 15
0
 public virtual bool AddConnection(IConnectionServer connection)
 {
     return(!_connections.ContainsKey(connection.Client.GetHashCode()) ? _connections.TryAdd(connection.Client.GetHashCode(), connection) : false);
 }