ProudNet Docs
WebsiteProud ConsoleLanguage
  • 🌐ProudNet 프라우드넷
    • ProudNet 소개
    • 다운로드 및 설치
      • ProudNet 라이센스 인증 방법
      • AMI
    • 프로젝트 설정
      • C++
      • C#
      • Mac Xcode
      • Linux
      • Unity3D
        • iOS 빌드
      • Unreal Engine 4
      • PIDL 컴파일러 실행
    • ProudNet 사용하기
      • 서버와 클라이언트
        • 서버 활용법
        • 클라이언트 활용법
      • RMI
        • RMI 활용법
      • PIDL
        • PIDL 활용법
      • 이벤트 핸들링
      • 통신 메시지
      • P2P 통신
        • P2P 통신 활용법
    • ProudNet 활용하기
      • 활용 방법
      • 성능을 위한 팁
    • ProudNet에서 DB 사용하기
      • DB Cache System ver.2
        • DB Cache 이론 및 이해
        • DB Cache 설치 및 네트워크 설정
        • DB Cache 서버와 클라이언트
        • DB Cache 사용 및 활용
          • DB Cache 활용법
      • ADO API
      • ODBC API
    • ProudNet 유틸리티
  • ProudNet 참고자료
    • 기술노트
      • 메인 루프의 이해
      • 서버 방화벽 설정
      • 암호화 및 복호화
      • 에러발생 시 대처사항
      • 에러 메시지 목록
      • 캐릭터 위치 동기화
      • 클라이언트-서버 통신
      • MiniDump (오류덤프시스템)
      • [1.6 버전] 서버 간 LAN 통신기
    • 용어집
    • Sample 예제
  • 🌐프라우드 서비스
    • 콘솔 이용 안내
    • ProudChat
      • SDK 다운로드
        • C++
        • C#
        • Unity3D
        • Unreal Engine 4
      • 서비스 기능
Powered by GitBook
On this page
  • 클라이언트
  • - Proxy & Stub
  • - RMI 함수 정의
  • - NetClient 객체 생성
  • - 이벤트 연결
  • - Proxy & Stub 등록
  • - 서버 연결
  • 서버
  • - Proxy & Stub
  • - RMI 함수 정의
  • - NetServer 객체 생성
  • - 이벤트 연결
  • - Proxy & Stub 등록
  • 서버 시작
  • 공통
  • - vars.h
  • - vars.cpp, vars.cs

Was this helpful?

  1. ProudNet 참고자료

Sample 예제

Previous용어집Next콘솔 이용 안내

Last updated 1 year ago

Was this helpful?

아래 예제는 git 샘플 프로젝트를 기반으로 만들어졌습니다. 자세한 내용은 아래의 링크를 참고해 주세요.

클라이언트

- Proxy & Stub

// 클라이언트 -> 서버 RMI Proxy 인스턴스
Simple::Proxy g_SimpleProxy;

// RMI proxy와는 다르게 함수 오버라이딩 후 사용한다.
class SimpleStub : public Simple::Stub
{
public:
	DECRMI_Simple_ShowChat;
	DECRMI_Simple_SystemChat;

	DECRMI_Simple_P2PChat;
};

// 메세지를 받기 위한 RMI stub 인스턴스
SimpleStub g_SimpleStub;
using namespace Nettention.Proud;

// RMI proxy는 메시지를 보내는 데 사용된다.
// 함수 호출은 다른 프로세스에서 실행된다.
static Simple.Proxy g_Proxy = new Simple.Proxy();

// RMI stub은 메시지를 받는 데 사용된다.
static Simple.Stub g_Stub = new Simple.Stub();

- RMI 함수 정의

RMI 함수는 쉬운 네이밍을 위해 다음과 같은 규칙을 사용합니다.

=> DEFRMI_GlobalName_FunctionName

DEFRMI_Simple_P2PChat(SimpleStub)
{
    ...

    // 따로 의미는 없으나 반드시 true를 리턴해주어야 함.
    return true;
}

DEFRMI_Simple_ShowChat(SimpleStub)
{
    ...
    return true;
}

DEFRMI_Simple_SystemChat(SimpleStub)
{
    ...
    return true;
}
// 각 Stub 함수 수신 시 실행할 기능을 정의한 함수
g_Stub.P2PChat = (...) =>
{
    ...
    return true;
}
    
g_Stub.ShowChat = (...) =>
{
    ...
    return true;
}
    
g_Stub.SystemChat = (...) =>
{
    ...
    return true;
}

- NetClient 객체 생성

std::shared_ptr<Proud::CNetClient> netClient(Proud::CNetClient::Create());
Nettention.Proud.NetClient netClient = new Nettention.Proud.NetClient();

- 이벤트 연결

서버 연결 이벤트에서 필요한 로직을 설계하신 후 사용하시면 됩니다.

// 서버 연결이 완료된 후 호출되는 이벤트
netClient->OnJoinServerComplete = 
   [&](ErrorInfo *info, const ByteArray &replyFromServer)
{
    ...
}
    
// 서버 연결이 끊어졌을 때 실행할 이벤트
netClient->OnLeaveServer = [&](ErrorInfo *errorInfo)
{
    ...
}
    
// 새로운 p2p 연결이 들어왔을 때 실행될 이벤트
// memberHostID : p2p 연결된 클라이언트 아이디
// groupHostID : p2p 연결된 그룹 아이디
netClient->OnP2PMemberJoin = 
    [&](HostID memberHostID, HostID groupHostID,int memberCount, const ByteArray &customField)
{
    ...
}
    
// p2p 연결이 끊겼을 때 실행될 이벤트 
netClient->OnP2PMemberLeave = 
    [](HostID memberHostID, HostID groupHostID,int memberCount)
{
    ...
}
// 서버 연결이 완료된 후 호출되는 이벤트
netClient.JoinServerCompleteHandler = (info, replyFromServer) =>
{
    ...
};

// 서버 연결이 끊어졌을 때 실행할 로직
netClient.LeaveServerHandler = (errorInfo) =>
{
    ...
};

// p2p 그룹에 새로운 멤버가 추가됐을 때 실행할 로직
netClient.P2PMemberJoinHandler = 
    (memberHostID, groupHostID, memberCount, customField) =>
{
    ...
};

// p2p 멤버 접속이 끊겼을 때 실행할 로직
netClient.P2PMemberLeaveHandler = (memberHostID, groupHostID, memberCount) =>
{
    ...
};

- Proxy & Stub 등록

// CNetClient에 유저가 만든 Proxy와 Stub을 등록한다
netClient->AttachProxy(&g_SimpleProxy);
netClient->AttachStub(&g_SimpleStub);
// NetClient 인스턴스에 proxy와 stub 연결
netClient.AttachProxy(g_Proxy);	    // Client-to-server => 클라이언트에서 서버로
netClient.AttachStub(g_Stub);	   // server-to-client => 서버에서 클라이언트로

- 서버 연결

// 서버 시작에 필요한 Parameter 설정하기
Proud::CNetConnectionParam cp;

// 서버와 같은 protocol 버전을 입력해야 한다. 아예 입력하지 않을 수도 있음.
cp.m_protocolVersion = g_Version;
cp.m_closeNoPingPongTcpConnections = false;
cp.m_serverPort = g_ServerPort;	

// 서버와 연결 시작
// 이 함수는 즉시 return 됩니다.
// 그동안 백그라운드에서 연결을 시도하고,
// 결과는 OnJoinServerComplete 이벤트에 의해서 알려집니다.
netClient->Connect(cp);

이후 매 프레임 호출이 되는 함수에서 netClient -> FrameMove()를 호출합니다.

// 예시
while (true)
{
    netClient->FrameMove();
}
// 서버 시작에 필요한 Parameter 설정하기
Nettention.Proud.NetConnectionParam cp = 
    new Nettention.Proud.NetConnectionParam();

// 서버와 동일한 protocol version
cp.protocolVersion.Set(SimpleCSharp.Vars.m_Version);

// server address
cp.serverIP = "localhost";

// server port
cp.serverPort = (ushort)SimpleCSharp.Vars.m_serverPort;

// 서버 연결 시작
// 이 함수는 즉시 리턴됩니다.
// 그동안 백그라운드에서 연결을 시도하고,
// OnJoinServerComplete 이벤트에 의해 접속 완료를 알 수 있습니다.
netClient.Connect(cp);

이후 매 프레임 호출이 되는 함수에서 netClient.FrameMove() 를 호출합니다.

// 예시
while (true)
{
    netClient.FrameMove();
}

기본적으로 FrameMove를 진행해주셔야 이벤트가 발생합니다.

서버

- Proxy & Stub

// 서버에서 클라이언트로 전송을 위한 RMI Proxy
Simple::Proxy g_SimpleProxy;


// RMI proxy와는 다르게 함수 오버라이딩 후 사용한다.
class SimpleStub : public Simple::Stub
{
public:
	DECRMI_Simple_Chat;
};

// 서버 -> 클라이언트 RMI Stub 인스턴스
SimpleStub g_SimpleStub;
using namespace Nettention.Proud;

// 클라이언트에서 오는 메시지를 받기 위한 RMI Stub
static Simple.Stub g_Stub = new Simple.Stub();

// RMI proxy
static Simple.Proxy g_Proxy = new Simple.Proxy();

- RMI 함수 정의

DEFRMI_Simple_Chat(SimpleStub)
{
    ...
    return true;
}
// 클라이언트에서 채팅 메시지가 왔을 때 실행할 로직을 정의합니다.
g_Stub.Chat = (...) =>
{
    ...
    return true;
};

- NetServer 객체 생성

std::shared_ptr<Proud::CNetServer> srv(Proud::CNetServer::Create());
Nettention.Proud.NetServer srv = new Nettention.Proud.NetServer();

- 이벤트 연결

// 클라이언트가 서버에 접속했을 때 실행할 로직을 설정한다.
srv->OnClientJoin = 
    [](CNetClientInfo* clientInfo, ErrorInfo* errorInfo, const ByteArray& comment)
{
    ...
};

// 클라이언트 서버 접속이 끊어졌을 때 실행할 로직을 설정한다.
srv->OnClientLeave = 
    [](CNetClientInfo *clientInfo, ErrorInfo *errorInfo, const ByteArray& comment)
{
    ...
};
// 클라이언트가 서버에 접속했을 때 실행할 로직을 설정한다.
srv.ClientJoinHandler = (clientInfo) =>
{
    ...
};

// 클라이언트 서버 접속이 끊어졌을 때 실행할 로직을 설정한다.
srv.ClientLeaveHandler = (clientInfo, errorInfo, comment) =>
{
    ...
};

- Proxy & Stub 등록

// 생성된 CNetServer 인스턴스에 proxy와 stub을 등록한다.
srv->AttachStub(&g_SimpleStub);
srv->AttachProxy(&g_SimpleProxy);
// 생성된 CNetServer 인스턴스에 proxy와 stub을 등록한다.
srv.AttachStub(g_Stub);
srv.AttachProxy(g_Proxy);

서버 시작

Proud::CStartServerParameter p1;

// This must be the same to the client. => 클라이언트와 같아야 함.
p1.m_protocolVersion = g_Version; 

// TCP listening endpoint => tcp 연결 들어오는 port
p1.m_tcpPorts.Add(g_ServerPort); 

/* 서버 시작
이 함수는 실패시 exception을 발생시킨다.
특별히 threading model을 구체화하지 않는다면
메시지 송신에 의한 RMI 함수와 이벤트 콜백은 나눠진 thread pool에서 호출된다.
thread model을 따로 지정할 수 있는데 해당 부분은 도움말을 참고하면 된다. https://guide.nettention.com/cpp_ko#thread_pool_sharing
*/
srv->Start(p1);
var p1 = new Nettention.Proud.StartServerParameter();

// This must be the same to the client. => 클라이언트와 같아야 함.
p1.protocolVersion = new Nettention.Proud.Guid(Vars.m_Version); 

// TCP listening endpoint => tcp 연결 들어오는 port
p1.tcpPorts.Add(Vars.m_serverPort);

/* 서버 시작
이 함수는 실패시 exception을 발생시킨다.
특별히 threading model을 구체화하지 않는다면
메시지 송신에 의한 RMI 함수와 이벤트 콜백은 나눠진 thread pool에서 호출된다.
thread model을 따로 지정할 수 있는데 해당 부분은 도움말을 참고하면 된다. https://guide.nettention.com/cpp_ko#thread_pool_sharing
*/
srv.Start(p1);

공통

- vars.h

extern Proud::Guid g_Version;
extern int g_ServerPort;

- vars.cpp, vars.cs

// 정의한 프로토콜 버전
// 서버와 클라이언트가 모두 동일한 값을 가져야 한다.
PNGUID guid = { 0x3ae33249, 0xecc6, 0x4980, { 0xbc, 0x5d, 0x7b, 0xa, 0x99, 0x9c, 0x7, 0x39 } };
Guid g_Version = Guid(guid);

// TCP listening port number.
int g_ServerPort = 33334;
namespace SimpleCSharp
{
    public class Vars
    {
        // 서버와 클라이언트에 동일하게 적용할 protocol version
        public static System.Guid m_Version = new System.Guid("{ 0x3ae33249, 0xecc6, 0x4980, { 0xbc, 0x5d, 0x7b, 0xa, 0x99, 0x9c, 0x7, 0x39 } }");
        public static int m_serverPort = 33334;
    }
}

📂
📂
C++ 예제 다운로드
C# 예제 다운로드