public void CloseTest() { int shutdownCount = 0; int closeCount = 0; ManualResetEvent resetEvent = new ManualResetEvent(false); NetworkConnection connection = new NetworkConnection(_client); connection.Shutdown += (sender, args) => { shutdownCount++; resetEvent.Set(); }; connection.ConnectionClosed += (sender, args) => { closeCount++; resetEvent.Set(); }; connection.Start(); connection.Close(); Assert.That(resetEvent.WaitOne(2000)); Assert.That(shutdownCount, Is.EqualTo(0)); Assert.That(closeCount, Is.EqualTo(1)); }
public void RapidReceiveData() { ManualResetEvent resetEvent = new ManualResetEvent(false); NetworkConnection connection = new NetworkConnection(_client); byte[] originalBuffer = new byte[10000]; List<byte> copiedBuffer = new List<byte>(); for (int i = 0; i < 10000; i++) { originalBuffer[i] = (byte) ( i % 100 ); } connection.DataAvailable += (sender, args) => { var receiveBuffer = args.Data; copiedBuffer.AddRange(receiveBuffer); if (copiedBuffer.Count() == 10000) { resetEvent.Set(); } }; connection.Start(); for (int i = 0; i < 1000; i++) { byte[] buffer = new byte[10]; Array.Copy( originalBuffer, i * 10, buffer, 0, 10 ); _serverClient.Send(buffer); } Assert.That(resetEvent.WaitOne(2000)); Assert.That(originalBuffer, Is.EqualTo(copiedBuffer) ); }
private void HandleNewClientConnect( IAsyncResult ar ) { Contract.Requires( ar != null ); try { // Server may be in the process of shutting down. Ignore pending connect notifications. if ( _socket != null ) { Socket clientSocket = _socket.EndAccept( ar ); // Immediately listen for the next clientSession _socket.BeginAccept( HandleNewClientConnect, null ); if ( clientSocket == null ) { ServiceLog.Logger.Warning( "The client connection failed and the client session will be aborted." ); } else { ServiceLog.Logger.Info( "{0} New client connect", clientSocket.GetHashCode() ); // TODO: Unsure if this is really needed. Client sessions remain in memory longer if this is not set //clientSocket.SetSocketOption( SocketOptionLevel.Socket, SocketOptionName.Linger, false ); //clientSocket.SetSocketOption( SocketOptionLevel.Socket, SocketOptionName.KeepAlive, false ); NetworkConnection networkConnection = new NetworkConnection( clientSocket ); _clientConnectCallback( networkConnection ); } } } catch ( Exception ex ) { ServiceLog.Logger.Exception( "Error establishing client connect", ex ); } }
public void StopSendingTest() { int shutdownCount = 0; ManualResetEvent resetEvent = new ManualResetEvent(false); NetworkConnection connection = new NetworkConnection(_client); connection.Shutdown += ( sender, args ) => { shutdownCount++; resetEvent.Set(); }; connection.Start(); _serverClient.Shutdown(SocketShutdown.Send); Assert.That(resetEvent.WaitOne(2000)); Assert.That(shutdownCount, Is.EqualTo(1)); }
public void StartupTest() { NetworkConnection connection = new NetworkConnection(_server); }
public void SocketClosedBeforeSendTest() { NetworkConnection connection = new NetworkConnection(_client); _serverClient.Close(); _client.Close(); Assert.Throws<SocketException>(() => connection.SendData(_buffer)); }
public void SendData() { int eventCount = 0; ManualResetEvent resetEvent = new ManualResetEvent(false); NetworkConnection connection = new NetworkConnection(_client); connection.DataSent += ( sender, args ) => { eventCount++; resetEvent.Set(); }; byte[] buffer = new byte[10]; _serverClient.BeginReceive( buffer, 0, 10, SocketFlags.None, ar =>_serverClient.EndReceive( ar ), null ); connection.SendData(_buffer); Assert.That(resetEvent.WaitOne(2000)); Assert.That(buffer[0], Is.EqualTo(1), "Data not sent to server"); Assert.That(eventCount, Is.EqualTo(1)); }
public void ReceiveDoesNotWorkIfNotStarted() { ManualResetEvent resetEvent = new ManualResetEvent(false); NetworkConnection connection = new NetworkConnection(_client); byte[] buffer = null; connection.DataAvailable += (sender, args) => { buffer = args.Data; resetEvent.Set(); }; // Send data from other end. Object under test should not receive data since it was not started. _serverClient.Send(_buffer); Assert.That(resetEvent.WaitOne(1000), Is.False); }
public void ReceiveData() { ManualResetEvent resetEvent = new ManualResetEvent(false); NetworkConnection connection = new NetworkConnection(_client); byte[] buffer = null; connection.DataAvailable += ( sender, args ) => { buffer = args.Data; resetEvent.Set(); }; // Send data from other end. Place this before the Start method to verify what might happen // on an actual network. Data may arrive before we start to receive it. _serverClient.Send( _buffer ); connection.Start(); Assert.That(resetEvent.WaitOne(2000)); Assert.That(buffer[0], Is.EqualTo(1)); }