schangxiang@126.com
2025-11-04 ca398287db3f5ea01f03aac4a85edb13b28100a6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
 
namespace Sodao.FastSocket.Server
{
    /// <summary>
    /// socket listener
    /// </summary>
    public sealed class SocketListener : ISocketListener
    {
        #region Private Members
        private readonly SocketBase.IHost _host = null;
        private const int BACKLOG = 500;
        private Socket _socket = null;
        private readonly SocketAsyncEventArgs _ae = null;
        #endregion
 
        #region Constructors
        /// <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;
        }
        #endregion
 
        #region ISocketListener Members
        /// <summary>
        /// socket accepted event
        /// </summary>
        public event Action<ISocketListener, SocketBase.IConnection> Accepted;
        /// <summary>
        /// get listener endPoint
        /// </summary>
        public EndPoint EndPoint { get; private set; }
        /// <summary>
        /// start
        /// </summary>
        public void Start()
        {
            if (this._socket == null)
            {
                this._socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                this._socket.Bind(this.EndPoint);
                this._socket.Listen(BACKLOG);
 
                this.AcceptAsync(this._socket);
            }
        }
        /// <summary>
        /// stop
        /// </summary>
        public void Stop()
        {
            if (this._socket != null)
            {
                this._socket.Close();
                this._socket = null;
            }
        }
        #endregion
 
        #region Private Methods
        /// <summary>
        /// accept socket.
        /// </summary>
        /// <param name="socket"></param>
        private void AcceptAsync(Socket socket)
        {
            if (socket == null) return;
 
            bool completed = true;
            try { completed = this._socket.AcceptAsync(this._ae); }
            catch (Exception ex) { SocketBase.Log.Trace.Error(ex.Message, ex); }
 
            if (!completed) ThreadPool.QueueUserWorkItem(_ => this.AcceptCompleted(this, this._ae));
        }
        /// <summary>
        /// async accept socket completed handle.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AcceptCompleted(object sender, SocketAsyncEventArgs e)
        {
            Socket accepted = null;
            if (e.SocketError == SocketError.Success) accepted = e.AcceptSocket;
            e.AcceptSocket = null;
 
            if (accepted != null)
                this.Accepted(this, this._host.NewConnection(accepted));
 
            //continue to accept!
            this.AcceptAsync(this._socket);
        }
        #endregion
    }
}