Repository navigation
Expand file tree
/
Copy pathMainServer.cs
More file actions
120 lines (95 loc) · 3.43 KB
/
Copy pathMainServer.cs
File metadata and controls
120 lines (95 loc) · 3.43 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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;
using System.Diagnostics;
using SuperSocketLite.SocketBase.Logging;
using SuperSocketLite.SocketBase;
using SuperSocketLite.SocketBase.Protocol;
using SuperSocketLite.SocketBase.Config;
namespace BinaryPacketServer;
class MainServer : AppServer<NetworkSession, EFBinaryRequestInfo>
{
Dictionary<int, Action<NetworkSession, EFBinaryRequestInfo>> _handlerMap = new ();
CommonHandler _commonHandler = new ();
IServerConfig _config;
public MainServer()
: base(new DefaultReceiveFilterFactory<ReceiveFilter, EFBinaryRequestInfo>())
{
NewSessionConnected += new SessionHandler<NetworkSession>(OnConnected);
SessionClosed += new SessionHandler<NetworkSession, CloseReason>(OnClosed);
NewRequestReceived += new RequestHandler<NetworkSession, EFBinaryRequestInfo>(RequestReceived);
}
void RegistHandler()
{
_handlerMap.Add((int)PACKETID.ReqEcho, _commonHandler.RequestEcho);
DevLog.Write(string.Format("핸들러 등록 완료"), LOG_LEVEL.INFO);
}
public void InitConfig()
{
_config = new ServerConfig
{
Port = 11021,
Ip = "Any",
MaxConnectionNumber = 100,
Mode = SocketMode.Tcp,
Name = "BoardServerNet"
};
}
public void CreateServer()
{
try
{
bool isResult = Setup(new RootConfig(),
_config,
logFactory: new ConsoleLogFactory());
if (isResult == false)
{
DevLog.Write(string.Format("[ERROR] 서버 네트워크 설정 실패 ㅠㅠ"), LOG_LEVEL.ERROR);
return;
}
RegistHandler();
DevLog.Write(string.Format("서버 생성 성공"), LOG_LEVEL.INFO);
}
catch(Exception ex)
{
DevLog.Write(string.Format($"서버 생성 실패: {ex.ToString()}"), LOG_LEVEL.ERROR);
}
}
public bool IsRunning(ServerState curState)
{
if (curState == ServerState.Running)
{
return true;
}
return false;
}
void OnConnected(NetworkSession session)
{
DevLog.Write(string.Format("세션 번호 {0} 접속", session.SessionID), LOG_LEVEL.INFO);
}
void OnClosed(NetworkSession session, CloseReason reason)
{
DevLog.Write(string.Format("세션 번호 {0} 접속해제: {1}", session.SessionID, reason.ToString()), LOG_LEVEL.INFO);
}
void RequestReceived(NetworkSession session, EFBinaryRequestInfo reqInfo)
{
DevLog.Write(string.Format("세션 번호 {0} 받은 데이터 크기: {1}, ThreadId: {2}", session.SessionID, reqInfo.Body.Length, System.Threading.Thread.CurrentThread.ManagedThreadId), LOG_LEVEL.INFO);
var PacketID = reqInfo.PacketID;
var value1 = reqInfo.Value1;
var value2 = reqInfo.Value2;
if (_handlerMap.ContainsKey(PacketID))
{
_handlerMap[PacketID](session, reqInfo);
}
else
{
DevLog.Write(string.Format("세션 번호 {0} 받은 데이터 크기: {1}", session.SessionID, reqInfo.Body.Length), LOG_LEVEL.INFO);
}
}
}
public class NetworkSession : AppSession<NetworkSession, EFBinaryRequestInfo>
{
}