public async Task OnConnectionAcceptedTest() { const int port = 12347; using (var connectionListener = new ConnectionListener(port)) { using (var client = new TcpClient()) { var isAccepted = false; connectionListener.OnConnectionAccepted += (s, e) => { isAccepted = true; }; connectionListener.Start(); await client.ConnectAsync("localhost", port); await Task.Delay(100); Assert.IsTrue(connectionListener.IsListening, "Connection Listerner is listening"); Assert.IsTrue(client.Connected, "Client is connected"); Assert.IsTrue(isAccepted, "The flag was set"); client.Close(); } connectionListener.Stop(); } }
public async Task UpgradeToSecureAsServerAndClientAsync_ReturnTrue() { Assert.Ignore(); var tempPath = System.IO.Path.GetTempPath() + "certificate.pfx"; var certificate = Mocks.CertificateHelper.CreateOrLoadCertificate(tempPath, Localhost, "password"); ConnectionListener.Start(); await Client.ConnectAsync(Localhost, Port); ConnectionListener.OnConnectionAccepting += (s, e) => { using (var cn = new Connection(e.Client)) { cn.UpgradeToSecureAsServerAsync(certificate).Wait(); } }; using (var cn = new Connection(Client)) { var result = await cn.UpgradeToSecureAsClientAsync(); Assert.IsTrue(result); Assert.IsTrue(cn.IsActiveStreamSecure); } }
public async Task OnConnectionFailureTest() { Assert.Ignore("Fix"); const int port = 12348; using (var connectionListener = new ConnectionListener(port)) { using (var client = new TcpClient()) { var isFailure = false; connectionListener.OnConnectionAccepting += (s, e) => { e.Cancel = true; }; connectionListener.OnConnectionFailure += (s, e) => { isFailure = true; }; connectionListener.Start(); await client.ConnectAsync("localhost", port); connectionListener.Stop(); Assert.IsTrue(isFailure); } } }
public async Task ConnectionWriteTest(int port) { if (Environment.GetEnvironmentVariable("APPVEYOR") == "True") { Assert.Inconclusive("Can not test in AppVeyor"); } var message = Encoding.ASCII.GetBytes("HOLA"); using (var connectionListener = new ConnectionListener(port)) { using (var client = new TcpClient()) { connectionListener.Start(); connectionListener.OnConnectionAccepting += (s, e) => { e.Client?.GetStream().Write(message, 0, message.Length); }; await client.ConnectAsync("localhost", port); await Task.Delay(400); var connection = new Connection(client, Encoding.ASCII, "\r\n", true, 0); var response = await connection.ReadTextAsync(); Assert.IsNotNull(response); Assert.AreEqual("HOLA", response); } } }
public async Task OnConnectionAcceptingTest() { const int port = 12345; using (var connectionListener = new ConnectionListener(port)) { using (var client = new TcpClient()) { var isAccepting = false; connectionListener.Start(); connectionListener.OnConnectionAccepting += (s, e) => { Assert.IsTrue(e.Client.Connected); isAccepting = true; }; await client.ConnectAsync("localhost", port); await Task.Delay(100); Assert.IsTrue(connectionListener.IsListening); Assert.IsTrue(client.Connected); Assert.IsTrue(isAccepting); client.Close(); } connectionListener.Stop(); } }
/// <summary> /// Runs a client disconnect test on the given listener and connection. /// </summary> /// <param name="listener">The listener to test.</param> /// <param name="connection">The connection to test.</param> internal static void RunClientDisconnectTest(ConnectionListener listener, Connection connection) { ManualResetEvent mutex = new ManualResetEvent(false); ManualResetEvent mutex2 = new ManualResetEvent(false); listener.NewConnection += delegate(object sender, NewConnectionEventArgs args) { args.Connection.Disconnected += delegate(object sender2, DisconnectedEventArgs args2) { mutex2.Set(); }; mutex.Set(); }; listener.Start(); connection.Connect(); mutex.WaitOne(); connection.Close(); mutex2.WaitOne(); }
/// <summary> /// Runs a general test on the given listener and connection. /// </summary> /// <param name="listener">The listener to test.</param> /// <param name="connection">The connection to test.</param> internal static void RunServerToClientTest(ConnectionListener listener, Connection connection, int dataSize, SendOption sendOption) { //Setup meta stuff byte[] data = BuildData(dataSize); ManualResetEvent mutex = new ManualResetEvent(false); //Setup listener listener.NewConnection += delegate(object sender, NewConnectionEventArgs args) { args.Connection.SendBytes(data, sendOption); }; listener.Start(); //Setup conneciton connection.DataReceived += delegate(object sender, DataReceivedEventArgs args) { Trace.WriteLine("Data was received correctly."); for (int i = 0; i < data.Length; i++) { Assert.AreEqual(data[i], args.Bytes[i]); } Assert.AreEqual(sendOption, args.SendOption); mutex.Set(); }; connection.Connect(); //Wait until data is received mutex.WaitOne(); }
public static void Main(string[] args) { md5 = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")); //System.Diagnostics.Debug.WriteLine ("md5 " + MD5 ("linearch2570" + MD5("4qwh0lyg4n5134"))); dbconn = new MySql.Data.MySqlClient.MySqlConnection(myConnectionString); dbconn.Open(); AddDataHandler(0, Login); AddDataHandler(1, Relog); //2 reserved for register //3 reserved for character creation //4 reserved for logout AddDataHandler(5, RoomList); AddDataHandler(6, CreateRoom); AddDataHandler(7, JoinRoom); AddDataHandler(8, RoomStuff); /*int count = 0; * for (int i = 3000; i < 10000; i++) { * try{ * TcpConnectionListener l = new TcpConnectionListener(IPAddress.Any, i); * l.Start (); * listeners.Add(l); * count++; * }catch(Exception ex){ * Console.WriteLine ("Failed listening on : " + i); * count--; * } * } * Console.WriteLine ("Total listeners : " + count);*/ listener = new TcpConnectionListener(IPAddress.Any, 2570); listener.NewConnection += NewConnectionHandler; listener.Start(); Console.WriteLine("Server started!"); Console.WriteLine("Type 'exit' to exit"); while (true) { string i = Console.ReadLine(); if (i == "exit") { break; } } }
public void Start() { if (Interlocked.CompareExchange(ref isInActivating, ActiveSentinel, NoneSentinel) != NoneSentinel) { throw new InvalidOperationException("activated"); } connectionListener = binding.CreateConnectionListener(serviceProvider); connectionListener.Start(); }
public void Start() { ConnectionListener listener = new ConnectionListener(port); listener.ClientConnected += listener_ClientConnected; listener.ClientDisconnected += listener_ClientDisconnected; listener.ClientMessageReceived += listener_ClientMessageReceived; listener.Start(); }
public void UsingLoopback_CanListen() { const int port = 12346; using (var connectionListener = new ConnectionListener(System.Net.IPAddress.Parse("127.0.0.1"), port)) { connectionListener.Start(); Assert.IsTrue(connectionListener.IsListening); connectionListener.Stop(); } }
/// <summary> /// Runs a general test on the given listener and connection. /// </summary> /// <param name="listener">The listener to test.</param> /// <param name="connection">The connection to test.</param> internal static void RunClientToServerTest(ConnectionListener listener, Connection connection, int headerSize, int totalHandshakeSize, SendOption sendOption) { //Setup meta stuff byte[] data = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; ManualResetEvent mutex = new ManualResetEvent(false); ManualResetEvent mutex2 = new ManualResetEvent(false); //Setup listener listener.NewConnection += delegate(object sender, NewConnectionEventArgs args) { args.Connection.DataReceived += delegate(object innerSender, DataReceivedEventArgs innerArgs) { Trace.WriteLine("Data was received correctly."); for (int i = 0; i < data.Length; i++) { Assert.AreEqual(data[i], innerArgs.Bytes[i]); } Assert.AreEqual(sendOption, innerArgs.SendOption); Assert.AreEqual(0, args.Connection.Statistics.DataBytesSent); Assert.AreEqual(data.Length, args.Connection.Statistics.DataBytesReceived); Assert.AreEqual(0, args.Connection.Statistics.TotalBytesSent); Assert.AreEqual(data.Length + headerSize, args.Connection.Statistics.TotalBytesReceived); mutex2.Set(); }; mutex.Set(); }; listener.Start(); //Connect connection.Connect(); mutex.WaitOne(); connection.SendBytes(data, sendOption); //Wait until data is received mutex2.WaitOne(); Assert.AreEqual(data.Length + 1, connection.Statistics.DataBytesSent); Assert.AreEqual(0, connection.Statistics.DataBytesReceived); Assert.AreEqual(totalHandshakeSize + data.Length + headerSize, connection.Statistics.TotalBytesSent); Assert.AreEqual(0, connection.Statistics.TotalBytesReceived); }
public void OnListenerStoppedTest() { const int port = 12349; using (var connectionListener = new ConnectionListener(port)) { var isStopped = false; connectionListener.Start(); connectionListener.OnListenerStopped += (s, e) => { isStopped = true; }; Assert.IsTrue(connectionListener.IsListening); connectionListener.Stop(); Assert.IsFalse(connectionListener.IsListening); Assert.IsTrue(isStopped); connectionListener.Stop(); } }
/// <summary> /// Runs a server disconnect test on the given listener and connection. /// </summary> /// <param name="listener">The listener to test.</param> /// <param name="connection">The connection to test.</param> internal static void RunServerDisconnectTest(ConnectionListener listener, SCMTOperationCore.Connection.Connection connection) { ManualResetEvent mutex = new ManualResetEvent(false); connection.Disconnected += delegate(object sender, DisconnectedEventArgs args) { mutex.Set(); }; listener.NewConnection += delegate(object sender, NewConnectionEventArgs args) { args.Connection.Close(); }; listener.Start(); connection.Connect(); mutex.WaitOne(); }
public async Task WriteLineAsync_MessageEqualResponse() { ConnectionListener.OnConnectionAccepting += (s, e) => { using (var cn = new Connection(e.Client)) { cn.WriteLineAsync(Message).Wait(); } }; ConnectionListener.Start(); await Client.ConnectAsync(Localhost, Port); using (var cn = new Connection(Client, Encoding.UTF8, "\r\n", true, 0)) { var response = await cn.ReadLineAsync(); Assert.IsNotNull(response); Assert.AreEqual(Message.Remove(MessageBytes.Length - 2), response); } }
public async Task EnableContinousReading_ThrowsInvalidOperationException() { ConnectionListener.OnConnectionAccepting += (s, e) => { using (var cn = new Connection(e.Client)) { cn.WriteDataAsync(MessageBytes, true).Wait(); } }; ConnectionListener.Start(); await Client.ConnectAsync(Localhost, Port); using (var cn = new Connection(Client)) { Assert.ThrowsAsync <InvalidOperationException>(async() => { await cn.ReadLineAsync(); }); } }
public static void Main(string[] args) { NetworkEndPoint endPoint = new NetworkEndPoint(IPAddress.Any, 4296); listener = new TcpConnectionListener(endPoint); listener.NewConnection += NewConnectionHandler; Console.WriteLine("Starting server!"); Console.WriteLine("Server listening on " + (listener as TcpConnectionListener).EndPoint); listener.Start(); Console.WriteLine("Press any key to continue..."); Console.ReadKey(); listener.Close(); Environment.Exit(0); }
public async Task WriteDataAsync_MessageEqualsResponse(bool forceFlush, int port) { ConnectionListener = new ConnectionListener(port); ConnectionListener.OnConnectionAccepting += (s, e) => { using (var cn = new Connection(e.Client)) { cn.WriteDataAsync(MessageBytes, forceFlush).Wait(); } }; ConnectionListener.Start(); await Client.ConnectAsync(Localhost, port); using (var cn = new Connection(Client, Encoding.UTF8, "\r\n", true, 0)) { var response = await cn.ReadDataAsync(); Assert.IsNotNull(response); Assert.AreEqual(MessageBytes, response, $"Using forceFlush: {forceFlush}"); } }
public async Task ConnectionOpenTest(int port) { if (Environment.GetEnvironmentVariable("APPVEYOR") == "True") { Assert.Inconclusive("Can not test in AppVeyor"); } using (var connectionListener = new ConnectionListener(port)) { using (var client = new TcpClient()) { connectionListener.Start(); await client.ConnectAsync("localhost", port); await Task.Delay(400); var connection = new Connection(client); Assert.IsTrue(connectionListener.IsListening); Assert.IsTrue(connection.IsConnected); } } }
public override void Start() { if (IsStarted()) { throw new InvalidOperationException("Server is already running."); } if (Port == 0) { throw new InvalidOperationException("Port must be set prior to starting server."); } if (KeyHash == null) { throw new InvalidOperationException("Key hash must be set prior to starting server."); } if (UseSSL && ServerCertificate == null) { throw new InvalidOperationException("ServerCertificate must be set if using SSL."); } //make sure we are configured properly ConnectorInfoManagerFactory.GetInstance().GetLocalManager(); _requestCount = 0; _pendingRequests.Clear(); TcpListener socket = CreateServerSocket(); ConnectionListener listener = new ConnectionListener(this, socket); listener.Start(); _listener = listener; }
public override void Start() { if (IsStarted()) { throw new InvalidOperationException("Server is already running."); } if (Port == 0) { throw new InvalidOperationException("Port must be set prior to starting server."); } if (KeyHash == null) { throw new InvalidOperationException("Key hash must be set prior to starting server."); } if (UseSSL && ServerCertificate == null) { throw new InvalidOperationException("ServerCertificate must be set if using SSL."); } //make sure we are configured properly ConnectorInfoManagerFactory.GetInstance().GetLocalManager(); _requestCount = 0; /* * the Java and .Net dates have a diferent starting point: zero milliseconds in Java corresponds to January 1, 1970, 00:00:00 GMT (aka “the epoch”). * In .Net zero milliseconds* corresponds to 12:00 A.M., January 1, 0001 GMT. * So the basic is to bridge over the reference points gap with adding (or substracting) the corresponding number of milliseconds * such that zero milliseconds in .Net is mapped to -62135769600000L milliseconds in Java. * This number of milliseconds corresponds to GMT zone, so do not forget to include your time zone offset into the calculations. */ _startDate = (DateTime.UtcNow.Ticks - 621355968000000000) / 10000; _pendingRequests.Clear(); TcpListener socket = CreateServerSocket(); ConnectionListener listener = new ConnectionListener(this, socket); listener.Start(); _listener = listener; }
public void WhenTheConnectionListenerStartsListeningOnThePort(int port) { ConnectionListener.Start(port); }
public void Start() { _listener.Start(); }