서버/네트워크 라이브러리

3. Service

광란의슈가슈가룬 2024. 11. 29. 19:45

지난번에 iocpCore를 만들어서 실행해보았고 이를 좀 더 편하게 관리하기 위해 Service라는 것을 도입했다.

이에 맞춰 기존의 라이브러리를 조금 수정했다.

 

Service.h

#pragma once
#include "NetAddress.h"
#include "IocpCore.h"
#include "Listener.h"
#include <functional>

enum class ServiceType : uint8
{
	Server,
	Client
};


//-------------
//  Service
//-------------

using SessionFactory = function<SessionRef(void)>;

class Service : public enable_shared_from_this<Service>
{
public:
	Service(ServiceType type, NetAddress address, IocpCoreRef core, SessionFactory factory, int32 maxSessionCount = 1);
	virtual ~Service();

	virtual bool		Start() abstract;
	bool			CanStart() { return _sessionFactory != nullptr; }

	virtual void		CloseService();
	void			SetSessionFactory(SessionFactory func) { _sessionFactory = func; }

	SessionRef		CreateSession();
	void			AddSession(SessionRef session);
	void			RealeseSession(SessionRef session);
	int32			GetCurrentSessionCount() { return _sessionCount; }
	int32			GetMaxSessionCount() { return _maxSessionCount; }

public:
	ServiceType		GetServiceType() { return _type; }
	NetAddress		GetNetAddress() { return _netAddress; }
	IocpCoreRef&		GetIocpCore() { return _iocpCore; }

protected:
	USE_LOCK;

	ServiceType		_type;
	NetAddress		_netAddress = {};
	IocpCoreRef		_iocpCore;
	
	Set<SessionRef>		_sessions;
	int32			_sessionCount = 0;
	int32			_maxSessionCount = 0;
	SessionFactory		_sessionFactory;
};

//-----------------
//  ClientService
//-----------------

class ClientService : public Service
{
public:
	ClientService(NetAddress targetAddress, IocpCoreRef core, SessionFactory factory, int32 maxSessionCount = 1);
	virtual ~ClientService();

	virtual bool Start() override;

};

//-----------------
//  ServerService
//-----------------

class ServerService : public Service
{
public:
	ServerService(NetAddress targetAddress, IocpCoreRef core, SessionFactory factory, int32 maxSessionCount = 1);
	virtual ~ServerService();

	virtual bool Start() override;
	virtual void CloseService() override;

private:
	ListenerRef _listener = nullptr;
};

Service.cpp

#include "pch.h"
#include "Service.h"
#include "Session.h"
#include "Listener.h"

//-------------
//  Service
//-------------

Service::Service(ServiceType type, NetAddress address, IocpCoreRef core, SessionFactory factory, int32 maxSessionCount)
	: _type(type), _netAddress(address), _iocpCore(core), _sessionFactory(factory), _maxSessionCount(maxSessionCount)
{
}

Service::~Service()
{
}

void Service::CloseService()
{
	// TODO
}

SessionRef Service::CreateSession()
{
	SessionRef session = _sessionFactory();

	if (_iocpCore->Register(session) == false)
		return nullptr;

	return session;
}

void Service::AddSession(SessionRef session)
{
	WRITE_LOCK;
	_sessionCount++;
	_sessions.insert(session);
}

void Service::RealeseSession(SessionRef session)
{
	WRITE_LOCK;
	ASSERT_CRASH(_sessions.erase(session) != 0);
	_sessionCount--;
}

//-----------------
//   ClientService
//-----------------

ClientService::ClientService(NetAddress targetAddress, IocpCoreRef core, SessionFactory factory, int32 maxSessionCount)
	:Service(ServiceType::Client, targetAddress, core, factory, maxSessionCount)
{
}

ClientService::~ClientService()
{
}

bool ClientService::Start()
{
	// TODO
	return true;
}

//-----------------
//  ServerService
//-----------------

ServerService::ServerService(NetAddress address, IocpCoreRef core, SessionFactory factory, int32 maxSessionCount)
	:Service(ServiceType::Server, address, core, factory, maxSessionCount)
{
}

ServerService::~ServerService()
{
}

bool ServerService::Start()
{
	if (CanStart() == false)
		return false;

	_listener = MakeShared<Listener>();
	if (_listener == nullptr)
		return false;

	ServerServiceRef service = static_pointer_cast<ServerService>(shared_from_this());
	if (_listener->StartAccept(service) == false)
		return false;

	return true;
}

void ServerService::CloseService()
{
	//TODO

	Service::CloseService();
}

'서버 > 네트워크 라이브러리' 카테고리의 다른 글

5. Session#2  (0) 2025.01.04
4. Session#1  (0) 2025.01.01
2. IocpCore  (0) 2024.10.05
1. Socket Utils  (0) 2024.10.01