/// <summary> /// 侦测设备连接请求 /// </summary> private void AcceptClient(IAsyncResult result) { var server = (Socket)result.AsyncState; try { var client = server.EndAccept(result); var misakaClient = new MisakaTcpClient(client) { ParentWindow = ParentWindow, ConnectionType = ConnectionItemType.TcpAcceptedClient }; misakaClient.ClientBeginReceive(); misakaClient.ClientReceivedDataEvent += OnClientReceivedData; misakaClient.ClientDisconnectEvent += OnClientDisconnect; _tcpClients.Add(client.RemoteEndPoint.ToString(), misakaClient); ParentWindow.DispatcherAddReportData(ReportMessageType.Info, $"{ReportMessageEnum.AcceptClientSuccess}{client.RemoteEndPoint}"); OnClientAccepted(EventArgs.Empty); } catch (ObjectDisposedException ex) { LogService.Instance.Info(ReportMessageEnum.ListenerClosed, ex); OnServerDisconnected(); return; } catch (Exception ex) { LogService.Instance.Warn(ReportMessageEnum.AcceptClientFailed, ex); ParentWindow.DispatcherAddReportData(ReportMessageType.Error, ReportMessageEnum.AcceptClientFailed); } server.BeginAccept(AcceptClient, server); }
/// <summary> /// 设置当前客户端连接 /// </summary> public void SetCurrentClient(string clientName) { if (clientName == Appconfig.SelectAllConnection) { _broadcast = true; return; } _broadcast = false; _currenTcpClient = _tcpClients[clientName]; }
/// <summary> /// 创建新的连接 /// </summary> /// <param name="type"></param> /// <returns></returns> public static void NewMisakaConnection(string type) { IMisakaConnection conn = null; switch (type) { case ConnectionItemType.TcpServer: conn = new MisakaTcpServer(Globals.GetLocalIpAddress(), Globals.RandomPort()) { ConnectionType = type }; break; case ConnectionItemType.TcpClient: conn = new MisakaTcpClient(Globals.GetLocalIpAddress(), Globals.RandomPort()) { ConnectionType = type }; break; } AddConnection(conn); }