/// <summary> /// Se gatilla para levantar los servicios /// Si ocurre un error se arroja /// </summary> private void start() { try { if (netData.OpSystem == OpSystemType.WINXPSP3) { commHandler.informationNetworkingHandler("NETHANDLER: start netHandler..."); netHandlerState = NetHandlerState.STARTING; commHandler.informationNetworkingHandler("NETHANDLER: nerwork adapter is... " + netData.NetworkAdapter.Description); ////intenta poner la ip en modo estatico try { commHandler.informationNetworkingHandler("NETHANDLER: disable adapter..."); SystemHandler.disableIpAdapter(netData.NetworkAdapter); commHandler.informationNetworkingHandler("NETHANDLER: disable adapter... ok!"); } catch (ThreadAbortException e) { throw e; } catch (Exception e) { commHandler.informationNetworkingHandler("NETHANDLER: disable adapter... failed! " + e.Message); } try { commHandler.informationNetworkingHandler("NETHANDLER: set IP... " + netData.IpTcpListener); SystemHandler.setStaticIP(netData.NetworkAdapter, netData.IpTcpListener.ToString(), netData.SubnetMask); commHandler.informationNetworkingHandler("NETHANDLER: set IP... ok!"); } catch (ThreadAbortException e) { throw e; } catch (Exception e) { commHandler.informationNetworkingHandler("NETHANDLER: set IP... failed! " + e.Message); } try { commHandler.informationNetworkingHandler("NETHANDLER: enable adapter..."); SystemHandler.enableIpAdapter(netData.NetworkAdapter); commHandler.informationNetworkingHandler("NETHANDLER: enable adapter... ok!"); } catch (ThreadAbortException e) { throw e; } catch (Exception e) { commHandler.informationNetworkingHandler("NETHANDLER: enable adapter... failed! " + e.Message); } //hecha a andar wifiHandler commHandler.informationNetworkingHandler("NETHANDLER: start wifi..."); wifiHandler.connect(); //espera por primera conexión while (wifiHandler.ConnectionState == WifiConnectionState.DISCONNECTED) { commHandler.informationNetworkingHandler("NETHANDLER: waiting for other devices"); System.Threading.Thread.Sleep(netData.WaitTimeStart); } commHandler.informationNetworkingHandler("NETHANDLER: start wifi... ok!"); //Setea la IP en el sistema operativo Boolean ipChange = false; Int32 timeOutIpChange = 0; while (!ipChange) { try { commHandler.informationNetworkingHandler("NETHANDLER: set IP... " + netData.IpTcpListener); SystemHandler.setStaticIP(netData.NetworkAdapter, netData.IpTcpListener.ToString(), netData.SubnetMask); ipChange = true; commHandler.informationNetworkingHandler("NETHANDLER: set IP... ok!"); } catch (ThreadAbortException e) { throw e; } catch (Exception e) { commHandler.informationNetworkingHandler("NETHANDLER: set IP... failed! " + e.Message); timeOutIpChange++; if (timeOutIpChange > netData.WaitForStart) { throw new Exception("timeout, para configurar IP"); } System.Threading.Thread.Sleep(netData.WaitTimeStart); } } } else if (netData.OpSystem == OpSystemType.WIN7) { commHandler.informationNetworkingHandler("NETHANDLER: start netHandler..."); netHandlerState = NetHandlerState.STARTING; commHandler.informationNetworkingHandler("NETHANDLER: nerwork adapter is... " + netData.NetworkAdapter.Description); try { commHandler.informationNetworkingHandler("NETHANDLER: enable adapter..."); SystemHandler.enableIpAdapter(netData.NetworkAdapter); commHandler.informationNetworkingHandler("NETHANDLER: enable adapter... ok!"); } catch (ThreadAbortException e) { throw e; } catch (Exception e) { commHandler.informationNetworkingHandler("NETHANDLER: enable adapter... failed! " + e.Message); } //hecha a andar wifiHandler commHandler.informationNetworkingHandler("NETHANDLER: start wifi..."); wifiHandler.connect(); //espera por primera conexión while (wifiHandler.ConnectionState == WifiConnectionState.DISCONNECTED) { commHandler.informationNetworkingHandler("NETHANDLER: waiting for other devices"); System.Threading.Thread.Sleep(netData.WaitTimeStart); } commHandler.informationNetworkingHandler("NETHANDLER: start wifi... ok!"); //Setea la IP en el sistema operativo Boolean ipChange = false; Int32 timeOutIpChange = 0; while (!ipChange) { try { commHandler.informationNetworkingHandler("NETHANDLER: set IP... " + netData.IpTcpListener); SystemHandler.setStaticIP2(netData.NetworkAdapter, netData.IpTcpListener.ToString(), netData.SubnetMask); ipChange = true; commHandler.informationNetworkingHandler("NETHANDLER: set IP... ok!"); } catch (ThreadAbortException e) { throw e; } catch (Exception e) { commHandler.informationNetworkingHandler("NETHANDLER: set IP... failed! " + e.Message); timeOutIpChange++; if (timeOutIpChange > netData.WaitForStart) { throw new Exception("timeout, para configurar IP"); } System.Threading.Thread.Sleep(netData.WaitTimeStart); } } } //chequeo de ip commHandler.informationNetworkingHandler("NETHANDLER: start strong DAD"); ipHandler.startStrongDAD(); //Servicios TCP commHandler.informationNetworkingHandler("NETHANDLER: start TCP..."); tcpListener.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1); Boolean tcpChange = false; Int32 timeOutTcpChange = 0; while (!tcpChange) { try { commHandler.informationNetworkingHandler("NETHANDLER: start TCP listener... " + netData.IpTcpListener + ":" + netData.TcpPort); tcpListener.Start(); tcpChange = true; commHandler.informationNetworkingHandler("NETHANDLER: start TCP listener... ok!"); } catch (ThreadAbortException e) { throw e; } catch (Exception e) { commHandler.informationNetworkingHandler("NETHANDLER: start TCP listener... failed! " + e.Message); timeOutTcpChange++; if (timeOutTcpChange > netData.WaitForStart) { throw new Exception("timeout, para levantar servicio TCP"); } else { System.Threading.Thread.Sleep(netData.WaitTimeStart); } } } tcpListenerThread.Start(); commHandler.informationNetworkingHandler("NETHANDLER: start TCP... ok!"); //conecta a UDP commHandler.informationNetworkingHandler("NETHANDLER: start UDP... " + netData.IpUdpMulticast + ":" + netData.UdpPort); udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1); //udpClient.Client.Bind(new IPEndPoint(IPAddress.Any, netData.UdpPort)); udpClient.Client.Bind(new IPEndPoint(tcpAddress, netData.UdpPort)); udpClient.JoinMulticastGroup(udpMulticastAdress, tcpAddress); udpIpEndPoint = new IPEndPoint(udpMulticastAdress, netData.UdpPort); udpClientThread.Start(); commHandler.informationNetworkingHandler("NETHANDLER: start UDP... ok!"); //cambio a cheque de ip debil commHandler.informationNetworkingHandler("NETHANDLER: start weak DAD"); ipHandler.chageToWeakDAD(); //Avisa que empiece la comunicacion commHandler.startNetworkingHandler(); netHandlerState = NetHandlerState.STARTED; commHandler.informationNetworkingHandler("NETHANDLER: start netHandler... ok!"); commHandler.informationNetworkingHandler("NETHANDLER: we are so connected!!! welcome to HLMP"); } catch (ThreadAbortException e) { commHandler.informationNetworkingHandler("NETHANDLER: start netHandler... failed! " + e.Message); } catch (Exception e) { //disconnect(); commHandler.informationNetworkingHandler("NETHANDLER: start netHandler... failed! " + e.Message); commHandler.errorNetworkingHandler(e); } }
/// <summary> /// Es ejecutado por un thread para verificar el estado de la conexión con la red inalambrica /// </summary> private void wakeUpDaemon() { int sleepingPill = 0; while (true) { lock (syncPoint) { switch (connectionState) { case WifiConnectionState.CONNECTED: { sleepingPill = 0; break; } case WifiConnectionState.DISCONNECTED: { sleepingPill++; if (sleepingPill > netData.WaitForWifi) { sleepingPill = 0; try { wifiInformation("WIFI: disable adapter..."); SystemHandler.disableIpAdapter(netData.NetworkAdapter); wifiInformation("WIFI: disable adapter... ok!"); } catch (ThreadAbortException e) { throw e; } catch (Exception e) { wifiInformation("WIFI: disable adapter... failed! " + e.Message); } try { wifiInformation("WIFI: enable adapter..."); SystemHandler.enableIpAdapter(netData.NetworkAdapter); wifiInformation("WIFI: enable adapter... ok!"); } catch (ThreadAbortException e) { throw e; } catch (Exception e) { wifiInformation("WIFI: enable adapter... failed! " + e.Message); } } else { openWLanConnection(); } break; } case WifiConnectionState.WAINTING: { break; } } } if (connectionState == WifiConnectionState.STOP) { return; } else { Thread.Sleep(netData.WaitTimeWiFi); } } }