public void ClientDisconnectTest() { using (ThreadLimitedUdpConnectionListener listener = this.CreateListener(2, new IPEndPoint(IPAddress.Any, 4296), new TestLogger())) using (UdpConnection connection = this.CreateConnection(new IPEndPoint(IPAddress.Loopback, 4296), new TestLogger())) { ManualResetEvent mutex = new ManualResetEvent(false); ManualResetEvent mutex2 = new ManualResetEvent(false); listener.NewConnection += delegate(NewConnectionEventArgs args) { args.Connection.Disconnected += delegate(object sender2, DisconnectedEventArgs args2) { mutex2.Set(); }; mutex.Set(); }; listener.Start(); connection.Connect(); mutex.WaitOne(1000); Assert.AreEqual(ConnectionState.Connected, connection.State); connection.Disconnect("Testing"); mutex2.WaitOne(1000); Assert.AreEqual(ConnectionState.NotConnected, connection.State); } }
public void KeepAliveServerTest() { ManualResetEvent mutex = new ManualResetEvent(false); using (ThreadLimitedUdpConnectionListener listener = this.CreateListener(2, new IPEndPoint(IPAddress.Any, 4296), new TestLogger())) using (UdpConnection connection = this.CreateConnection(new IPEndPoint(IPAddress.Loopback, 4296), new TestLogger())) { UdpConnection client = null; listener.NewConnection += delegate(NewConnectionEventArgs args) { client = (UdpConnection)args.Connection; client.KeepAliveInterval = 100; Thread.Sleep(1050); //Enough time for ~10 keep alive packets mutex.Set(); }; listener.Start(); connection.Connect(); mutex.WaitOne(); Assert.AreEqual(ConnectionState.Connected, client.State); Assert.IsTrue( client.Statistics.TotalBytesSent >= 27 && client.Statistics.TotalBytesSent <= 50, "Sent: " + client.Statistics.TotalBytesSent ); } }
public void ServerExtraDataDisconnectTest() { using (ThreadLimitedUdpConnectionListener listener = this.CreateListener(2, new IPEndPoint(IPAddress.Any, 4296), new TestLogger())) using (UdpConnection connection = this.CreateConnection(new IPEndPoint(IPAddress.Loopback, 4296), new TestLogger())) { string received = null; ManualResetEvent mutex = new ManualResetEvent(false); connection.Disconnected += delegate(object sender, DisconnectedEventArgs args) { // We don't own the message, we have to read the string now received = args.Message.ReadString(); mutex.Set(); }; listener.NewConnection += delegate(NewConnectionEventArgs args) { MessageWriter writer = MessageWriter.Get(SendOption.None); writer.Write("Goodbye"); args.Connection.Disconnect("Testing", writer); }; listener.Start(); connection.Connect(); mutex.WaitOne(5000); Assert.IsNotNull(received); Assert.AreEqual("Goodbye", received); } }
public void ClientDisposeDisconnectTest() { IPEndPoint ep = new IPEndPoint(IPAddress.Loopback, 4296); bool serverConnected = false; bool serverDisconnected = false; bool clientDisconnected = false; using (ThreadLimitedUdpConnectionListener listener = this.CreateListener(2, new IPEndPoint(IPAddress.Any, 4296), new TestLogger())) using (UdpConnection connection = this.CreateConnection(ep, new TestLogger())) { listener.NewConnection += (evt) => { serverConnected = true; evt.Connection.Disconnected += (o, et) => serverDisconnected = true; }; connection.Disconnected += (o, et) => clientDisconnected = true; listener.Start(); connection.Connect(); Thread.Sleep(100); // Gotta wait for the server to set up the events. connection.Dispose(); Thread.Sleep(100); Assert.IsTrue(serverConnected); Assert.IsTrue(serverDisconnected); Assert.IsFalse(clientDisconnected); } }
public void UdpUnreliableMessageSendTest() { byte[] TestData = new byte[] { 1, 2, 3, 4, 5, 6 }; using (ThreadLimitedUdpConnectionListener listener = this.CreateListener(2, new IPEndPoint(IPAddress.Any, 4296), new TestLogger())) using (UdpConnection connection = this.CreateConnection(new IPEndPoint(IPAddress.Loopback, 4296), new TestLogger())) { MessageReader output = null; listener.NewConnection += delegate(NewConnectionEventArgs e) { e.Connection.DataReceived += delegate(DataReceivedEventArgs evt) { output = evt.Message.Duplicate(); }; }; listener.Start(); connection.Connect(); for (int i = 0; i < 4; ++i) { var msg = MessageWriter.Get(SendOption.None); msg.Write(TestData); connection.Send(msg); msg.Recycle(); } Thread.Sleep(10); for (int i = 0; i < TestData.Length; ++i) { Assert.AreEqual(TestData[i], output.ReadByte()); } } }
void Start() { udp = new UdpConnection(); udp.Connect("", 7777); udp.ReceiveEvent += OnReceive; udp.DisConnect(); }
public void UdpIPv4ConnectionTest() { using (ThreadLimitedUdpConnectionListener listener = this.CreateListener(2, new IPEndPoint(IPAddress.Any, 4296), new TestLogger())) using (UdpConnection connection = this.CreateConnection(new IPEndPoint(IPAddress.Loopback, 4296), new TestLogger())) { listener.Start(); connection.Connect(); } }
public void UdpIPv6ConnectionTest() { using (ThreadLimitedUdpConnectionListener listener = this.CreateListener(2, new IPEndPoint(IPAddress.IPv6Any, 4296), new TestLogger(), IPMode.IPv6)) { listener.Start(); using (UdpConnection connection = this.CreateConnection(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 4296), new TestLogger(), IPMode.IPv6)) { connection.Connect(); } } }
// Use this for initialization void Start() { connection = new UdpConnection(); stream = new NetworkStream(); connection.m_Stream = stream; stream.connection = connection; connection.m_OnConnected = OnConnected; connection.m_OnDisconnected = OnDisconnected; bool ret = connection.Connect("192.168.1.168", 9530, "channel"); Debug.Log("ret = " + ret); }
public void UdpIPv4ConnectionTest() { using (ThreadLimitedUdpConnectionListener listener = this.CreateListener(2, new IPEndPoint(IPAddress.Any, 4296), new TestLogger())) using (UdpConnection connection = this.CreateConnection(new IPEndPoint(IPAddress.Loopback, 4296), new TestLogger())) { listener.Start(); connection.Connect(); Assert.AreEqual(ConnectionState.Connected, connection.State); Console.Write($"Client sent {connection.Statistics.TotalBytesSent} bytes "); } }
public void UdpFieldTest() { IPEndPoint ep = new IPEndPoint(IPAddress.Loopback, 4296); using (ThreadLimitedUdpConnectionListener listener = this.CreateListener(2, new IPEndPoint(IPAddress.Any, 4296), new TestLogger())) using (UdpConnection connection = this.CreateConnection(ep, new TestLogger())) { listener.Start(); connection.Connect(); //Connection fields Assert.AreEqual(ep, connection.EndPoint); //UdpConnection fields Assert.AreEqual(new IPEndPoint(IPAddress.Loopback, 4296), connection.EndPoint); Assert.AreEqual(1, connection.Statistics.DataBytesSent); Assert.AreEqual(0, connection.Statistics.DataBytesReceived); } }
public virtual void KeepAliveClientTest() { using (ThreadLimitedUdpConnectionListener listener = this.CreateListener(2, new IPEndPoint(IPAddress.Any, 4296), new TestLogger())) using (UdpConnection connection = this.CreateConnection(new IPEndPoint(IPAddress.Loopback, 4296), new TestLogger())) { listener.Start(); connection.Connect(); connection.KeepAliveInterval = 100; Thread.Sleep(1050); //Enough time for ~10 keep alive packets Assert.AreEqual(ConnectionState.Connected, connection.State); Assert.IsTrue( connection.Statistics.TotalBytesSent >= 30 && connection.Statistics.TotalBytesSent <= 50, "Sent: " + connection.Statistics.TotalBytesSent ); } }
static void Main(string[] args) { using (UdpConnection connection = new UdpConnection()) { ManualResetEvent e = new ManualResetEvent(false); //Whenever we receive data print the number of bytes and how it was sent connection.DataReceived += (object sender, DataEventArgs a) => Console.WriteLine("Received {0} bytes via {1}!", a.Bytes.Length, a.SendOption); //When the end point disconnects from us then release the main thread and exit connection.Disconnected += (object sender, DisconnectedEventArgs a) => e.Set(); //Connect to a server connection.Connect(new NetworkEndPoint("127.0.0.1", 4296)); //Wait until the end point disconnects from us e.WaitOne(); } }
public void ServerDisconnectTest() { using (ThreadLimitedUdpConnectionListener listener = this.CreateListener(2, new IPEndPoint(IPAddress.Any, 4296), new TestLogger())) using (UdpConnection connection = this.CreateConnection(new IPEndPoint(IPAddress.Loopback, 4296), new TestLogger())) { SemaphoreSlim mutex = new SemaphoreSlim(0, 100); ManualResetEventSlim serverMutex = new ManualResetEventSlim(false); connection.Disconnected += delegate(object sender, DisconnectedEventArgs args) { mutex.Release(); }; listener.NewConnection += delegate(NewConnectionEventArgs args) { mutex.Release(); // This has to be on a new thread because the client will go straight from Connecting to NotConnected ThreadPool.QueueUserWorkItem(_ => { serverMutex.Wait(500); args.Connection.Disconnect("Testing"); }); }; listener.Start(); connection.Connect(); mutex.Wait(500); Assert.AreEqual(ConnectionState.Connected, connection.State); serverMutex.Set(); mutex.Wait(500); Assert.AreEqual(ConnectionState.NotConnected, connection.State); } }
public void UdpHandshakeTest() { byte[] TestData = new byte[] { 1, 2, 3, 4, 5, 6 }; using (ThreadLimitedUdpConnectionListener listener = this.CreateListener(2, new IPEndPoint(IPAddress.Any, 4296), new TestLogger())) using (UdpConnection connection = this.CreateConnection(new IPEndPoint(IPAddress.Loopback, 4296), new TestLogger())) { listener.Start(); MessageReader output = null; listener.NewConnection += delegate(NewConnectionEventArgs e) { output = e.HandshakeData.Duplicate(); }; connection.Connect(TestData); Thread.Sleep(10); for (int i = 0; i < TestData.Length; ++i) { Assert.AreEqual(TestData[i], output.ReadByte()); } } }
public void ServerDisconnectTest() { using (ThreadLimitedUdpConnectionListener listener = this.CreateListener(2, new IPEndPoint(IPAddress.Any, 4296), new TestLogger())) using (UdpConnection connection = this.CreateConnection(new IPEndPoint(IPAddress.Loopback, 4296), new TestLogger())) { ManualResetEvent mutex = new ManualResetEvent(false); connection.Disconnected += delegate(object sender, DisconnectedEventArgs args) { mutex.Set(); }; listener.NewConnection += delegate(NewConnectionEventArgs args) { args.Connection.Disconnect("Testing"); }; listener.Start(); connection.Connect(); mutex.WaitOne(); } }
public void MixedConnectionTest() { using (ThreadLimitedUdpConnectionListener listener2 = this.CreateListener(4, new IPEndPoint(IPAddress.IPv6Any, 4296), new ConsoleLogger(true), IPMode.IPv6)) { listener2.Start(); listener2.NewConnection += (evt) => { Console.WriteLine($"Connection: {evt.Connection.EndPoint}"); }; using (UdpConnection connection = this.CreateConnection(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 4296), new TestLogger())) { connection.Connect(); Assert.AreEqual(ConnectionState.Connected, connection.State); } using (UdpConnection connection2 = this.CreateConnection(new IPEndPoint(IPAddress.IPv6Loopback, 4296), new TestLogger(), IPMode.IPv6)) { connection2.Connect(); Assert.AreEqual(ConnectionState.Connected, connection2.State); } } }
public bool Connect(IPAddress address, int port) { CreateConnection(address, port); Connection.Connect(); return(IsConnected); }
void Connect() { _udp.Connect("127.0.0.1", 7777); }
void Button_Connect() { udp.Connect("127.0.0.1", 999); }