/// <summary> /// new /// </summary> /// <param name="info"></param> /// <param name="host"></param> /// <param name="connectedCallback"></param> /// <param name="alreadyCallback"></param> public Node(NodeInfo info, SocketBase.IHost host, Action <Node, SocketBase.IConnection> connectedCallback, Action <Node, SocketBase.IConnection> alreadyCallback) { if (info == null) { throw new ArgumentNullException("info"); } if (host == null) { throw new ArgumentNullException("host"); } if (connectedCallback == null) { throw new ArgumentNullException("connectedCallback"); } if (alreadyCallback == null) { throw new ArgumentNullException("alreadyCallback"); } this.ID = Interlocked.Increment(ref NODE_ID); this.Info = info; this._host = host; this._connectedCallback = connectedCallback; this._alreadyCallback = alreadyCallback; this.Connect(); }
/// <summary> /// new /// </summary> /// <param name="host"></param> /// <exception cref="ArgumentNullException">host is null</exception> public DefaultServerPool(SocketBase.IHost host) { if (host == null) { throw new ArgumentNullException("host"); } this._host = host; }
/// <summary> /// new /// </summary> /// <param name="endPoint"></param> /// <param name="host"></param> /// <exception cref="ArgumentNullException">endPoint is null</exception> /// <exception cref="ArgumentNullException">host is null</exception> public SocketListener(IPEndPoint endPoint, SocketBase.IHost <TMessageInfo, TMessage> host) { this.EndPoint = endPoint ?? throw new ArgumentNullException("endPoint"); this._host = host ?? throw new ArgumentNullException("host"); this._ae = new SocketAsyncEventArgs(); this._ae.Completed += this.AcceptCompleted; }
/// <summary> /// new /// </summary> /// <param name="name"></param> /// <param name="endPoint"></param> /// <param name="host"></param> /// <param name="onConnected"></param> /// <param name="onDisconnected"></param> public SocketConnector(string name, EndPoint endPoint, SocketBase.IHost host, Action<SocketConnector, SocketBase.IConnection> onConnected, Action<SocketConnector, SocketBase.IConnection> onDisconnected) { this.Name = name; this.EndPoint = endPoint; this.Host = host; this._onConnected = onConnected; this._onDisconnected = onDisconnected; }
/// <summary> /// 开始连接服务器节点 /// </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> /// new /// </summary> /// <param name="name"></param> /// <param name="endPoint"></param> /// <param name="host"></param> /// <exception cref="ArgumentNullException">name is null or empty</exception> /// <exception cref="ArgumentNullException">endPoint is null</exception> /// <exception cref="ArgumentNullException">host is null</exception> public SocketListener(string name, IPEndPoint endPoint, SocketBase.IHost host) { if (string.IsNullOrEmpty(name)) throw new ArgumentNullException("name"); if (endPoint == null) throw new ArgumentNullException("endPoint"); if (host == null) throw new ArgumentNullException("host"); this.Name = name; this.EndPoint = endPoint; this._host = host; this._ae = new SocketAsyncEventArgs(); this._ae.Completed += new EventHandler<SocketAsyncEventArgs>(this.AcceptAsyncCompleted); }
/// <summary> /// new /// </summary> /// <param name="endPoint"></param> /// <param name="host"></param> /// <exception cref="ArgumentNullException">endPoint is null</exception> /// <exception cref="ArgumentNullException">host is null</exception> public SocketListener(IPEndPoint endPoint, SocketBase.IHost host) { if (endPoint == null) { throw new ArgumentNullException("endPoint"); } if (host == null) { throw new ArgumentNullException("host"); } this.EndPoint = endPoint; this._host = host; this._ae = new SocketAsyncEventArgs(); this._ae.Completed += this.AcceptCompleted; }
/// <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); } }
/// <summary> /// new /// </summary> /// <param name="name"></param> /// <param name="endPoint"></param> /// <param name="host"></param> /// <exception cref="ArgumentNullException">name is null or empty</exception> /// <exception cref="ArgumentNullException">endPoint is null</exception> /// <exception cref="ArgumentNullException">host is null</exception> public SocketListener(string name, IPEndPoint endPoint, SocketBase.IHost host) { if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException("name"); } if (endPoint == null) { throw new ArgumentNullException("endPoint"); } if (host == null) { throw new ArgumentNullException("host"); } this.Name = name; this.EndPoint = endPoint; this._host = host; this._ae = new SocketAsyncEventArgs(); this._ae.Completed += new EventHandler <SocketAsyncEventArgs>(this.AcceptAsyncCompleted); }
/// <summary> /// new /// </summary> /// <param name="host"></param> /// <exception cref="ArgumentNullException">host is null</exception> public DefaultServerPool(SocketBase.IHost host) { if (host == null) throw new ArgumentNullException("host"); this._host = host; }
/// <summary> /// new /// </summary> /// <param name="name"></param> /// <param name="endPoint"></param> /// <param name="host"></param> public SocketConnector(string name, EndPoint endPoint, SocketBase.IHost host) { this.Name = name; this.EndPoint = endPoint; this.Host = host; }
static public bool TryGetHost(string name, out SocketBase.IHost host) { return(_dicHosts.TryGetValue(name, out host)); }
/// <summary> /// new /// </summary> /// <param name="host"></param> public EndPointManager(SocketBase.IHost host) { this._host = host; }