示例#1
0
文件: rchat.cs 项目: lazebird/rabbit
        public void new_chat(IPEndPoint r, string ruser)
        {
            if (chathash.ContainsKey(r.Address))
            {
                if (((rchatform)chathash[r.Address]).IsDisposed)
                {
                    chathash.Remove(r.Address);
                }
                else
                {
                    return;
                }
            }
            log("I: new chat to " + ruser + " " + r.Address);
            rchatform f = new rchatform(log, username, ruser, r);
            Thread    t = new Thread(() => Application.Run(f));

            t.IsBackground = true;
            t.Start();
            chathash.Add(r.Address, f);
        }
示例#2
0
文件: rchat.cs 项目: lazebird/rabbit
        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());
            }
        }