Esempio n. 1
0
 void hear_task()
 {
     try
     {
         byte[] rcvBuffer;
         pkt    p = new pkt();
         while (true)
         {
             rcvBuffer = uc.Receive(ref r);
             p.parse(rcvBuffer);
             pkt_proc(p, r);
         }
     }
     catch (Exception) { }
 }
Esempio n. 2
0
        void server_task(int port)
        {
            try
            {
                uc = new UdpClient(port);
                IPEndPoint r = new IPEndPoint(IPAddress.Any, port);
                byte[]     rcvBuffer;
                pkt        p = new pkt();
                while (true)
                {
                    rcvBuffer = uc.Receive(ref r);
                    p.parse(rcvBuffer);
                    switch (p.type)
                    {
                    case "query":
                        pkt    n   = new query_response_pkt(username);
                        byte[] buf = n.pack();
                        uc.SendAsync(buf, buf.Length, r);
                        break;

                    case "query_response":
                        adduser(r, p.user);
                        break;

                    case "notification":
                        show_notification(r, p.user, p.content);
                        break;

                    case "message":
                        log("I: recv chat from " + p.user + " " + r.Address + " msg " + p.content);
                        if (chathash.ContainsKey(r.Address))
                        {
                            if (((rchatform)chathash[r.Address]).IsDisposed)
                            {
                                chathash.Remove(r.Address);
                            }
                            else
                            {
                                ((rchatform)chathash[r.Address]).pkt_proc(p, r);     // reuse old session
                                ((rchatform)chathash[r.Address]).Activate();
                                break;
                            }
                        }
                        rchatform f = new rchatform(log, username, p.user, r);
                        Thread    t = new Thread(() => Application.Run(f));
                        t.IsBackground = true;
                        t.Start();
                        f.pkt_proc(p, r);
                        chathash.Add(r.Address, f);     // keep only one session per ip
                        break;

                    default:
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                log("!E: daemon " + e.ToString());
            }
        }