private void Release(bool userRequestedDisconnect) { lock (netLock) { cancellationToken.Dispose(); cancellationToken = null; if (netWriter != null) { netWriter.Dispose(); netWriter = null; } if (netReader != null) { netReader.Dispose(); netReader = null; } if (netStream != null) { netStream.Dispose(); netStream = null; } socket.Close(); socket = null; netFilter = null; } OnDisconnected(new DisconnectedEventArgs(userRequestedDisconnect)); connectionFree.Set(); }
private void Release( bool userRequestedDisconnect ) { lock (netLock) { cancellationToken.Dispose(); cancellationToken = null; if (netWriter != null) { netWriter.Dispose(); netWriter = null; } if (netReader != null) { netReader.Dispose(); netReader = null; } if (netStream != null) { netStream.Dispose(); netStream = null; } socket.Close(); socket = null; netFilter = null; } OnDisconnected( new DisconnectedEventArgs( userRequestedDisconnect ) ); connectionFree.Set(); }
void ConnectCompleted(Socket sock, IAsyncResult asyncResult, CancellationTokenSource connectToken) { if (connectToken.IsCancellationRequested) { DebugLog.WriteLine("TcpConnection", "Connect request was cancelled"); return; } else if (sock == null) { DebugLog.WriteLine("TcpConnection", "Timed out while connecting"); OnDisconnected(EventArgs.Empty); return; } try { sock.EndConnect(asyncResult); } catch (Exception ex) { DebugLog.WriteLine("TcpConnection", "Socket exception while connecting: {0}", ex); OnDisconnected(EventArgs.Empty); return; } netLock.EnterWriteLock(); try { if (!sock.Connected) { DebugLog.WriteLine("TcpConnection", "Unable to connect"); OnDisconnected(EventArgs.Empty); return; } DebugLog.WriteLine("TcpConnection", "Connected!"); filter = null; wantsNetShutdown = false; netStream = new NetworkStream(sock, false); netReader = new BinaryReader(netStream); netWriter = new BinaryWriter(netStream); // initialize our network thread netThread = new Thread(NetLoop); netThread.Name = "TcpConnection Thread"; netThread.Start(sock); } finally { netLock.ExitWriteLock(); } OnConnected(EventArgs.Empty); }
public override void SetNetEncryptionFilter(NetFilterEncryption filter) { lock (netLock) { if (socket != null) { netFilter = filter; } } }
/// <summary> /// Sets the network encryption filter for this connection /// </summary> /// <param name="filter">filter implementing <see cref="NetFilterEncryption"/></param> public override void SetNetEncryptionFilter(NetFilterEncryption filter) { while (!wantsNetShutdown && !netLock.TryEnterWriteLock(500)) { } this.filter = filter; if (netLock.IsWriteLockHeld) { netLock.ExitWriteLock(); } }
private void ConnectCompleted(bool success) { // Always discard result if our request was cancelled if (cancellationToken.IsCancellationRequested) { DebugLog.WriteLine("TcpConnection", "Connection request to {0} was cancelled", destination); if (success) { Shutdown(); } Release(userRequestedDisconnect: true); return; } else if (!success) { DebugLog.WriteLine("TcpConnection", "Timed out while connecting to {0}", destination); Release(userRequestedDisconnect: false); return; } DebugLog.WriteLine("TcpConnection", "Connected to {0}", destination); try { lock (netLock) { netStream = new NetworkStream(socket, false); netReader = new BinaryReader(netStream); netWriter = new BinaryWriter(netStream); netFilter = null; netThread = new Thread(NetLoop); netThread.Name = "TcpConnection Thread"; } netThread.Start(); OnConnected(EventArgs.Empty); } catch (Exception ex) { DebugLog.WriteLine("TcpConnection", "Exception while setting up connection to {0}: {1}", destination, ex); Release(userRequestedDisconnect: false); } }
private void ConnectCompleted(bool success) { // Always discard result if our request was cancelled if (cancellationToken.IsCancellationRequested) { DebugLog.WriteLine("TcpConnection", "Connection request to {0} was cancelled", destination); if (success) Shutdown(); Release( userRequestedDisconnect: true ); return; } else if (!success) { DebugLog.WriteLine("TcpConnection", "Timed out while connecting to {0}", destination); Release( userRequestedDisconnect: false ); return; } DebugLog.WriteLine("TcpConnection", "Connected to {0}", destination); try { lock (netLock) { netStream = new NetworkStream(socket, false); netReader = new BinaryReader(netStream); netWriter = new BinaryWriter(netStream); netFilter = null; netThread = new Thread(NetLoop); netThread.Name = "TcpConnection Thread"; } netThread.Start(); OnConnected(EventArgs.Empty); } catch (Exception ex) { DebugLog.WriteLine("TcpConnection", "Exception while setting up connection to {0}: {1}", destination, ex); Release( userRequestedDisconnect: false ); } }
/// <summary> /// Sets the network encryption filter for this connection /// </summary> /// <param name="filter">filter implementing <see cref="NetFilterEncryption"/></param> public abstract void SetNetEncryptionFilter(NetFilterEncryption filter);
/// <summary> /// Sets the network encryption filter for this connection /// </summary> /// <param name="filter">filter implementing <see cref="NetFilterEncryption"/></param> public override void SetNetEncryptionFilter(NetFilterEncryption filter) { this.filter = filter; }