C# Rookiss Part4 ๊ฒ์์๋ฒ : Connector
๐โโ๏ธConnector
๋ฆฌ์ค๋์ ๋ฐ๋๋๋ ๊ฐ๋
๐ช์ฝ๋
public class Connector
{
Func<Session> _sessionFactory;
public void Connect(IPEndPoint endPoint, Func<Session> sessionFactory)
{
// ํด๋ํฐ ์ค์
Socket socket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
_sessionFactory = sessionFactory;
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
args.Completed += OnConnectCompleted;
args.RemoteEndPoint = endPoint;
args.UserToken = socket;
RegisterConnect(args);
}
void RegisterConnect(SocketAsyncEventArgs args)
{
Socket socket = args.UserToken as Socket;
if (socket == null)
return;
bool pending = socket.ConnectAsync(args);
if (pending == false)
OnConnectCompleted(null, args);
}
void OnConnectCompleted(object sender, SocketAsyncEventArgs args)
{
if (args.SocketError == SocketError.Success)
{
Session session = _sessionFactory.Invoke();
session.Start(args.ConnectSocket);
session.OnConnected(args.RemoteEndPoint);
}
else
{
Console.WriteLine($"OnConnectCompleted Fail : {args.SocketError}");
}
}
}
๐ช๋ถ์
์ปค๋ฅํฐ๋ฅผ ๋ง๋๋ ์ด์ : ๋ฆฌ์๋ธ๋ ์๋๋ฅผ ๊ณต์ฉ์ผ๋ก ํฉ์น๊ธฐ ์ํด, ๋ถํ ์๋ฒ๋ฅผ ํ ๋ ํต์ ์ ํ๊ธฐ ์ํด (ํ์ชฝ์ ๋ฆฌ์คํฐ ์ํ, ํ์ชฝ์ ์ปค๋ฅํธ ์ํ )
์ฐ๊ฒฐํ๋ ๋ถ๋ถ์ ์๋๋ฐฉ์ ์ฃผ์๋ฅผ ๋ฃ์ด์ค์ผ ๋จ - args.RemoteEndPoint = endPoint;
Socket์ ๋งด๋ฒ๋ณ์๋ก ๋์ง ์๋ ์ด์ : ์ปค๋ฅํฐ๋ฅผ ํ๋ช ๋ง ๋ฐ๊ณ ๋๋ด๋๊ฒ ์๋๋ผ ์ฌ๋ฌ๋ช ์ ๋ฐ์ ์ ์๊ธฐ ๋๋ฌธ์ ์ด๋ฒคํธ๋ฅผ ํตํด์ ์ธ์๋ฅผ ๋๊ฒจ์ค
Session.Start(args.ConnectSocket) - Session.Start๋ฅผ ํ๊ธฐ ์ํด ์์ผ์ด ํ์ํ๋ฐ args.ConnectSocket๋ก ๋ฃ์ด์ค, Start๋ฅผ ํ์ผ๋ฉด Recv์ด๋ฒคํธ๋ ๋ฑ๋ก๋ ์ํ
์๋ฒ์ฝ์ด์ ์๋ ์ปค๋ฅํฐ ๋ฑ๋ฑ์ ํจ์๋ฅผ ํด๋ผ๋ ์๋ฒ์์ ์ฌ์ฉํ๊ธฐ ์ํด ์๋ฒ์ฝ์ด๋ฅผ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก ๋ง๋ค๊ธฐ
๐ช๋ผ์ด๋ธ๋ฌ๋ฆฌ
ServerCore ์์ฑ -> ์ถ๋ ฅํ์ -> ํด๋์ค๋ผ์ด๋ธ๋ฌ๋ฆฌ Server, DummyClient ์ถ๊ฐ -> ์ฐธ์กฐ -> ServerCore์ ํ