/// <summary> /// begin connect /// </summary> /// <param name="endPoint"></param> /// <param name="host"></param> /// <param name="callback"></param> /// <exception cref="ArgumentNullException">endPoint is null</exception> /// <exception cref="ArgumentNullException">host is null</exception> /// <exception cref="ArgumentNullException">callback is null</exception> static public void BeginConnect(EndPoint endPoint, SocketBase.IHost host, Action<SocketBase.IConnection> callback) { if (endPoint == null) throw new ArgumentNullException("endPoint"); if (host == null) throw new ArgumentNullException("host"); if (callback == null) throw new ArgumentNullException("callback"); SocketBase.Log.Trace.Debug(string.Concat("begin connect to ", endPoint.ToString())); var socket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); try { socket.BeginConnect(endPoint, ar => { try { socket.EndConnect(ar); socket.NoDelay = true; socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true); socket.ReceiveBufferSize = host.SocketBufferSize; socket.SendBufferSize = host.SocketBufferSize; } catch (Exception ex) { try { socket.Close(); socket.Dispose(); } catch { } SocketBase.Log.Trace.Error(ex.Message, ex); callback(null); return; } callback(new SocketBase.DefaultConnection(host.NextConnectionID(), socket, host)); }, null); } catch (Exception ex) { SocketBase.Log.Trace.Error(ex.Message, ex); callback(null); } }
/// <summary> /// begin connect /// </summary> /// <param name="endPoint"></param> /// <param name="host"></param> /// <param name="callback"></param> /// <exception cref="ArgumentNullException">endPoint is null</exception> /// <exception cref="ArgumentNullException">host is null</exception> /// <exception cref="ArgumentNullException">callback is null</exception> static public void BeginConnect(EndPoint endPoint, SocketBase.IHost host, Action<SocketBase.IConnection> callback) { if (endPoint == null) throw new ArgumentNullException("endPoint"); if (host == null) throw new ArgumentNullException("host"); if (callback == null) throw new ArgumentNullException("callback"); var socket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); try { socket.BeginConnect(endPoint, ar => { bool success = false; try { socket.EndConnect(ar); success = true; } catch { } if (success) { socket.NoDelay = true; socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true); socket.ReceiveBufferSize = host.SocketBufferSize; socket.SendBufferSize = host.SocketBufferSize; callback(new SocketBase.DefaultConnection(host.NextConnectionID(), socket, host)); } else callback(null); }, null); } catch { callback(null); } }