protected virtual async Task MessageReceivedAsync(string message, IConnectionTcpServer connection) { IPacket packet; try { packet = JsonConvert.DeserializeObject <Packet>(message); if (string.IsNullOrWhiteSpace(packet.Data)) { packet = new Packet { Data = message, Timestamp = DateTime.UtcNow }; } } catch { packet = new Packet { Data = message, Timestamp = DateTime.UtcNow }; } await FireEventAsync(this, new TcpMessageServerEventArgs { MessageEventType = MessageEventType.Receive, Packet = packet, Connection = connection }); }
public virtual async Task <bool> SendToConnectionAsync <S>(S packet, IConnectionTcpServer 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 { 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); }
protected virtual async Task StartListeningForMessagesAsync(IConnectionTcpServer connection) { do { try { var line = await connection.Reader.ReadLineAsync(); if (!string.IsNullOrWhiteSpace(line)) { if (line.Trim().ToLower() == "pong") { connection.HasBeenPinged = false; } else { await MessageReceivedAsync(line, connection); } } } catch { await FireEventAsync(this, new TcpConnectionServerEventArgs { Connection = connection, ConnectionEventType = ConnectionEventType.Disconnect, }); await DisconnectConnectionAsync(connection); } } while (connection.Client != null && connection.Client.Connected); }
public virtual async Task <bool> SendToConnectionAsync(string message, IConnectionTcpServer connection) { return(await SendToConnectionAsync(new Packet { Data = message, Timestamp = DateTime.UtcNow }, connection)); }
public virtual async Task BroadcastToAllAuthorizedUsersAsync(string message, IConnectionTcpServer connectionSending) { await BroadcastToAllAuthorizedUsersAsync(new Packet { Data = message, Timestamp = DateTime.UtcNow }, connectionSending); }
public virtual async Task BroadcastToAllAuthorizedUsersAsync <S>(S packet, IConnectionTcpServer 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); } } } } }
public virtual async Task <bool> DisconnectConnectionAsync(IConnectionTcpServer 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); }
public virtual async Task <bool> SendToConnectionRawAsync(string message, IConnectionTcpServer connection) { try { if (_handler != null && _handler.IsServerRunning && _connectionManager.IsConnectionOpen(connection)) { if (!await _handler.SendRawAsync(message, connection)) { return(false); } await FireEventAsync(this, new TcpMessageServerEventArgs { 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); }
public virtual async Task <bool> SendRawAsync(string message, IConnectionTcpServer connection) { try { if (!_isRunning) { return(false); } await connection.Writer.WriteLineAsync(message); await FireEventAsync(this, new TcpMessageServerEventArgs { MessageEventType = MessageEventType.Sent, 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); }
public virtual async Task <bool> SendToConnectionAsync <S>(S packet, IConnectionTcpServer 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> { MessageEventType = MessageEventType.Sent, Connection = connection, Packet = packet, UserId = default }); return(true); }
public virtual async Task <bool> SendAsync <T>(T packet, IConnectionTcpServer 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, 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); }
public virtual async Task <bool> DisconnectConnectionAsync(IConnectionTcpServer connection) { return(await _handler.DisconnectConnectionAsync(connection)); }
public bool IsConnectionOpen(IConnectionTcpServer connection) { return(_connections.TryGetValue(connection.Client.GetHashCode(), out var instance) ? instance.Client.Connected : false); }
public void RemoveConnection(IConnectionTcpServer connection) { _connections.TryRemove(connection.Client.GetHashCode(), out var instance); }
public bool AddConnection(IConnectionTcpServer connection) { return(!_connections.ContainsKey(connection.Client.GetHashCode()) ? _connections.TryAdd(connection.Client.GetHashCode(), connection) : false); }