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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
 
namespace Sodao.FastSocket.Server
{
    /// <summary>
    /// Socket server manager.
    /// </summary>
    public class SocketServerManager
    {
        #region Private Members
        /// <summary>
        /// key:server name.
        /// </summary>
        static private readonly Dictionary<string, SocketBase.IHost> _dicHosts = new Dictionary<string, SocketBase.IHost>();
        #endregion
 
        #region Static Methods
        /// <summary>
        /// 初始化Socket Server
        /// </summary>
        static public void Init()
        {
            Init("socketServer");
        }
        
        /// <summary>
        /// 初始化Socket Server
        /// </summary>
        /// <param name="sectionName"></param>
        static public void Init(string sectionName)
        {
            if (string.IsNullOrEmpty(sectionName)) throw new ArgumentNullException("sectionName");
            Init(ConfigurationManager.GetSection(sectionName) as Config.SocketServerConfig);
        }
 
        /// <summary>
        /// 初始化Socket Server
        /// </summary>
        /// <param name="config"></param>
        static public void Init(Config.SocketServerConfig config)
        {
            if (config == null) throw new ArgumentNullException("config");
            if (config.Servers == null) return;
            
            foreach (Config.Server serverConfig in config.Servers)
            {
 
                //init protocol
                var objProtocol = GetProtocol(serverConfig.Protocol);
                if (objProtocol == null) throw new InvalidOperationException("protocol");
                
                //init custom service
                var tService = Type.GetType(serverConfig.ServiceType, false);
                if (tService == null) throw new InvalidOperationException("serviceType");
                
                var objService = Activator.CreateInstance(tService);
                if (objService == null) throw new InvalidOperationException("serviceType");
 
                //init host.
                _dicHosts.Add(serverConfig.Name, Activator.CreateInstance(
                    typeof(SocketServer<>).MakeGenericType(
                    objProtocol.GetType().GetInterface(typeof(Protocol.IProtocol<>).Name).GetGenericArguments()),
                        serverConfig.Port,
                        objService,
                        objProtocol,
                        serverConfig.SocketBufferSize,
                        serverConfig.MessageBufferSize,
                        serverConfig.MaxMessageSize,
                        serverConfig.MaxConnections) as SocketBase.IHost);
                        
            }
            
        }
 
        /// <summary>
        /// 初始化Socket Server
        /// </summary>
        /// <param name="nSocketPort">端口号</param>
        static public void Init(int nSocketPort)
        {
            Init("socketServer", nSocketPort);
        }
        /// <summary>
        /// 初始化Socket Server
        /// </summary>
        /// <param name="sectionName"></param>
        static public void Init(string sectionName, int nSocketPort)
        {
            if (string.IsNullOrEmpty(sectionName)) throw new ArgumentNullException("sectionName");
            Init(ConfigurationManager.GetSection(sectionName) as Config.SocketServerConfig, nSocketPort);
        }
 
#region 定制方便修改端口号
 
        /// <summary>
        /// 初始化Socket Server
        /// </summary>
        /// <param name="config"></param>
        static public void Init(Config.SocketServerConfig config, int nSocketPort)
        {
            if (config == null) throw new ArgumentNullException("config");
            if (config.Servers == null) return;
            
            foreach (Config.Server serverConfig in config.Servers)
            {
                //init protocol
                var objProtocol = GetProtocol(serverConfig.Protocol);
                
                if (objProtocol == null) throw new InvalidOperationException("protocol");
 
                //init custom service
                var tService = Type.GetType(serverConfig.ServiceType, false);
                if (tService == null) throw new InvalidOperationException("serviceType");
 
                var objService = Activator.CreateInstance(tService);
                if (objService == null) throw new InvalidOperationException("serviceType");
                if (nSocketPort == 0) nSocketPort = serverConfig.Port;
 
                //init host.
                _dicHosts.Add(serverConfig.Name, Activator.CreateInstance(
                    typeof(SocketServer<>).MakeGenericType(
                    objProtocol.GetType().GetInterface(typeof(Protocol.IProtocol<>).Name).GetGenericArguments()),
                        nSocketPort,
                        objService,
                        objProtocol,
                        serverConfig.SocketBufferSize,
                        serverConfig.MessageBufferSize,
                        serverConfig.MaxMessageSize,
                        serverConfig.MaxConnections) as SocketBase.IHost);
                        
            }
        }
#endregion
 
        /// <summary>
        /// get protocol.
        /// </summary>
        /// <param name="protocol"></param>
        /// <returns></returns>
        static public object GetProtocol(string protocol)
        {
            switch (protocol)
            {
                case Protocol.ProtocolNames.Thrift: return new Protocol.ThriftProtocol();
                case Protocol.ProtocolNames.CommandLine: return new Protocol.CommandLineProtocol();
                case Protocol.ProtocolNames.HxProtocol: return new Protocol.HxProtocol();
            }
            return Activator.CreateInstance(Type.GetType(protocol, false));
        }
 
        /// <summary>
        /// 启动服务
        /// </summary>
        static public void Start()
        {
            _dicHosts.ToList().ForEach(c => c.Value.Start());
        }
        /// <summary>
        /// 停止服务
        /// </summary>
        static public void Stop()
        {
            _dicHosts.ToList().ForEach(c => c.Value.Stop());
        }
        /// <summary>
        /// try get host by name.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="host"></param>
        /// <returns></returns>
        static public bool TryGetHost(string name, out SocketBase.IHost host)
        {
            return _dicHosts.TryGetValue(name, out host);
        }
        #endregion
    }
}