示例#1
0
            /// <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();
            }
示例#2
0
 /// <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;
 }
示例#3
0
        /// <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;
 }
示例#5
0
 /// <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;
 }
示例#6
0
        /// <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);
            }
        }
示例#7
0
        /// <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);
        }
示例#8
0
        /// <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;
        }
示例#9
0
        /// <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); }
        }
示例#10
0
        /// <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);
        }
示例#11
0
 /// <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;
 }
示例#12
0
 /// <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;
 }
示例#13
0
 static public bool TryGetHost(string name, out SocketBase.IHost host)
 {
     return(_dicHosts.TryGetValue(name, out host));
 }
示例#14
0
 /// <summary>
 /// new
 /// </summary>
 /// <param name="host"></param>
 public EndPointManager(SocketBase.IHost host)
 {
     this._host = host;
 }