/// <summary> /// Accept completed event callback. /// </summary> /// <param name="e">The event args.</param> /// <returns>True if socket is still open.</returns> private bool OnAcceptCompleted(SocketAsyncEventArgs e) { try { SocketError error = e.SocketError; if (error != SocketError.Success) { if (error == SocketError.OperationAborted) { return(false); } if (error != SocketError.ConnectionReset) { throw new SocketException((int)e.SocketError); } } else { OnAcceptSocket(e); return(true); } if (log.IsDebugEnabled) { log.DebugFormat("Connection was reset by remote peer {0} at end point {1}", e.AcceptSocket, LocalEndPoint); } return(true); } catch (SocketException exception) { if (log.IsErrorEnabled) { log.Error("Accept failed, " + SocketHelper.FormatSocketException(exception), exception); } } catch (ThreadAbortException) { throw; } catch (OutOfMemoryException) { throw; } catch (Exception exception2) { if (log.IsErrorEnabled) { log.Error("Accept failed, EndPoint=" + localEndPoint, exception2); } } return(false); }
/// <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> /// Sends a byte array asynchronously. /// </summary> /// <param name="data">The payload.</param> public void Send(byte[] data) { PgmSender item = queue.Dequeue(); try { item.Send(data); } catch (SocketException exception) { switch (exception.SocketErrorCode) { case SocketError.ConnectionReset: case SocketError.NotConnected: case SocketError.Disconnecting: try { item.Connect(this.bindInterface, null, null); } catch (SocketException exception2) { log.Error("Reconnect failed, " + SocketHelper.FormatSocketException(exception2)); } catch (ThreadAbortException) { throw; } catch (OutOfMemoryException) { throw; } catch (Exception exception3) { log.Error("Reconnect failed", exception3); } break; } throw; } finally { queue.Enqueue(item); } }