public bool pkt_proc(pkt p, IPEndPoint r) { this.r = r; // update remote info dynamically switch (p.type) { case "message": hear(p.id, p.content); break; case "message_response": int pid = int.Parse(p.id); if (!pkthash.ContainsKey(pid)) { break; } byte[] buf = (byte[])pkthash[pid]; pkthash.Remove(pid); msghash.Remove(buf); Timer retrytmr = (Timer)tmrhash[buf]; tmrhash.Remove(buf); retrytmr.Change(Timeout.Infinite, Timeout.Infinite); retrytmr.Dispose(); break; default: break; } return(true); }
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) { } }
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()); } }
public bool pkt_proc(pkt pkt, IPEndPoint r) { this.r = r; return(chatss.pkt_proc(pkt, r)); }