Repository navigation
Expand file tree
/
Copy pathMainServer.cs
More file actions
177 lines (136 loc) · 5.24 KB
/
Copy pathMainServer.cs
File metadata and controls
177 lines (136 loc) · 5.24 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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;
using SuperSocketLite.SocketBase;
using SuperSocketLite.SocketBase.Protocol;
using SuperSocketLite.SocketEngine;
using CSBaseLib;
//TODO 1. 주기적으로 접속한 세션이 패킷을 주고 받았는지 조사(좀비 클라이언트 검사)하는 기능을 만든다
namespace ChatServer;
public class MainServer : AppServer<ClientSession, EFBinaryRequestInfo>
{
public static ChatServerOption s_ServerOption;
public static SuperSocketLite.SocketBase.Logging.ILog s_MainLogger;
SuperSocketLite.SocketBase.Config.IServerConfig _config;
PacketProcessor _mainPacketProcessor = new ();
RoomManager _roomMgr = new ();
public MainServer()
: base(new DefaultReceiveFilterFactory<ReceiveFilter, EFBinaryRequestInfo>())
{
NewSessionConnected += new SessionHandler<ClientSession>(OnConnected);
SessionClosed += new SessionHandler<ClientSession, CloseReason>(OnClosed);
NewRequestReceived += new RequestHandler<ClientSession, EFBinaryRequestInfo>(OnPacketReceived);
}
public void InitConfig(ChatServerOption option)
{
s_ServerOption = option;
_config = new SuperSocketLite.SocketBase.Config.ServerConfig
{
Name = option.Name,
Ip = "Any",
Port = option.Port,
Mode = SocketMode.Tcp,
MaxConnectionNumber = option.MaxConnectionNumber,
MaxRequestLength = option.MaxRequestLength,
ReceiveBufferSize = option.ReceiveBufferSize,
SendBufferSize = option.SendBufferSize
};
}
public void CreateStartServer()
{
try
{
bool bResult = Setup(new SuperSocketLite.SocketBase.Config.RootConfig(),
_config,
logFactory: new NLogLogFactory());
if (bResult == false)
{
Console.WriteLine("[ERROR] 서버 네트워크 설정 실패 ㅠㅠ");
return;
}
else
{
s_MainLogger = base.Logger;
s_MainLogger.Info("서버 초기화 성공");
}
CreateComponent();
Start();
s_MainLogger.Info("서버 생성 성공");
}
catch (Exception ex)
{
Console.WriteLine($"[ERROR] 서버 생성 실패: {ex.ToString()}");
}
}
public void StopServer()
{
Stop();
_mainPacketProcessor.Destory();
}
// 주요 객체 생성
public ErrorCode CreateComponent()
{
Room.NetSendFunc = this.SendData;
_roomMgr.CreateRooms();
_mainPacketProcessor = new PacketProcessor();
_mainPacketProcessor.CreateAndStart(_roomMgr.GetRoomsList(), this);
s_MainLogger.Info("CreateComponent - Success");
return ErrorCode.None;
}
// 네트워크로 패킷을 보낸다
public bool SendData(string sessionID, byte[] sendData)
{
var session = GetSessionByID(sessionID);
try
{
if (session == null)
{
return false;
}
session.Send(sendData, 0, sendData.Length);
}
catch(Exception ex)
{
// TimeoutException 예외가 발생할 수 있다
MainServer.s_MainLogger.Error($"{ex.ToString()}, {ex.StackTrace}");
session.SendEndWhenSendingTimeOut();
session.Close();
}
return true;
}
// 패킷처리기로 패킷을 전달한다
public void Distribute(ServerPacketData requestPacket)
{
_mainPacketProcessor.InsertPacket(requestPacket);
}
void OnConnected(ClientSession session)
{
//옵션의 최대 연결 수를 넘으면 SuperSocket이 바로 접속을 짤라버린다. 즉 이 OnConneted 함수가 호출되지 않는다
s_MainLogger.Info(string.Format("세션 번호 {0} 접속", session.SessionID));
var packet = ServerPacketData.MakeNTFInConnectOrDisConnectClientPacket(true, session.SessionID);
Distribute(packet);
}
void OnClosed(ClientSession session, CloseReason reason)
{
s_MainLogger.Info($"세션 번호 {session.SessionID} 접속해제: {reason.ToString()}");
var packet = ServerPacketData.MakeNTFInConnectOrDisConnectClientPacket(false, session.SessionID);
Distribute(packet);
}
void OnPacketReceived(ClientSession session, EFBinaryRequestInfo reqInfo)
{
s_MainLogger.Debug($"세션 번호 {session.SessionID} 받은 데이터 크기: {reqInfo.Body.Length}, ThreadId: {System.Threading.Thread.CurrentThread.ManagedThreadId}");
var packet = new ServerPacketData();
packet.SessionID = session.SessionID;
packet.PacketSize = reqInfo.Size;
packet.PacketID = reqInfo.PacketID;
packet.Type = reqInfo.Type;
packet.BodyData = reqInfo.Body;
Distribute(packet);
}
}
public class ClientSession : AppSession<ClientSession, EFBinaryRequestInfo>
{
}