public void Connect(string sendInterface, int?sendBufferSize, PgmSendWindowSize?windowSize) { lock (syncRoot) { if (socket != null) { socket.Close(); socket = null; } socket = new PgmSocket(); int?nullable = sendBufferSize; socket.SendBufferSize = nullable.HasValue ? nullable.GetValueOrDefault() : 0x100000; if (!string.IsNullOrEmpty(sendInterface)) { IPAddress interfaceIpAddress = IPAddress.Parse(sendInterface); this.socket.SetSendInterface(interfaceIpAddress); } socket.Bind(new IPEndPoint(IPAddress.Any, 50001)); if (!windowSize.HasValue) { windowSize = new PgmSendWindowSize(DefaultRateKbitsPerSec, DefaultWindowSizeInMSecs, DefaultWindowSizeInBytes); } socket.SetSendWindowSize(windowSize.Value); socket.Connect(EndPoint); } }
/// <summary> /// Closes the <see cref="T:System.Net.Sockets.Socket"/>. /// </summary> public void Close() { lock (syncRoot) { if (socket != null) { socket.Close(); socket = null; } } }
/// <summary> /// Starts the listening. /// </summary> /// <exception cref="T:System.InvalidOperationException"> /// Already running. /// </exception> /// <exception cref="T:System.Net.Sockets.SocketException"> /// Exception from the underlying <see cref="T:System.Net.Sockets.Socket"/>. /// </exception> public void Start() { try { lock (_lock) { if (running) { throw new InvalidOperationException("Listener is allready started."); } socket = new PgmSocket() { ReceiveBufferSize = receiveBufferSize }; socket.SetReuseAddress(ReuseAddress); socket.Bind(LocalEndPoint); socket.AddReceiveInterfaces(receiveInterfaces); if (UseHighSpeedIntranet) { socket.SetHighSpeedIntranetOption(UseHighSpeedIntranet); } socket.Listen(5); socketAsyncEventArgs = new SocketAsyncEventArgs(); socketAsyncEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(OnAcceptAsyncCompleted); running = true; } BeginAccept(); if (log.IsDebugEnabled) { log.DebugFormat("Listening on {0}", LocalEndPoint); } } catch (ThreadAbortException) { throw; } catch (OutOfMemoryException) { throw; } catch (SocketException exception) { if (log.IsErrorEnabled) { log.Error(SocketHelper.FormatSocketException(exception), exception); } throw; } }
/// <summary> /// Stops the listening and closes the socket. /// </summary> public void Stop() { if (log.IsDebugEnabled) { log.DebugFormat("Stopp listening on {0}", LocalEndPoint); } lock (_lock) { running = false; if (socket != null) { socket.Close(); socket = null; } } }
/// <summary> /// Disposes the listener. /// </summary> /// <param name="flag1"> The disposing.</param> private void Dispose(bool disposing) { if (disposing) { lock (_lock) { running = false; if (log.IsDebugEnabled) { log.DebugFormat("Disposing PgmListener for endpoint {0}.", LocalEndPoint); } if (socket != null) { socket.Close(); socket = null; } if (socketAsyncEventArgs != null) { socketAsyncEventArgs.Dispose(); socketAsyncEventArgs = null; } } } }