public void CanConnectUsingCallbackToExampleSite() { TimeSpan timeout = TimeSpan.FromSeconds(2); ManualResetEvent check = new ManualResetEvent(false); IPAddress[] addresses = Dns.GetHostAddresses("www.example.com"); IPEndPoint endpoint = new IPEndPoint(addresses[0], 80); using (CompletionThread worker = new CompletionThread()) { SocketFactory factory = new SocketFactory(worker); TcpSocket socket = factory.Tcp(); socket.Bind(); worker.Start(); TcpSocketConnectCallback onConnected = result => { check.Set(); }; socket.Connect(endpoint, onConnected); bool completed = check.WaitOne(timeout); Assert.That(completed, Is.True); } }
public void CanSendDataUsingCallbackToExampleSite() { TimeSpan timeout = TimeSpan.FromSeconds(2); ManualResetEvent check = new ManualResetEvent(false); IPAddress[] addresses = Dns.GetHostAddresses("www.example.com"); IPEndPoint endpoint = new IPEndPoint(addresses[0], 80); using (CompletionThread worker = new CompletionThread()) { SocketFactory factory = new SocketFactory(worker); TcpSocket socket = factory.Tcp(); socket.Bind(); worker.Start(); TcpSocketSendCallback onSent = result => { check.Set(); }; TcpSocketConnectCallback onConnected = result => { string request = "GET /index.html HTTP/1.1\r\nHost: www.example.com\r\n\r\n"; byte[] data = Encoding.ASCII.GetBytes(request); socket.Send(data, onSent); }; socket.Connect(endpoint, onConnected); bool completed = check.WaitOne(timeout); Assert.That(completed, Is.True); } }
public void Connect(IPEndPoint endpoint, TcpSocketConnectCallback callback) { TcpSocketConnectRoutine routine = new TcpSocketConnectRoutine(handle, worker, endpoint); TcpSocketConnectResult result = new TcpSocketConnectResult { Handle = handle, Socket = this, Endpoint = endpoint, OnConnected = callback }; routine.Execute(result); }