/// <summary> /// Start the Dme Tcp Server. /// </summary> public virtual async void Start() { // _bossGroup = new MultithreadEventLoopGroup(1); _workerGroup = new MultithreadEventLoopGroup(); _scertHandler = new ScertServerHandler(); // Add client on connect _scertHandler.OnChannelActive += (channel) => { string key = channel.Id.AsLongText(); _channelDatas.TryAdd(key, new ChannelData()); }; // Remove client on disconnect _scertHandler.OnChannelInactive += (channel) => { string key = channel.Id.AsLongText(); _channelDatas.TryRemove(key, out var data); }; // Queue all incoming messages _scertHandler.OnChannelMessage += (channel, message) => { string key = channel.Id.AsLongText(); if (_channelDatas.TryGetValue(key, out var data)) { data.RecvQueue.Enqueue(message); } // Log if id is set if (message.CanLog()) { Logger.Info($"TCP RECV {channel}: {message}"); } }; var bootstrap = new ServerBootstrap(); bootstrap .Group(_bossGroup, _workerGroup) .Channel <TcpServerSocketChannel>() .Option(ChannelOption.SoBacklog, 100) .Handler(new LoggingHandler(LogLevel.INFO)) .ChildHandler(new ActionChannelInitializer <ISocketChannel>(channel => { IChannelPipeline pipeline = channel.Pipeline; pipeline.AddLast(new WriteTimeoutHandler(15)); pipeline.AddLast(new ScertEncoder()); pipeline.AddLast(new ScertIEnumerableEncoder()); pipeline.AddLast(new ScertTcpFrameDecoder(DotNetty.Buffers.ByteOrder.LittleEndian, 1024, 1, 2, 0, 0, false)); pipeline.AddLast(new ScertDecoder(_sessionCipher, AuthKey)); pipeline.AddLast(_scertHandler); })); _boundChannel = await bootstrap.BindAsync(Port); }
public async Task Start() { _group = new MultithreadEventLoopGroup(); _scertHandler = new ScertServerHandler(); TimeLostConnection = null; // Add client on connect _scertHandler.OnChannelActive += (channel) => { }; // Remove client on disconnect _scertHandler.OnChannelInactive += async(channel) => { Logger.Error($"Lost connection to MPS"); TimeLostConnection = DateTime.UtcNow; await Stop(); }; // Queue all incoming messages _scertHandler.OnChannelMessage += (channel, message) => { // Add to queue _mpsRecvQueue.Enqueue(message); // Log if id is set if (message.CanLog()) { Logger.Info($"MPS RECV {channel}: {message}"); } }; _bootstrap = new Bootstrap(); _bootstrap .Group(_group) .Channel <TcpSocketChannel>() .Option(ChannelOption.TcpNodelay, true) .Handler(new ActionChannelInitializer <ISocketChannel>(channel => { IChannelPipeline pipeline = channel.Pipeline; pipeline.AddLast(new ScertEncoder()); pipeline.AddLast(new ScertIEnumerableEncoder()); pipeline.AddLast(new ScertTcpFrameDecoder(DotNetty.Buffers.ByteOrder.LittleEndian, Constants.MEDIUS_MESSAGE_MAXLEN, 1, 2, 0, 0, false)); pipeline.AddLast(new ScertDecoder(_sessionCipher, _serverKey)); pipeline.AddLast(_scertHandler); })); await ConnectMPS(); }
public virtual async void Start() { // _bossGroup = new MultithreadEventLoopGroup(1); _workerGroup = new MultithreadEventLoopGroup(); Func <RT_MSG_TYPE, CipherContext, ICipher> getCipher = (id, context) => { switch (context) { case CipherContext.RC_CLIENT_SESSION: return(_sessionCipher); case CipherContext.RSA_AUTH: return(AuthKey); default: return(null); } }; _scertHandler = new ScertServerHandler(); // Add client on connect _scertHandler.OnChannelActive += (channel) => { string key = channel.Id.AsLongText(); var data = new ChannelData() { State = ClientState.CONNECTED }; _channelDatas.TryAdd(key, data); // Check if IP is banned Program.Database.GetIsIpBanned((channel.RemoteAddress as IPEndPoint).Address.MapToIPv4().ToString()).ContinueWith((r) => { data.IsBanned = r.IsCompletedSuccessfully && r.Result; if (data.IsBanned == true) { QueueBanMessage(data); } else { // Check if in maintenance mode Program.Database.GetServerFlags().ContinueWith((r) => { if (r.IsCompletedSuccessfully && r.Result != null && r.Result.MaintenanceMode != null) { // Ensure that maintenance is active // Ensure that we're past the from date // Ensure that we're before the to date (if set) if (r.Result.MaintenanceMode.IsActive && DateTime.UtcNow > r.Result.MaintenanceMode.FromDt && (!r.Result.MaintenanceMode.ToDt.HasValue || r.Result.MaintenanceMode.ToDt > DateTime.UtcNow)) { QueueBanMessage(data, "Server in maintenance."); } } }); } }); }; // Remove client on disconnect _scertHandler.OnChannelInactive += async(channel) => { await Tick(channel); string key = channel.Id.AsLongText(); if (_channelDatas.TryRemove(key, out var data)) { data.State = ClientState.DISCONNECTED; data.ClientObject?.OnDisconnected(); } }; // Queue all incoming messages _scertHandler.OnChannelMessage += (channel, message) => { string key = channel.Id.AsLongText(); if (_channelDatas.TryGetValue(key, out var data)) { // Don't queue message if client is ignored if (!data.Ignore) { // Don't queue if banned if (data.IsBanned == null || data.IsBanned == false) { data.RecvQueue.Enqueue(message); if (message is RT_MSG_SERVER_ECHO serverEcho) { data.ClientObject?.OnRecvServerEcho(serverEcho); } } } } // Log if id is set if (message.CanLog()) { Logger.Info($"RECV {data?.ClientObject},{channel}: {message}"); } }; try { var bootstrap = new ServerBootstrap(); bootstrap .Group(_bossGroup, _workerGroup) .Channel <TcpServerSocketChannel>() //.Option(ChannelOption.SoBacklog, 100) .Handler(new LoggingHandler(LogLevel.INFO)) .ChildHandler(new ActionChannelInitializer <ISocketChannel>(channel => { IChannelPipeline pipeline = channel.Pipeline; pipeline.AddLast(new WriteTimeoutHandler(Program.Settings.ClientTimeoutSeconds)); pipeline.AddLast(new ScertEncoder()); pipeline.AddLast(new ScertIEnumerableEncoder()); pipeline.AddLast(new ScertTcpFrameDecoder(DotNetty.Buffers.ByteOrder.LittleEndian, 1024, 1, 2, 0, 0, false)); pipeline.AddLast(new ScertDecoder(_sessionCipher, AuthKey)); pipeline.AddLast(_scertHandler); })); _boundChannel = await bootstrap.BindAsync(Port); } finally { } }
private async void Start(string serverIp, short serverPort) { Func <RT_MSG_TYPE, CipherContext, ICipher> getCipher = (id, context) => { switch (context) { case CipherContext.RC_CLIENT_SESSION: return(_sessionCipher); case CipherContext.RSA_AUTH: return(AuthKey); default: return(null); } }; _group = new MultithreadEventLoopGroup(); _scertHandler = new ScertServerHandler(); // Initialize on connect _scertHandler.OnChannelActive += async(channel) => { RecvQueue.Clear(); SendQueue.Clear(); State = ClientState.CONNECTED; await OnConnected(channel); }; // _scertHandler.OnChannelInactive += async(channel) => { await OnDisconnected(channel); }; // Queue all incoming messages _scertHandler.OnChannelMessage += (channel, message) => { RecvQueue.Enqueue(message); // Log if id is set if (message.CanLog()) { Logger.Info($"RECV {channel}: {message}"); } }; try { var bootstrap = new Bootstrap(); bootstrap .Group(_group) .Channel <TcpSocketChannel>() .Option(ChannelOption.TcpNodelay, true) .Handler(new ActionChannelInitializer <ISocketChannel>(channel => { IChannelPipeline pipeline = channel.Pipeline; pipeline.AddLast(new ScertEncoder()); pipeline.AddLast(new ScertIEnumerableEncoder()); pipeline.AddLast(new ScertTcpFrameDecoder(DotNetty.Buffers.ByteOrder.LittleEndian, Constants.MEDIUS_MESSAGE_MAXLEN, 1, 2, 0, 0, false)); pipeline.AddLast(new ScertDecoder(_sessionCipher, AuthKey)); pipeline.AddLast(_scertHandler); })); try { _boundChannel = await bootstrap.ConnectAsync(new IPEndPoint(IPAddress.Parse(serverIp), serverPort)); } catch (Exception e) { Logger.Error($"Failed to connect to server {e}"); State = ClientState.DISCONNECTED; return; } } finally { } }
public virtual async void Start() { // _bossGroup = new MultithreadEventLoopGroup(1); _workerGroup = new MultithreadEventLoopGroup(); Func <RT_MSG_TYPE, CipherContext, ICipher> getCipher = (id, context) => { switch (context) { case CipherContext.RC_CLIENT_SESSION: return(_sessionCipher); case CipherContext.RSA_AUTH: return(AuthKey); default: return(null); } }; _scertHandler = new ScertServerHandler(); // Add client on connect _scertHandler.OnChannelActive += (channel) => { string key = channel.Id.AsLongText(); _channelDatas.TryAdd(key, new ChannelData() { State = ClientState.CONNECTED }); }; // Remove client on disconnect _scertHandler.OnChannelInactive += async(channel) => { await Tick(channel); string key = channel.Id.AsLongText(); if (_channelDatas.TryRemove(key, out var data)) { data.State = ClientState.DISCONNECTED; data.ClientObject?.OnDisconnected(); } }; // Queue all incoming messages _scertHandler.OnChannelMessage += (channel, message) => { string key = channel.Id.AsLongText(); if (_channelDatas.TryGetValue(key, out var data)) { data.RecvQueue.Enqueue(message); data.ClientObject?.OnEcho(DateTime.UtcNow); } // Log if id is set if (message.CanLog()) { Logger.Info($"RECV {data?.ClientObject},{channel}: {message}"); } }; try { var bootstrap = new ServerBootstrap(); bootstrap .Group(_bossGroup, _workerGroup) .Channel <TcpServerSocketChannel>() .Option(ChannelOption.SoBacklog, 100) .Handler(new LoggingHandler(LogLevel.INFO)) .ChildHandler(new ActionChannelInitializer <ISocketChannel>(channel => { IChannelPipeline pipeline = channel.Pipeline; pipeline.AddLast(new ScertEncoder()); pipeline.AddLast(new ScertIEnumerableEncoder()); pipeline.AddLast(new ScertTcpFrameDecoder(DotNetty.Buffers.ByteOrder.LittleEndian, 1024, 1, 2, 0, 0, false)); pipeline.AddLast(new ScertDecoder(_sessionCipher, AuthKey)); pipeline.AddLast(_scertHandler); })); _boundChannel = await bootstrap.BindAsync(Port); } finally { } }