public static void Disconnect(Socket sender) { sender.Close(); sender.Dispose(); System.Environment.Exit(1); }
public void Close() { socket.Dispose(); socket.Close(); }
public void Close() { _socket.Dispose(); _socket.Close(); }
public void Close() { _s.Dispose(); _s.Close(); }
/// <summary> /// Disconnect the session /// </summary> /// <returns>'true' if the section was successfully disconnected, 'false' if the section is already disconnected</returns> public virtual bool Disconnect() { if (!IsConnected) { return(false); } if (_disconnecting) { return(false); } // Update the disconnecting flag _disconnecting = true; try { try { // Shutdown the SSL stream // _sslStream.ShutdownAsync().Wait(); } catch (Exception) { } // Dispose the SSL stream & buffer _sslStream.Dispose(); _sslStreamId = null; try { // Shutdown the socket associated with the client Socket.Shutdown(SocketShutdown.Both); } catch (SocketException) { } // Close the session socket Socket.Close(); // Dispose the session socket Socket.Dispose(); // Update the session socket disposed flag IsSocketDisposed = true; } catch (ObjectDisposedException) { } // Update the handshaked flag IsHandshaked = false; // Update the connected flag IsConnected = false; // Update sending/receiving flags _receiving = false; _sending = false; // Clear send/receive buffers ClearBuffers(); // Call the session disconnected handler OnDisconnected(); // Call the session disconnected handler in the server Server.OnDisconnectedInternal(this); // Unregister session Server.UnregisterSession(Id); // Reset the disconnecting flag _disconnecting = false; return(true); }
public void Dispose() { _client.Close(); _client.Dispose(); }
private void Form1_FormClosing(object sender, FormClosingEventArgs e) { soc.Dispose(); }
/// <summary> /// Disconnect the client (synchronous) /// </summary> /// <returns>'true' if the client was successfully disconnected, 'false' if the client is already disconnected</returns> public virtual bool Disconnect() { if (!IsConnected && !IsConnecting) { return(false); } // Cancel connecting operation if (IsConnecting) { Socket.CancelConnectAsync(_connectEventArg); } if (_disconnecting) { return(false); } // Reset connecting & handshaking flags IsConnecting = false; IsHandshaking = false; // Update the disconnecting flag _disconnecting = true; // Reset event args _connectEventArg.Completed -= OnAsyncCompleted; try { try { // Shutdown the SSL stream // _sslStream.ShutdownAsync().Wait(); } catch (Exception) { } // Dispose the SSL stream & buffer _sslStream.Dispose(); _sslStreamId = null; try { // Shutdown the socket associated with the client Socket.Shutdown(SocketShutdown.Both); } catch (SocketException) { } // Close the client socket Socket.Close(); // Dispose the client socket Socket.Dispose(); // Dispose event arguments _connectEventArg.Dispose(); // Update the client socket disposed flag IsSocketDisposed = true; } catch (ObjectDisposedException) { } // Update the handshaked flag IsHandshaked = false; // Update the connected flag IsConnected = false; // Update sending/receiving flags _receiving = false; _sending = false; // Clear send/receive buffers ClearBuffers(); // Call the client disconnected handler OnDisconnected(); // Reset the disconnecting flag _disconnecting = false; return(true); }
/// <summary> /// Connect the client (synchronous) /// </summary> /// <remarks> /// Please note that synchronous connect will not receive data automatically! /// You should use Receive() or ReceiveAsync() method manually after successful connection. /// </remarks> /// <returns>'true' if the client was successfully connected, 'false' if the client failed to connect</returns> public virtual bool Connect() { if (IsConnected || IsHandshaked || IsConnecting || IsHandshaking) { return(false); } // Setup buffers _receiveBuffer = new Buffer(); _sendBufferMain = new Buffer(); _sendBufferFlush = new Buffer(); // Setup event args _connectEventArg = new SocketAsyncEventArgs(); _connectEventArg.RemoteEndPoint = Endpoint; _connectEventArg.Completed += OnAsyncCompleted; // Create a new client socket Socket = CreateSocket(); // Apply the option: dual mode (this option must be applied before connecting) if (Socket.AddressFamily == AddressFamily.InterNetworkV6) { Socket.DualMode = OptionDualMode; } try { // Connect to the server Socket.Connect(Endpoint); } catch (SocketException ex) { // Close the client socket Socket.Close(); // Dispose the client socket Socket.Dispose(); // Dispose event arguments _connectEventArg.Dispose(); // Call the client disconnected handler SendError(ex.SocketErrorCode); OnDisconnected(); return(false); } // Update the client socket disposed flag IsSocketDisposed = false; // Apply the option: keep alive if (OptionKeepAlive) { Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true); } // Apply the option: no delay if (OptionNoDelay) { Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true); } // Prepare receive & send buffers _receiveBuffer.Reserve(OptionReceiveBufferSize); _sendBufferMain.Reserve(OptionSendBufferSize); _sendBufferFlush.Reserve(OptionSendBufferSize); // Reset statistic BytesPending = 0; BytesSending = 0; BytesSent = 0; BytesReceived = 0; // Update the connected flag IsConnected = true; // Call the client connected handler OnConnected(); try { // Create SSL stream _sslStreamId = Guid.NewGuid(); _sslStream = (Context.CertificateValidationCallback != null) ? new SslStream(new NetworkStream(Socket, false), false, Context.CertificateValidationCallback) : new SslStream(new NetworkStream(Socket, false), false); // SSL handshake _sslStream.AuthenticateAsClient(Address, Context.Certificates ?? new X509CertificateCollection(new[] { Context.Certificate }), Context.Protocols, true); } catch (Exception) { SendError(SocketError.NotConnected); DisconnectAsync(); return(false); } // Update the handshaked flag IsHandshaked = true; // Call the session handshaked handler OnHandshaked(); // Call the empty send buffer handler if (_sendBufferMain.IsEmpty) { OnEmpty(); } return(true); }
public static void Json(int itemCount, TestIpc ipcType, int portNumber) { SafeFileHandle pipe = null; Socket socket = null; var start = DateTime.Now; if (ipcType == TestIpc.NamedPipe) { pipe = GetPipeClient("d2n-json-pipe"); } else if (ipcType == TestIpc.TcpSocket) { socket = GetSocket(portNumber); } var itemsSent = 0; var serializer = new JsonSerializer(); var data = TestData.Generate(itemCount); uint bytesWritten = 0; byte[] buffer = new byte[100 * 1024 * 1024]; byte[] headBuffer = new byte[4]; while (true) { var chunk = data.Take(50000); chunk = chunk.ToList(); itemsSent += chunk.Count(); data = data.Skip(chunk.Count()); if (!chunk.Any()) { break; } var stream = new MemoryStream(buffer); var writer = new StreamWriter(stream); var jsonWriter = new JsonTextWriter(writer); serializer.Serialize(jsonWriter, chunk); jsonWriter.Flush(); var bufferSize = (uint)stream.Position; int chunkBytesWritten = 0; if (ipcType == TestIpc.NamedPipe) { chunkBytesWritten = (int)WriteToPipe(pipe, buffer, bufferSize); bytesWritten += (uint)chunkBytesWritten; } else if (ipcType == TestIpc.TcpSocket) { WriteToSocket(socket, BitConverter.GetBytes(bufferSize), 4); chunkBytesWritten = WriteToSocket(socket, buffer, (int)bufferSize); bytesWritten += (uint)chunkBytesWritten; } Console.WriteLine("JSON: {0} %, {1} bytes", ((float)itemsSent / (float)itemCount) * 100, bufferSize); } var duration = DateTime.Now.Subtract(start).TotalSeconds; Console.WriteLine("Total {0} mbytes, duration {1} secs", bytesWritten / 1024 / 1024, duration); if (ipcType == TestIpc.NamedPipe) { Win32.CloseHandle(pipe); } else if (ipcType == TestIpc.TcpSocket) { socket.Dispose(); } }