/// <summary> /// Desconecta los servicios TCP asociados a una maquina remota /// </summary> /// <param name="o">La ip de la maquina remota a desconectar en formato de String o la maquina remota</param> private void disconnectFromAsync(Object o) { try { commHandler.informationNetworkingHandler("TCP: disconnection..."); if (o.GetType().Equals(typeof(IPAddress))) { IPAddress machineIp = (IPAddress)o; RemoteMachine machine = tcpServerList.getRemoteMachine(machineIp); commHandler.informationNetworkingHandler("TCP: close signal"); machine.close(); commHandler.informationNetworkingHandler("TCP: remove from queue"); tcpServerList.remove(machine); } else if (o.GetType().Equals(typeof(RemoteMachine))) { RemoteMachine machine = (RemoteMachine)o; commHandler.informationNetworkingHandler("TCP: close signal"); machine.close(); commHandler.informationNetworkingHandler("TCP: remove from queue"); tcpServerList.remove(machine); } commHandler.informationNetworkingHandler("TCP: disconnection... ok!"); } catch (ThreadAbortException e) { throw e; } catch (Exception e) { commHandler.informationNetworkingHandler("TCP: disconnection... failed! " + e.Message); } }
/// <summary> /// Escucha nuevos clientes TCP que quieran establecer comunicación con ésta máquina /// Si ocurre un error se notifica en errorNetworkingHandler /// </summary> private void listenTCPClients() { try { while (true) { TcpClient tcpClient = tcpListener.AcceptTcpClient(); commHandler.informationNetworkingHandler("TCP: new client detected"); tcpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true); tcpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontRoute, true); tcpClient.LingerState.LingerTime = 0; tcpClient.LingerState.Enabled = false; IPAddress ip = ((IPEndPoint)tcpClient.Client.RemoteEndPoint).Address; Thread clientThread = new Thread(new ParameterizedThreadStart(listenTCPMessages)); RemoteMachine remoteMachine = new RemoteMachine(ip, tcpClient, clientThread); clientThread.Start(remoteMachine); RemoteMachine oldRemoteMachine = tcpServerList.getRemoteMachine(ip); if (oldRemoteMachine != null) { oldRemoteMachine.close(); tcpServerList.remove(oldRemoteMachine); } tcpServerList.add(ip, remoteMachine); commHandler.informationNetworkingHandler("TCP: new client connected"); } } catch (ThreadAbortException e) { throw e; } catch (Exception e) { commHandler.informationNetworkingHandler("TCP WARNING: TCP listener has stopped!! " + e.Message); } }
/// <summary> /// Se conecta a una máquina remota por TCP para enviarle mensajes posteriormente /// Si ocurre un error se notifica en informationNetworkingHandler /// </summary> /// <param name="o">La dirección IP de la máquina remota en formato IPAddress</param> private void connectToAsync(Object o) { try { commHandler.informationNetworkingHandler("TCP: connection..."); IPAddress serverIp = (IPAddress)o; TcpClient tcpClient = new TcpClient(new IPEndPoint(tcpAddress, 0)); tcpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true); tcpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontRoute, true); tcpClient.LingerState.LingerTime = 0; tcpClient.LingerState.Enabled = false; //Conexion asincrona con time out IAsyncResult result = tcpClient.BeginConnect(serverIp, netData.TcpPort, null, null); bool success = result.AsyncWaitHandle.WaitOne(netData.TcpConnectTimeOut, true); if (!success) { commHandler.informationNetworkingHandler("TCP: connection... time out!"); return; } else { tcpClient.EndConnect(result); Thread clientThread = new Thread(new ParameterizedThreadStart(listenTCPMessages)); RemoteMachine remoteMachine = new RemoteMachine(serverIp, tcpClient, clientThread); clientThread.Start(remoteMachine); RemoteMachine oldRemoteMachine = tcpServerList.getRemoteMachine(serverIp); if (oldRemoteMachine != null) { oldRemoteMachine.close(); tcpServerList.remove(oldRemoteMachine); } tcpServerList.add(serverIp, remoteMachine); commHandler.informationNetworkingHandler("TCP: connection... ok!"); } } catch (ThreadAbortException e) { throw e; } catch (Exception e) { commHandler.informationNetworkingHandler("TCP: connection... failed! " + e.Message); commHandler.informationNetworkingHandler(e.StackTrace); } }