void TcpServer_ClientConnected(object sender, AsyncEventArgs e) { AsyncTCPServer server = sender as AsyncTCPServer; int temp = ServersList.ToList <ServersInfo>().FindIndex(ex => (ex.IPAddr.ToString() == server.Address.ToString() && ex.Port == server.Port)); if (temp == -1)//列表中没找到服务器 { return; } TCPClientState client = e._state as TCPClientState; ClientsInfo clientinfo = new ClientsInfo(); clientinfo.IpEndPort = client.TcpClient.Client.RemoteEndPoint; clientinfo.ClientState = client; clientinfo.ParentServer = ServersList[temp]; App.Current.Dispatcher.Invoke((System.Action)(() => { ServersList[temp].ClientsList.Add(clientinfo); foreach (var ser in ServersList) { ser.IsSelected = false; foreach (var tmp in ser.ClientsList) { tmp.IsSelected = false; } } SelectServer = ServersList[temp]; SelectServer.SelectClient = clientinfo; SelectServer.SelectClient.IsSelected = true; IsHaveClent = true; })); }
void TcpServer_ClientDisconnected(object sender, AsyncEventArgs e) { AsyncTCPServer server = sender as AsyncTCPServer; int temp = ServersList.ToList <ServersInfo>().FindIndex(ex => (ex.IPAddr.ToString() == server.Address.ToString() && ex.Port == server.Port)); if (temp == -1)//列表中没找到服务器 { return; } TCPClientState client = e._state as TCPClientState; EndPoint endpoint = client.TcpClient.Client.RemoteEndPoint; App.Current.Dispatcher.Invoke((System.Action)(() => { var tmp = ServersList[temp].ClientsList.First(ex => ex.IpEndPort == endpoint); ServersList[temp].ClientsList.Remove(tmp); })); }
public void NewServer_Click(object sender, RoutedEventArgs e) { MainWindow wnd = sender as MainWindow; Views.NewServerView view = new Views.NewServerView(); view.Owner = wnd; if (view.ShowDialog() == true) { string ip = view.iptext.Text.Trim().ToString(); if (ip == "127.0.0.1") { ip = "0.0.0.0"; } int port = int.Parse(view.porttext.Text.Trim().ToString()); ServersInfo ser = new ServersInfo(); ser.IPAddr = IPAddress.Parse(ip); ser.Port = port; ser.IpEndPort = new IPEndPoint(ser.IPAddr, ser.Port); if (ServersList.Count != 0) { int temp = ServersList.ToList <ServersInfo>().FindIndex(ex => ex.IpEndPort.ToString() == ser.IpEndPort.ToString()); if (temp != -1) { MessageBox.Show("该服务器已添加"); return; } } ser.TcpServer = new AsyncTCPServer(ser.IPAddr, ser.Port); ser.TcpServer.ClientConnected += TcpServer_ClientConnected; ser.TcpServer.ClientDisconnected += TcpServer_ClientDisconnected; ser.TcpServer.DataReceived += TcpServer_DataReceived; ServersList.Add(ser); SelectServer = ServersList[ServersList.Count - 1]; } }