public void TearDown() { _socket.Dispose(); _client.Dispose(); _listener.Dispose(); _wrapper.Dispose(); }
public static async Task <ShoutcastStream> ConnectAsync(Uri serverUrl, ShoutcastStreamFactoryConnectionSettings settings) { //http://www.smackfu.com/stuff/programming/shoutcast.html ShoutcastStream shoutStream = null; ShoutcastStreamFactoryInternalConnectResult result = await ConnectInternalAsync(serverUrl, settings); SocketWrapper socketWrapper = SocketWrapperFactory.CreateSocketWrapper(result); shoutStream = new ShoutcastStream(serverUrl, settings, socketWrapper); string httpLine = result.httpResponse.Substring(0, result.httpResponse.IndexOf('\n')).Trim(); if (string.IsNullOrWhiteSpace(httpLine)) { throw new InvalidOperationException("httpLine is null or whitespace"); } var action = ParseHttpCodeAndResponse(httpLine, result.httpResponse, shoutStream); //todo handle when we get a text/html page. if (action != null) { switch (action.ActionType) { case ConnectionActionType.Success: var headers = ParseResponse(result.httpResponse, shoutStream); await shoutStream.HandleHeadersAsync(headers); return(shoutStream); case ConnectionActionType.Fail: throw action.ActionException; case ConnectionActionType.Redirect: { //clean up. shoutStream.Dispose(); return(await ConnectAsync(action.ActionUrl, settings)); } default: socketWrapper.Dispose(); throw new Exception("We weren't able to connect for some reason."); } } else { socketWrapper.Dispose(); throw new Exception("We weren't able to connect for some reason."); } }
public void Dispose() { if (_wrapper != null) { _wrapper.Dispose(); } }
private void MoverClientForm_FormClosing(object sender, FormClosingEventArgs e) { // 服务端通信 moverComm.FinishClient(); // PLC通信 socketWrapper.Dispose(); }
public override void Dispose() { _listenSocket = null; if (_socketWrapper != null) { _socketWrapper.Dispose(); _socketWrapper = null; } }
public void ShouldCompleteAcceptTaskOnDispose() { Task task = _wrapper.Accept(socket => { }, exception => { }); _wrapper.Dispose(); Assert.DoesNotThrow(task.Wait); Assert.IsTrue(task.IsCompleted); }
/// <summary> /// Closes the socket, hereby interrupting the blocking receive in <see cref="Bind()"/>. /// </summary> private void CloseSocket() { if (_socket == null) { return; } lock (_socketLock) { if (_socket == null) { return; } // closing a socket actually disposes the socket, so we can safely dereference // the field to avoid entering the lock again later _socket.Dispose(); _socket = null; } }
public override void Dispose() { try { // Try to shutdown the send side of the socket. // This seems to help avoid connection reset issues caused by buffered data // that has not been sent/acked when the graceful shutdown timeout expires. // This may throw if the socket was already closed, so eat any exception. _socket?.Shutdown(SocketShutdown.Send); } catch (Exception) { } _stream.Dispose(); _socket?.Dispose(); }
private void DisconnectSockets() { streamProcessor = null; if (socket != null) { try { socket.Dispose(); } catch (ObjectDisposedException) { } socket = null; } }
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously public async Task ListenAsync() #pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously { try { IPEndPoint localEndPoint; #if TARGET_BROWSER _listenSocket = new ClientWebSocket(); await _listenSocket.ConnectAsync(Configuration.Http.RemoteLoopServer, CancellationToken.None); byte[] buffer = new byte[128 * 1024]; var message = Encoding.ASCII.GetBytes($"{_options.ListenBacklog},{_options.Address}"); await _listenSocket.SendAsync(message, WebSocketMessageType.Binary, true, CancellationToken.None); var first = await _listenSocket.ReceiveAsync(buffer, CancellationToken.None); localEndPoint = IPEndPoint.Parse(Encoding.ASCII.GetString(buffer, 0, first.Count)); #else _listenSocket = new Socket(_options.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp); _listenSocket.Bind(new IPEndPoint(_options.Address, 0)); _listenSocket.Listen(_options.ListenBacklog); localEndPoint = (IPEndPoint)_listenSocket.LocalEndPoint; #endif string host = _options.Address.AddressFamily == AddressFamily.InterNetworkV6 ? $"[{localEndPoint.Address}]" : localEndPoint.Address.ToString(); string scheme = _options.UseSsl ? "https" : "http"; if (_options.WebSocketEndpoint) { scheme = _options.UseSsl ? "wss" : "ws"; } _uri = new Uri($"{scheme}://{host}:{localEndPoint.Port}/"); _socketWrapper = new SocketWrapper(_listenSocket); } catch { _listenSocket?.Dispose(); _socketWrapper?.Dispose(); throw; } }
public override async ValueTask DisposeAsync() { try { // Try to shutdown the send side of the socket. // This seems to help avoid connection reset issues caused by buffered data // that has not been sent/acked when the graceful shutdown timeout expires. // This may throw if the socket was already closed, so eat any exception. _socket?.Shutdown(SocketShutdown.Send); } catch (Exception) { } #if !NETSTANDARD2_0 && !NETFRAMEWORK await _stream.DisposeAsync().ConfigureAwait(false); #else _stream.Dispose(); await Task.CompletedTask.ConfigureAwait(false); #endif _socket?.Dispose(); }