Sample examples

The examples below are based on the git sample project, see the link below for more information.

📂 Download C++ examples

📂 Download C# examples

Client

- Proxy & Stub

// Client -> Server RMI Proxy instance
Simple::Proxy g_SimpleProxy;

// Unlike an RMI proxy, it is used after overriding a function.
class SimpleStub : public Simple::Stub
{
public:
	DECRMI_Simple_ShowChat;
	DECRMI_Simple_SystemChat;

	DECRMI_Simple_P2PChat;
};

// RMI stub instance for receiving messages
SimpleStub g_SimpleStub;

- Defining RMI functions

RMI functions use the following conventions for easy naming.

=> DEFRMI_GlobalName_FunctionName

DEFRMI_Simple_P2PChat(SimpleStub)
{
    ...

    // It has no special meaning, but must return true.
    return true;
}

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

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

- Create a NetClient object

std::shared_ptr<Proud::CNetClient> netClient(Proud::CNetClient::Create());

- Connecting Events

You can design the logic you need in the server connection event and then use it.

// Events that are called after the server connection is complete
netClient->OnJoinServerComplete = 
   [&](ErrorInfo *info, const ByteArray &replyFromServer)
{
    ...
}
    
// Events to run when the server is disconnected
netClient->OnLeaveServer = [&](ErrorInfo *errorInfo)
{
    ...
}
    
// Event to fire when a new p2p connection comes in
// memberHostID : p2p Connected Client ID
// groupHostID : p2p Connected Group ID
netClient->OnP2PMemberJoin = 
    [&](HostID memberHostID, HostID groupHostID,int memberCount, const ByteArray &customField)
{
    ...
}
    
// Events to fire when a p2p connection is lost 
netClient->OnP2PMemberLeave = 
    [](HostID memberHostID, HostID groupHostID,int memberCount)
{
    ...
}

- Proxy & Stub Registration

// Register the proxy and stub you created with CNetClient
netClient->AttachProxy(&g_SimpleProxy);
netClient->AttachStub(&g_SimpleStub);

- Connect to a server

// Setting the parameters needed to start the server
Proud::CNetConnectionParam cp;

// You must enter the same protocol version as the server. You may not enter any at all.
cp.m_protocolVersion = g_Version;
cp.m_closeNoPingPongTcpConnections = false;
cp.m_serverPort = g_ServerPort;	

// Start connecting to the server
// This function returns immediately.
// In the meantime, it will try to connect in the background,
// the result is signaled by the OnJoinServerComplete event.
netClient->Connect(cp);

Then, in a function that will be called every frame, call netClient -> FrameMove().

// Example
while (true)
{
    netClient->FrameMove();
}

By default, a FrameMove must be performed before the event is fired.

Server

- Proxy & Stub

// RMI Proxy for Server to Client Transfers
Simple::Proxy g_SimpleProxy;


// Unlike an RMI proxy, it is used after overriding a function.
class SimpleStub : public Simple::Stub
{
public:
	DECRMI_Simple_Chat;
};

// Server -> Client RMI Stub Instance
SimpleStub g_SimpleStub;

- Defining RMI functions

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

- Create a NetServer object

std::shared_ptr<Proud::CNetServer> srv(Proud::CNetServer::Create());

- Connecting Events

// Sets the logic to run when a client connects to the server.
srv->OnClientJoin = 
    [](CNetClientInfo* clientInfo, ErrorInfo* errorInfo, const ByteArray& comment)
{
    ...
};

// Sets the logic to run when the client server connection is lost.
srv->OnClientLeave = 
    [](CNetClientInfo *clientInfo, ErrorInfo *errorInfo, const ByteArray& comment)
{
    ...
};

- Proxy & Stub Registration

// Register the proxy and stub on the created CNetServer instance.
srv->AttachStub(&g_SimpleStub);
srv->AttachProxy(&g_SimpleProxy);

Start a server

Proud::CStartServerParameter p1;

// This must be the same to the client. => Must be the same as the client.
p1.m_protocolVersion = g_Version; 

// TCP listening endpoint => TCP connections incoming PORT
p1.m_tcpPorts.Add(g_ServerPort); 

/* Start the server
This function raises an exception on failure.
If you don't specifically specify a threading model, 
RMI functions and event callbacks by sending messages are invoked in a partitioned thread pool.
You can specify the thread model separately, which is covered in the help. https://guide.nettention.com/cpp_ko#thread_pool_sharing
*/
srv->Start(p1);

Common

- vars.h

extern Proud::Guid g_Version;
extern int g_ServerPort;

- vars.cpp, vars.cs

// Defined protocol version
// Both the server and client must have the same value.
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;

Last updated