public void sl(IRawServer rawServer, DummyEncrypter encrypter, int port) { rawServer.Bind(port, "127.0.0.1", false); this.handler = encrypter; Thread t = new Thread(Go); t.Start(); }
/// <summary> /// 构造函数 /// </summary> /// <param name="rawServer">服务器</param> /// <param name="socket">套接字</param> /// <param name="handler">封装连接管理类</param> public DummySingleSocket(DummyRawServer rawServer, Socket socket, DummyEncrypter handler) { this.rawServer = rawServer; this.socket = socket; this.handler = handler; buffer = new LinkedList <byte[]>(); lastHit = DateTime.Now; isConnected = false; }
public DummySingleSocket StartConnection(IPEndPoint dns, DummyEncrypter handler) { if (handler == null) { handler = this.handler; } Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); sock.Blocking = false; try { sock.Connect(dns); } catch { } poll.Register(sock, PollMode.PollIn); DummySingleSocket s = new DummySingleSocket(this, sock, handler); singleSockets[sock.Handle] = s; return(s); }
public void ListenForever(DummyEncrypter handler) { this.handler = handler; try { while (!doneFlag.IsSet) { try { double period; PopExternal(); if (tasks.Count == 0) { period = 2 ^ 30; } else { OriginalTask t = (OriginalTask)tasks[0]; period = (t.When - DateTime.Now).TotalSeconds; if (period < 0) { period = 0; } } List <PollItem> events = poll.Select((int)(period * 1000000)); if (doneFlag.IsSet) { return; } while (tasks.Count > 0 && tasks[0].When <= DateTime.Now) { OriginalTask t = tasks[0]; tasks.RemoveAt(0); try { t.TaskFunction(); } //TODO: except KeyboardInterrupt: // print_exc() // return catch { if (noisy) { ; } //TODO: print_exc() } } CloseDead(); HandleEvents(events); if (doneFlag.IsSet) { return; } CloseDead(); } //TODO: //except error: // if self.doneflag.isSet(): // return //except KeyboardInterrupt: // print_exc() // return catch { //TODO: print_exc() } } } finally { foreach (DummySingleSocket ss in singleSockets.Values) { ss.Close(true); } server.Close(); } }
public void Test1() { Flag fa = new Flag(); Flag fb = new Flag(); try { DummyEncrypter da = new DummyEncrypter(); IRawServer sa = new RawServer(fa, 100, 100, false); TestHelper ta = new TestHelper(); ta.Loop(sa); ta.sl(sa, da, 6800); DummyEncrypter db = new DummyEncrypter(); IRawServer sb = new RawServer(fb, 100, 100, false); TestHelper tb = new TestHelper(); tb.Loop(sb); tb.sl(sb, db, 6801); Thread.Sleep(50); ISingleSocket ca = sa.StartConnect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 6801), null); Thread.Sleep(50); Assert.AreEqual(0, da.externalDummySockets.Count); Assert.AreEqual(0, da.dataIn.Count); Assert.AreEqual(0, da.lostDummySockets.Count); Assert.AreEqual(1, db.externalDummySockets.Count); ISingleSocket cb = db.externalDummySockets[0]; db.externalDummySockets.Clear(); Assert.AreEqual(0, db.dataIn.Count); Assert.AreEqual(0, db.lostDummySockets.Count); ca.Write(new byte[] { (byte)'a', (byte)'a', (byte)'a' }); cb.Write(new byte[] { (byte)'b', (byte)'b', (byte)'b' }); Thread.Sleep(50); Assert.AreEqual(0, da.externalDummySockets.Count); //da.data_in = ca, 'bbb' da.dataIn.Clear(); Assert.AreEqual(0, da.lostDummySockets.Count); Assert.AreEqual(0, db.externalDummySockets.Count); //db.data_in = cb, 'aaa' db.dataIn.Clear(); Assert.AreEqual(0, db.lostDummySockets.Count); ca.Write(new byte[] { (byte)'c', (byte)'c', (byte)'c' }); cb.Write(new byte[] { (byte)'d', (byte)'d', (byte)'d' }); Thread.Sleep(50); Assert.AreEqual(0, da.externalDummySockets.Count); //da.data_in = ca, 'ddd' da.dataIn.Clear(); Assert.AreEqual(0, da.lostDummySockets.Count); Assert.AreEqual(0, db.externalDummySockets.Count); //db.data_in = cb, 'ccc' db.dataIn.Clear(); Assert.AreEqual(0, db.lostDummySockets.Count); ca.Close(); Thread.Sleep(50); Assert.AreEqual(0, da.externalDummySockets.Count); Assert.AreEqual(0, da.dataIn.Count); Assert.AreEqual(0, da.lostDummySockets.Count); Assert.AreEqual(0, db.externalDummySockets.Count); Assert.AreEqual(0, db.dataIn.Count); Assert.AreEqual(1, db.lostDummySockets.Count); db.lostDummySockets.Clear(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } finally { fa.Set(); fb.Set(); } }