static readonly object lockerDir = new object(); //each thread server must access to an exclusive section one at time public void EntryPoint() { try // start both udp and tcp { threadUDP = new Thread(EntryUDP); threadUDP.IsBackground = true; threadUDP.Start(); threadTCP = new Thread(EntryTCP); threadTCP.IsBackground = true; threadTCP.SetApartmentState(ApartmentState.STA); threadTCP.Start(); } catch (Exception e) { Console.WriteLine(e.ToString()); LANSharingApp.LogFile(e.Message, e.ToString(), e.Source); // delegate the operation on form to the GUI MessageFormError mfe = new MessageFormError("UDP or TCP service not available"); LANSharingApp.gui.Invoke((MethodInvoker) delegate() { mfe.ShowDialog(); }); LANSharingApp.closing = true; LANSharingApp.serverThread.Join(); LANSharingApp.clearTempFiles(); LANSharingApp.flushTmpDirectory(); Application.Exit(); } }
void OnUdpMessageReceived(object sender, MulticastUdpClient.UdpMessageReceivedEventArgs e) { try { string receivedText = ASCIIEncoding.ASCII.GetString(e.Buffer); string[] infoUser = receivedText.Split(','); //conversion //Console.WriteLine("UDP- ricevuto un pacchetto.: " + infoUser[0] + " " + infoUser[1]); // Console.WriteLine("UDP packet: " + infoUser[0] + ", " + infoUser[1] + ", " + infoUser[2] + " " + infoUser[3] + ", " + infoUser[4] + " " + infoUser[5]); Image userImage; userImage = Base64ToImage(infoUser[5]); if (LANSharingApp.umu.isPresent(infoUser[1] + infoUser[0])) { User u = new User(infoUser[0], infoUser[1], infoUser[2], infoUser[3], infoUser[4], userImage); //create new user if ((infoUser[2].CompareTo("online") == 0)) { // check if the user is already registered if (!u.isEqual(LANSharingApp.umu.getAdmin())) { LANSharingApp.umu.addUser(u); //add user } LANSharingApp.umu.resetUserTimer(infoUser[1] + infoUser[0]); // reset user timer } else { u.isOld(); } } else // user not in list { User p = new User(infoUser[0], infoUser[1], infoUser[2], infoUser[3], infoUser[4], userImage); //create new user if (!p.isEqual(LANSharingApp.umu.getAdmin())) { if (infoUser[2].CompareTo("online") == 0) //check not equal to admin { LANSharingApp.umu.addUser(p); //add user } } } } catch (Exception ex) { Console.WriteLine(ex.ToString()); LANSharingApp.LogFile(ex.Message, ex.ToString(), ex.Source); LANSharingApp.closing = true; LANSharingApp.serverThread.Join(); LANSharingApp.clearTempFiles(); LANSharingApp.flushTmpDirectory(); Application.Exit(); } }
// start to notify wiht UDP packets the online presence public void EntryNotifyPresence() { try { while (!LANSharingApp.closing) { //check ethernet, need this line to generate exception!!! string localIP = LANSharingApp.umu.GetLocalIP(); //check if localIP == Admin.getIP if (!localIP.Equals(LANSharingApp.umu.getAdmin().getIp().ToString())) { LANSharingApp.umu.getAdmin().setIP(localIP); Console.WriteLine("Internet Network Changed: Restart Procedure."); // delegate the operation on form to the GUI LANSharingApp.gui.Invoke((MethodInvoker) delegate() { Application.Restart(); }); } // send broadcast every 2s, only if ONLINE if (LANSharingApp.umu.getAdmin().getState().CompareTo("online") == 0) { sendBroadcastPacket(LANSharingApp.umu.getAdmin().getString()); //Console.WriteLine("UDP packet Admin: "+ LANSharingApp.umu.getAdmin().getString()); } // need a timer Thread.Sleep(2000); } } catch (Exception e) { Console.WriteLine(e.ToString()); LANSharingApp.LogFile(e.Message, e.ToString(), e.Source); LANSharingApp.closing = true; // delegate the operation on form to the GUI MessageFormError mfe = new MessageFormError("Error: no lan connection detected, connect to a lan then restart application."); LANSharingApp.gui.Invoke((MethodInvoker) delegate() { mfe.ShowDialog(); }); LANSharingApp.serverThread.Join(); LANSharingApp.clearTempFiles(); LANSharingApp.flushTmpDirectory(); Application.Exit(); } }
// send udp packets for online-presence static void sendBroadcastPacket(string message) { // there is a problem with d-link router --> need this broadcat, not IPAddress.Broadcast IPEndPoint ipEP = new IPEndPoint(IPAddress.Broadcast, FTP_Port_send); try { udpClientWrapper.SendMulticast(ASCIIEncoding.ASCII.GetBytes(message)); } catch (Exception e) { Console.WriteLine("\n" + e.ToString()); LANSharingApp.LogFile(e.Message, e.ToString(), e.Source); LANSharingApp.closing = true; LANSharingApp.serverThread.Join(); LANSharingApp.clearTempFiles(); LANSharingApp.flushTmpDirectory(); Application.Exit(); } }
// EntryPoint of TCP operation public void EntryTCP() { try { // call only one time, on launch if (LANSharingApp.umu.getAdmin().isOnline() && !LANSharingApp.closing) { ReceiveFileProcedure(); } } catch (Exception e) { Console.WriteLine(e.ToString()); LANSharingApp.LogFile(e.Message, e.ToString(), e.Source); LANSharingApp.closing = true; LANSharingApp.serverThread.Join(); LANSharingApp.clearTempFiles(); LANSharingApp.flushTmpDirectory(); Application.Exit(); } }
// EntryUDP start 2 thread: one for notify presence and one for detect other users on the LAN public void EntryUDP() { try { //create MulticastUdpClient udpClientWrapper = new MulticastUdpClient(multicastIPaddress, port, localIPaddress); udpClientWrapper.UdpMessageReceived += OnUdpMessageReceived; notifyPresenceUDP = new Thread(EntryNotifyPresence); notifyPresenceUDP.IsBackground = true; notifyPresenceUDP.Start(); } catch (Exception e) { Console.WriteLine(e.ToString()); LANSharingApp.LogFile(e.Message, e.ToString(), e.Source); LANSharingApp.closing = true; LANSharingApp.serverThread.Join(); LANSharingApp.clearTempFiles(); LANSharingApp.flushTmpDirectory(); Application.Exit(); } }