public static GetConnection ( ConnectionInfo connectionInfo, SendReceiveOptions defaultSendReceiveOptions, |
||
connectionInfo | ConnectionInfo | ConnectionInfo to be used to create connection |
defaultSendReceiveOptions | SendReceiveOptions | The SendReceiveOptions which will be set as this connections defaults |
sslOptions | SSLOptions to use with this connection | |
establishIfRequired | bool | If true will establish the TCP connection with the remote end point before returning |
Résultat |
private void newListenerInstance_ConnectionReceived(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args) { try { IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse(args.Socket.Information.LocalAddress.DisplayName.ToString()), int.Parse(args.Socket.Information.LocalPort)); IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse(args.Socket.Information.RemoteAddress.DisplayName.ToString()), int.Parse(args.Socket.Information.RemotePort)); ConnectionInfo newConnectionInfo = new ConnectionInfo(ConnectionType.TCP, remoteEndPoint, localEndPoint, ApplicationLayerProtocol, this); TCPConnection.GetConnection(newConnectionInfo, NetworkComms.DefaultSendReceiveOptions, args.Socket, true); } catch (ConfirmationTimeoutException) { //If this exception gets thrown its generally just a client closing a connection almost immediately after creation } catch (CommunicationException) { //If this exception gets thrown its generally just a client closing a connection almost immediately after creation } catch (ConnectionSetupException) { //If we are the server end and we did not pick the incoming connection up then tooo bad! } catch (SocketException) { //If this exception gets thrown its generally just a client closing a connection almost immediately after creation } catch (Exception ex) { //For some odd reason SocketExceptions don't always get caught above, so another check if (ex.GetBaseException().GetType() != typeof(SocketException)) { //Can we catch the socketException by looking at the string error text? if (ex.ToString().StartsWith("System.Net.Sockets.SocketException")) { LogTools.LogException(ex, "ConnectionSetupError_SE"); } else { LogTools.LogException(ex, "ConnectionSetupError"); } } } }
/// <summary> /// Async method for handling up new incoming TCP connections /// </summary> private void TCPConnectionReceivedAsync(IAsyncResult ar) { if (!IsListening) { return; } try { TcpClient newTCPClient = listenerInstance.EndAcceptTcpClient(ar); ConnectionInfo newConnectionInfo = new ConnectionInfo(ConnectionType.TCP, (IPEndPoint)newTCPClient.Client.RemoteEndPoint, (IPEndPoint)newTCPClient.Client.LocalEndPoint, ApplicationLayerProtocol, this); if (NetworkComms.LoggingEnabled) { NetworkComms.Logger.Info("New incoming TCP connection from " + newConnectionInfo); } //We have to use our own thread pool here as the performance of the .Net one is awful NetworkComms.IncomingConnectionEstablishThreadPool.EnqueueItem(QueueItemPriority.Normal, new WaitCallback((obj) => { #region Pickup The New Connection try { TCPConnection.GetConnection(newConnectionInfo, ListenerDefaultSendReceiveOptions, newTCPClient, true, SSLOptions); } catch (ConfirmationTimeoutException) { //If this exception gets thrown its generally just a client closing a connection almost immediately after creation } catch (CommunicationException) { //If this exception gets thrown its generally just a client closing a connection almost immediately after creation } catch (ConnectionSetupException) { //If we are the server end and we did not pick the incoming connection up then tooo bad! } catch (SocketException) { //If this exception gets thrown its generally just a client closing a connection almost immediately after creation } catch (Exception ex) { //For some odd reason SocketExceptions don't always get caught above, so another check if (ex.GetBaseException().GetType() != typeof(SocketException)) { //Can we catch the socketException by looking at the string error text? if (ex.ToString().StartsWith("System.Net.Sockets.SocketException")) { LogTools.LogException(ex, "ConnectionSetupError_SE"); } else { LogTools.LogException(ex, "ConnectionSetupError"); } } } #endregion Pickup The New Connection }), null); } catch (SocketException) { //If this exception gets thrown its generally just a client closing a connection almost immediately after creation } catch (Exception ex) { //For some odd reason SocketExceptions don't always get caught above, so another check if (ex.GetBaseException().GetType() != typeof(SocketException)) { //Can we catch the socketException by looking at the string error text? if (ex.ToString().StartsWith("System.Net.Sockets.SocketException")) { LogTools.LogException(ex, "ConnectionSetupError_SE"); } else { LogTools.LogException(ex, "ConnectionSetupError"); } } } finally { listenerInstance.BeginAcceptTcpClient(TCPConnectionReceivedAsync, null); } }