zrg
2024-08-16 18865d8bf24382e850e661dec6cca0e8b9dba6ae
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
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Top.Api;
 
namespace Taobao.Top.Link.Channel.TCP
{
    /// <summary>server channel bind on raw tcp, just impl an easy server
    /// </summary>
    public class TcpServerChannel : ServerChannel
    {
        private bool _running;
        private TcpListener _tcpListener;
        private Thread _acceptWorker;
        private IOWorker _ioWorker;
        private int _ioWorkerCount;
        private LinkedList<TcpClient> _tcpClients;
 
        public delegate void IOWorker(ITopLogger log, TcpServerChannel server, TcpClient tcpClient);
 
        /// <summary>init tcp server channel
        /// </summary>
        /// <param name="factory"></param>
        /// <param name="port"></param>
        /// <param name="ioWorker">deal with networkstream</param>
        public TcpServerChannel(ITopLogger log
            , int port
            , IOWorker ioWorker)
            : base(log
            , port)
        {
            this._ioWorker = ioWorker;
            this._tcpClients = new LinkedList<TcpClient>();
        }
 
        public override void Start()
        {
            this._running = true;
            this._tcpListener = new TcpListener(IPAddress.Any, this.Port);
            this._tcpListener.Start();
            this._acceptWorker = new Thread(() =>
            {
                while (this._running)
                {
                    try
                    {
                        var client = this._tcpListener.AcceptTcpClient();
                        this._tcpClients.AddLast(client);
                        ThreadPool.QueueUserWorkItem((state) =>
                        {
                            try { this.AcceptSocket(client); }
                            catch (Exception e)
                            {
                                this.InternalOnError(e);
                                this._tcpClients.Remove(client);
                            }
                        });
                    }
                    catch (SocketException) { break; }
                    catch (Exception e)
                    {
                        this.InternalOnError(e);
                        break;
                    }
                }
            });
            this._acceptWorker.IsBackground = true;
            this._acceptWorker.Start();
        }
        public override void Stop()
        {
            this._running = false;
            this._acceptWorker.Abort();
            this._tcpListener.Stop();
        }
 
        private void AcceptSocket(TcpClient client)
        {
            if (this._ioWorker == null)
                return;
            var logName = "IO-Worker#" + (++this._ioWorkerCount);
            ThreadPool.QueueUserWorkItem(o => 
                this._ioWorker(Log.Instance, this, client));
        }
        private void InternalOnError(Exception e)
        {
            if (this.OnError != null)
                this.OnError(this, new ChannelContext(e));
        }
    }
}