public async Task StartAsync(EndPoint endPoint) { var config = new TcpSocketSaeaServerConfiguration(); _server = new TcpSocketSaeaServer((IPEndPoint)endPoint, new SimpleMessageDispatcher((session, message) => { var sender = new SimpleServerMessageSender(_transportMessageEncoder, session, _logger); OnReceived(sender, message); }, _transportMessageDecoder, _logger), config); _server.Listen(); #if NET await Task.FromResult(1); #else await Task.CompletedTask; #endif }
static void Main(string[] args) { NLogLogger.Use(); try { var config = new TcpSocketSaeaServerConfiguration(); _server = new TcpSocketSaeaServer(22222, new SimpleMessageDispatcher(), config); _server.Listen(); Console.WriteLine("TCP server has been started on [{0}].", _server.ListenedEndPoint); Console.WriteLine("Type something to send to clients..."); while (true) { try { string text = Console.ReadLine(); if (text == "quit") { break; } Task.Run(async() => { await _server.BroadcastAsync(Encoding.UTF8.GetBytes(text)); Console.WriteLine("Server [{0}] broadcasts text -> [{1}].", _server.ListenedEndPoint, text); }); } catch (Exception ex) { Console.WriteLine(ex.Message); } } _server.Shutdown(); Console.WriteLine("TCP server has been stopped on [{0}].", _server.ListenedEndPoint); } catch (Exception ex) { Logger.Get <Program>().Error(ex.Message, ex); } Console.ReadKey(); }
static void Main(string[] args) { NLogLogger.Use(); try { var config = new TcpSocketSaeaServerConfiguration(); _server = new TcpSocketSaeaServer(22222, new SimpleMessageDispatcher(), config); _server.Listen(); Console.WriteLine("TCP server has been started on [{0}].", _server.ListenedEndPoint); Console.WriteLine("Type something to send to clients..."); while (true) { try { string text = Console.ReadLine(); if (text == "quit") break; Task.Run(async () => { await _server.BroadcastAsync(Encoding.UTF8.GetBytes(text)); Console.WriteLine("Server [{0}] broadcasts text -> [{1}].", _server.ListenedEndPoint, text); }); } catch (Exception ex) { Console.WriteLine(ex.Message); } } _server.Shutdown(); Console.WriteLine("TCP server has been stopped on [{0}].", _server.ListenedEndPoint); } catch (Exception ex) { Logger.Get<Program>().Error(ex.Message, ex); } Console.ReadKey(); }
public async Task StartAsync(EndPoint endPoint) { _server = new TcpSocketSaeaServer((IPEndPoint)endPoint, async(session, data, offset, count) => { if (_logger.IsEnabled(LogLevel.Information)) { _logger.LogInformation("接收到数据包。"); } var message = _transportMessageDecoder.Decode(data.Skip(offset).Take(count).ToArray()); if (_logger.IsEnabled(LogLevel.Information)) { _logger.LogInformation("接收到消息:" + message.Id); } var sender = new SimpleServerMessageSender(_transportMessageEncoder, session, _logger); await OnReceived(sender, message); }); _server.Listen(); #if NET await Task.FromResult(1); #else await Task.CompletedTask; #endif }
private void button4_Click(object sender, EventArgs e) { //启动监听 server.Listen(new IPEndPoint(IPAddress.Parse("0.0.0.0"), 5211)); this.listBox1.Items.Add("listening..."); }
static void Main(string[] args) { NLogLogger.Use(); try { var config = new TcpSocketSaeaServerConfiguration(); //config.FrameBuilder = new FixedLengthFrameBuilder(20000); //config.FrameBuilder = new RawBufferFrameBuilder(); //config.FrameBuilder = new LineBasedFrameBuilder(); //config.FrameBuilder = new LengthPrefixedFrameBuilder(); //config.FrameBuilder = new LengthFieldBasedFrameBuilder(); _server = new TcpSocketSaeaServer(22222, new SimpleMessageDispatcher(), config); _server.Listen(); Console.WriteLine("TCP server has been started on [{0}].", _server.ListenedEndPoint); Console.WriteLine("Type something to send to clients..."); while (true) { try { string text = Console.ReadLine(); if (text == "quit") { break; } Task.Run(async() => { if (text == "many") { text = new string('x', 8192); for (int i = 0; i < 1000000; i++) { await _server.BroadcastAsync(Encoding.UTF8.GetBytes(text)); Console.WriteLine("Server [{0}] broadcasts text -> [{1}].", _server.ListenedEndPoint, text); } } else if (text == "big1k") { text = new string('x', 1024 * 1); await _server.BroadcastAsync(Encoding.UTF8.GetBytes(text)); Console.WriteLine("Server [{0}] broadcasts text -> [{1} Bytes].", _server.ListenedEndPoint, text.Length); } else if (text == "big10k") { text = new string('x', 1024 * 10); await _server.BroadcastAsync(Encoding.UTF8.GetBytes(text)); Console.WriteLine("Server [{0}] broadcasts text -> [{1} Bytes].", _server.ListenedEndPoint, text.Length); } else if (text == "big100k") { text = new string('x', 1024 * 100); await _server.BroadcastAsync(Encoding.UTF8.GetBytes(text)); Console.WriteLine("Server [{0}] broadcasts text -> [{1} Bytes].", _server.ListenedEndPoint, text.Length); } else if (text == "big1m") { text = new string('x', 1024 * 1024 * 1); await _server.BroadcastAsync(Encoding.UTF8.GetBytes(text)); Console.WriteLine("Server [{0}] broadcasts text -> [{1} Bytes].", _server.ListenedEndPoint, text.Length); } else if (text == "big10m") { text = new string('x', 1024 * 1024 * 10); await _server.BroadcastAsync(Encoding.UTF8.GetBytes(text)); Console.WriteLine("Server [{0}] broadcasts text -> [{1} Bytes].", _server.ListenedEndPoint, text.Length); } else if (text == "big100m") { text = new string('x', 1024 * 1024 * 100); await _server.BroadcastAsync(Encoding.UTF8.GetBytes(text)); Console.WriteLine("Server [{0}] broadcasts text -> [{1} Bytes].", _server.ListenedEndPoint, text.Length); } else if (text == "big1g") { text = new string('x', 1024 * 1024 * 1024); await _server.BroadcastAsync(Encoding.UTF8.GetBytes(text)); Console.WriteLine("Server [{0}] broadcasts text -> [{1} Bytes].", _server.ListenedEndPoint, text.Length); } else { await _server.BroadcastAsync(Encoding.UTF8.GetBytes(text)); Console.WriteLine("Server [{0}] broadcasts text -> [{1} Bytes].", _server.ListenedEndPoint, text.Length); } }); } catch (Exception ex) { Logger.Get <Program>().Error(ex.Message, ex); } } _server.Shutdown(); Console.WriteLine("TCP server has been stopped on [{0}].", _server.ListenedEndPoint); } catch (Exception ex) { Logger.Get <Program>().Error(ex.Message, ex); } Console.ReadKey(); }
static void Main(string[] args) { NLogLogger.Use(); try { var config = new TcpSocketSaeaServerConfiguration(); //config.FrameBuilder = new FixedLengthFrameBuilder(20000); //config.FrameBuilder = new RawBufferFrameBuilder(); //config.FrameBuilder = new LineBasedFrameBuilder(); //config.FrameBuilder = new LengthPrefixedFrameBuilder(); //config.FrameBuilder = new LengthFieldBasedFrameBuilder(); _server = new TcpSocketSaeaServer(22222, new SimpleMessageDispatcher(), config); _server.Listen(); Console.WriteLine("TCP server has been started on [{0}].", _server.ListenedEndPoint); Console.WriteLine("Type something to send to clients..."); while (true) { try { string text = Console.ReadLine(); if (text == "quit") break; Task.Run(async () => { if (text == "many") { text = new string('x', 8192); for (int i = 0; i < 1000000; i++) { await _server.BroadcastAsync(Encoding.UTF8.GetBytes(text)); Console.WriteLine("Server [{0}] broadcasts text -> [{1}].", _server.ListenedEndPoint, text); } } else if (text == "big") { text = new string('x', 1024 * 1024 * 100); await _server.BroadcastAsync(Encoding.UTF8.GetBytes(text)); Console.WriteLine("Server [{0}] broadcasts text -> [{1}].", _server.ListenedEndPoint, text); } else { await _server.BroadcastAsync(Encoding.UTF8.GetBytes(text)); Console.WriteLine("Server [{0}] broadcasts text -> [{1}].", _server.ListenedEndPoint, text); } }); } catch (Exception ex) { Logger.Get<Program>().Error(ex.Message, ex); } } _server.Shutdown(); Console.WriteLine("TCP server has been stopped on [{0}].", _server.ListenedEndPoint); } catch (Exception ex) { Logger.Get<Program>().Error(ex.Message, ex); } Console.ReadKey(); }
private void SessionProviderService_Load(object sender, EventArgs e) { this.Text = "SiMay中间会话服务-IOASJHD BEAT " + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(); IntPtr sysMenuHandle = GetSystemMenu(this.Handle, false); InsertMenu(sysMenuHandle, 7, MF_SEPARATOR, 0, null); InsertMenu(sysMenuHandle, 8, MF_BYPOSITION, IDM_OPTIONS, "系统设置"); InsertMenu(sysMenuHandle, 9, MF_BYPOSITION, IDM_RUNLOG, "运行日志"); _log_imgList = new ImageList(); _log_imgList.Images.Add("ok", Resources.ok); _log_imgList.Images.Add("err", Resources.erro); logList.SmallImageList = _log_imgList; string address = ApplicationConfiguration.IPAddress; string port = ApplicationConfiguration.Port; this.lableIPAddress.Text = address; this.labelPort.Text = port; this.lableStatrTime.Text = DateTime.Now.ToString(); var ipe = new IPEndPoint(IPAddress.Parse(address), int.Parse(port)); var serverConfig = new TcpSocketSaeaServerConfiguration(); serverConfig.ReuseAddress = false; serverConfig.KeepAlive = true; serverConfig.KeepAliveInterval = 5000; serverConfig.KeepAliveSpanTime = 1000; serverConfig.PendingConnectionBacklog = int.Parse(ApplicationConfiguration.Backlog); _server = TcpSocketsFactory.CreateServerAgent(TcpSocketSaeaSessionType.Full, serverConfig, (notify, session) => { switch (notify) { case TcpSocketCompletionNotify.OnConnected: _connectionCount++; this.Invoke(new Action(() => { this.lableConnectionCount.Text = _connectionCount.ToString(); })); //创建通道上下文,等待确认通道类型 TcpChannelContext context = new TcpChannelContext(session); context.OnChannelTypeNotify += Context_TcpAwaitnotifyProc; _channelContexts.Add(context); break; case TcpSocketCompletionNotify.OnSend: this._uploadTransferBytes += session.SendTransferredBytes; break; case TcpSocketCompletionNotify.OnDataReceiveing: this._receiveTransferBytes += session.ReceiveBytesTransferred; ((TcpChannelContext)session.AppTokens[0]).OnMessage(session); break; case TcpSocketCompletionNotify.OnClosed: _connectionCount--; this.Invoke(new Action(() => { this.lableConnectionCount.Text = _connectionCount.ToString(); })); this.SessionClosed(session); break; default: break; } }); try { _server.Listen(ipe); LogShowQueueHelper.WriteLog("SiMay中间会话服务端口" + port + "启动成功!"); } catch { LogShowQueueHelper.WriteLog("SiMay中间会话服务端口" + port + "启动失败!", "err"); } System.Timers.Timer timer = new System.Timers.Timer(); timer.Interval = 100; timer.Elapsed += Timer_Elapsed; timer.Start(); System.Timers.Timer flow_timer = new System.Timers.Timer(); flow_timer.Interval = 1000; flow_timer.Elapsed += Flow_timer_Elapsed; flow_timer.Start(); }
public override void StartSerivce() => _server.Listen(ApplicationConfiguartion.Options.ServiceIPEndPoint);
public override void StartSerivce() => _server.Listen(_options.ServiceIPEndPoint);
public bool StartService(StartServiceOptions options) { ApplicationConfiguartion.SetOptions(options); var serverConfig = new TcpSocketSaeaServerConfiguration(); serverConfig.ReuseAddress = false; serverConfig.KeepAlive = true; serverConfig.KeepAliveInterval = 5000; serverConfig.KeepAliveSpanTime = 1000; serverConfig.PendingConnectionBacklog = 0; var ipe = new IPEndPoint(IPAddress.Parse(ApplicationConfiguartion.Options.LocalAddress), ApplicationConfiguartion.Options.ServicePort); _tcpSaeaServer = TcpSocketsFactory.CreateServerAgent(TcpSocketSaeaSessionType.Full, serverConfig, (notify, session) => { if (SynchronizationContext.IsNull()) { NotifyProc(null); } else { SynchronizationContext.Send(NotifyProc, null); } void NotifyProc(object @object) { switch (notify) { case TcpSessionNotify.OnConnected: ApportionDispatcher apportionDispatcher = new ApportionDispatcher(); apportionDispatcher.ApportionTypeHandlerEvent += ApportionTypeHandlerEvent; apportionDispatcher.LogOutputEventHandler += ApportionDispatcher_LogOutputEventHandler; apportionDispatcher.SetSession(session); break; case TcpSessionNotify.OnSend: break; case TcpSessionNotify.OnDataReceiveing: this.OnDataReceiveingHandler(session); break; case TcpSessionNotify.OnClosed: this.OnClosedHandler(session); break; default: break; } } }); try { _tcpSaeaServer.Listen(ipe); LogOutputEventHandler?.Invoke(LogOutLevelType.Debug, $"SiMay中间会话服务器监听 {ipe.Port} 端口启动成功!"); return(true); } catch (Exception ex) { LogOutputEventHandler?.Invoke(LogOutLevelType.Error, $"SiMay中间会话服务器启动发生错误,端口:{ipe.Port} 错误信息:{ex.Message}!"); return(false); } }