In Getting started with SuperSocket (2), we briefly looked at BootStrap to start the SuperSocket service by configuring the app. config file. Let’s start with an example of a basic configuration file from the previous example:
<? The XML version = "1.0" encoding = "utf-8"? > <configuration> <configSections> <! - log log - > < section name = "log4net" type = "System. The Configuration. IgnoreSectionHandler" / > <! --SocketEngine--> <section name="superSocket" type="SuperSocket.SocketEngine.Configuration.SocketServiceConfig, SuperSocket.SocketEngine"/> </configSections> <! -- Service information description, Name identifier in Window mode --> <appSettings> <add key="ServiceName" value="SupperSocketService"/> <add key="ServiceDescription" Value =" Pre-dusk post-dawn Socket program "/> </appSettings> <! SuperSocket server configuration information serverType is the project of the service like my custom myServer --> <! --name: instance name serverType: AppServer type where the instance is running IP: listening IP port: listening port --> <superSocket> < Servers > <! <server name="SuperSocketDemo" textEncoding=" GB2312" serverType="SuperSocketDemo.Server.MyServer,SuperSocketDemo" ip="Any" port="2020" maxConnectionNumber="100"> </server> </ Servers > </superSocket> <startup> <supportedRuntime version="v4.0" SKu =".netframework, version= v4.5.2"/> </startup> </configuration>Copy the code
App.config
1. Service Name:
<appSettings>
<add key=”ServiceName” value=”SupperSocketService”/>
<add key=”ServiceDescription” value=” pre-dusk post-dawn Socket “/>
</appSettings>
This configuration item will be used as the name of the SuperSocket system service. If ServiceName is set to SupperSocketService, run the installService. bat batch file (note: The file using the SuperSocket. SocketService. Exe -i command, Exe using supersocket. Socketservice. Make sure the server need all assembly and supersocket socketservice. Exe in the same directory), The SuperSocket system service will be installed with the name “SupperSocketService”.
2. SuperSocket root configuration
<socketServer loggingMode=”IndependantFile”>
.
</socketServer>
Details of parameters in the root configuration:
LoggingMode value: ShareFile: multiple server instances share the same log file.
IndependantFile: Multiple server instances have separate log files;
Console: Console log output, valid only in Console applications.
MaxWorkingThreads: Maximum number of worker threads in a thread pool
MinWorkingThreads: Minimum number of worker threads in a thread pool
MaxCompletionPortThreads: Maximum number of completion port threads in the thread pool
MinCompletionPortThreads: The minimum number of threads completing ports in the thread pool
3. Server instance configuration
<servers>
<server name=”SuperSocketDemo” textEncoding=”gb2312″ serverType=”SuperSocketDemo.Server.MyServer,SuperSocketDemo” ip=”Any” port=”2020″ maxConnectionNumber=”100″>
</server>
</servers>
Server instance configuration details:
- Name: indicates the server instance name
- ServiceName: Specifies the name of the service on which the server instance is running. This name is the name of the node that defines the service this server instance runs.
- IP: specifies the SERVER IP address monitored by the socket server. Any: monitors all IPv4 addresses of the local host. IPv6Any, which listens for all IPv6 addresses on the host.
- Port: indicates the listening port of the socket server.
- Mode: Sync: synchronization mode. Async: asynchronous mode; Udp: the Udp protocol
- Disabled: true or false. Whether to disable the server instance. The default value is no.
- ReadTimeOut: indicates the timeout period for reading data from the socket. The default value is 0.
- SendTimeOut: indicates the timeout period for sending data from the socket. The default value is 0.
- MaxConnectionNumber: indicates the maximum number of client connections allowed. The default value is 100.
- ReceiveBufferSize: Size of the buffer used to receive data. The default value is 2048.
- SendBufferSize: buffer size for sending data. The default value is 2048.
- LogCommand: true or false, whether to record commands.
- ClearIdleSession: true or false, whether to clear idle sessions. The default value is false.
- ClearIdleSessionInterval: clean up free session time interval, the default is 120, unit for seconds.
- IdleSessionTimeOut: session timeout. The default value is 300, expressed in seconds.
- Security: Empty, Tls or Ssl3. Transmission layer encryption protocol used by the Socket server. The default value is null.
- MaxCommandLength: Indicates the maximum command length. The default value is 1024.
- DisableSessionSnapshot: indicates whether to disableSessionSnapshot. The default value is false. (1.4 SP1)
- SessionSnapshotInterval: indicates the interval at which session snapshots are generated. The default value is 5 seconds.
- KeepAliveTime: Keepalive message sending interval. The default value is 600 seconds.
- KeepAliveInterval: Indicates the retry interval for keep alive failures. The default value is 60 seconds.
4. Configure multiple server instances
SuperSocket allows you to run multiple server instances in the same program listening on different IP addresses or ports. Simply add two server nodes to the configuration file and set different instance names and IP port combinations.
<servers>
<server name=”MyServerA”
serviceName=”SupperSocketServiceA”
ip=”Any”
port=”2016″>
</server>
<server name=”MyServerB”
serviceName=”SupperSocketServiceB”
ip=”Any”
port=”2017″>
</server>
<services>
<service name=”SupperSocketServiceA” type=”SuperSocketDemo.Server.MyServerA,SuperSocketDemo.SupperSocketServiceA” />
<service name=”SupperSocketServiceB” type=”SuperSocketDemo.Server.MyServerB,SuperSocketDemo.SupperSocketServiceB” />
</services>
Service configuration
Name: the name of the service definition, that is, the name of the server instance node serviceName.
Type: full name of the MyServer type to which the service corresponds.
In some cases, interaction between two server instances is required. For example, client C1 on MyServerA forwards messages to client C2 on MyServerB. After receiving the command from C1, MyServerA needs to forward the messages to client C2 through MyServerB. MyServerA OnStartup: SocketServerManager GetServerByName(string Name) : SocketServerManager GetServerByName(string name)
public class MyServerA : AppServer { private IDespatchServer m_DespatchServer; protected override void OnStartup() { m_DespatchServer = SocketServerManager.GetServerByName("ServerB") as IDespatchServer; base.OnStartup(); } internal void DespatchMessage(string targetSessionKey, string message) { m_DespatchServer.DispatchMessage(targetSessionKey, message); }}Copy the code
MyServerA class
public class MyServerB : AppServer, IDespatchServer { public void DispatchMessage(string sessionKey, string message) { var session = GetAppSessionByIndentityKey(sessionKey); if (session == null) return; session.SendResponse(message); }}}Copy the code
MyServerB class
interface IDespatchServer
{
void DispatchMessage(string sessionKey, string message);
}
Copy the code
IDespatchServer class
This allows you to send a message to the client of MyServerB from MyServerA by calling a method on instance MyServerB. When implementing a Socket server using SuperSocket, it is inevitable to define some parameters in the configuration file. SuperSocket provides a very simple way to define these parameters in a configuration file and then read them in code. Server extension configuration, custom property fields we’ll learn more about later.