forked from kerryjiang/SuperSocket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTcpSocketServerBase.cs
More file actions
71 lines (60 loc) · 2.7 KB
/
Copy pathTcpSocketServerBase.cs
File metadata and controls
71 lines (60 loc) · 2.7 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using SuperSocket.Common;
using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Command;
using AnyLog;
using SuperSocket.SocketBase.Protocol;
namespace SuperSocket.SocketEngine
{
abstract class TcpSocketServerBase : SocketServerBase
{
private readonly byte[] m_KeepAliveOptionValues;
private readonly byte[] m_KeepAliveOptionOutValues;
private readonly int m_SendTimeOut;
private readonly int m_ReceiveBufferSize;
private readonly int m_SendBufferSize;
public TcpSocketServerBase(IAppServer appServer, ListenerInfo[] listeners)
: base(appServer, listeners)
{
var config = appServer.Config;
uint dummy = 0;
m_KeepAliveOptionValues = new byte[Marshal.SizeOf(dummy) * 3];
m_KeepAliveOptionOutValues = new byte[m_KeepAliveOptionValues.Length];
//whether enable KeepAlive
BitConverter.GetBytes((uint)1).CopyTo(m_KeepAliveOptionValues, 0);
//how long will start first keep alive
BitConverter.GetBytes((uint)(config.KeepAliveTime * 1000)).CopyTo(m_KeepAliveOptionValues, Marshal.SizeOf(dummy));
//keep alive interval
BitConverter.GetBytes((uint)(config.KeepAliveInterval * 1000)).CopyTo(m_KeepAliveOptionValues, Marshal.SizeOf(dummy) * 2);
m_SendTimeOut = config.SendTimeOut;
m_ReceiveBufferSize = config.ReceiveBufferSize;
m_SendBufferSize = config.SendBufferSize;
}
protected IAppSession CreateSession(Socket client, ISocketSession session)
{
if (m_SendTimeOut > 0)
client.SendTimeout = m_SendTimeOut;
if (m_ReceiveBufferSize > 0)
client.ReceiveBufferSize = m_ReceiveBufferSize;
if (m_SendBufferSize > 0)
client.SendBufferSize = m_SendBufferSize;
if (!Platform.SupportSocketIOControlByCodeEnum)
client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, m_KeepAliveOptionValues);
else
client.IOControl(IOControlCode.KeepAliveValues, m_KeepAliveOptionValues, m_KeepAliveOptionOutValues);
client.NoDelay = true;
client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true);
return this.AppServer.CreateAppSession(session);
}
protected override ISocketListener CreateListener(ListenerInfo listenerInfo)
{
return new TcpAsyncSocketListener(listenerInfo);
}
}
}