Sample examples
Last updated
Last updated
The examples below are based on the git sample project, see the link below for more information.
// 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;
using namespace Nettention.Proud;
// An RMI proxy is used to send messages.
// The function call is executed in a different process.
static Simple.Proxy g_Proxy = new Simple.Proxy();
// The RMI stub is used to receive messages.
static Simple.Stub g_Stub = new Simple.Stub();
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;
}
// A function that defines what to do when each stub function is received.
g_Stub.P2PChat = (...) =>
{
...
return true;
}
g_Stub.ShowChat = (...) =>
{
...
return true;
}
g_Stub.SystemChat = (...) =>
{
...
return true;
}
std::shared_ptr<Proud::CNetClient> netClient(Proud::CNetClient::Create());
Nettention.Proud.NetClient netClient = new Nettention.Proud.NetClient();
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)
{
...
}
// Events that are called after the server connection is complete
netClient.JoinServerCompleteHandler = (info, replyFromServer) =>
{
...
};
// Logic to run when the server is disconnected
netClient.LeaveServerHandler = (errorInfo) =>
{
...
};
// Logic to run when a new member is added to a p2p group
netClient.P2PMemberJoinHandler =
(memberHostID, groupHostID, memberCount, customField) =>
{
...
};
// Logic to execute when a p2p member is disconnected
netClient.P2PMemberLeaveHandler = (memberHostID, groupHostID, memberCount) =>
{
...
};
// Register the proxy and stub you created with CNetClient
netClient->AttachProxy(&g_SimpleProxy);
netClient->AttachStub(&g_SimpleStub);
// Connecting a proxy and stub to a NetClient instance
netClient.AttachProxy(g_Proxy); // Client-to-server => From client to server
netClient.AttachStub(g_Stub); // server-to-client => From server to client
// 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();
}
// Setting the parameters needed to start the server
Nettention.Proud.NetConnectionParam cp =
new Nettention.Proud.NetConnectionParam();
// Same protocol version as the server
cp.protocolVersion.Set(SimpleCSharp.Vars.m_Version);
// server address
cp.serverIP = "localhost";
// server port
cp.serverPort = (ushort)SimpleCSharp.Vars.m_serverPort;
// Start connecting to the server
// This function returns immediately.
// In the meantime, it will try to connect in the background,
// the OnJoinServerComplete event indicates that the connection is complete.
netClient.Connect(cp);
Call netClient.FrameMove() in a function that will be called every frame thereafter.
// Example
while (true)
{
netClient.FrameMove();
}
By default, a FrameMove must be performed before the event is fired.
// 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;
using namespace Nettention.Proud;
// RMI Stub for receiving messages from clients
static Simple.Stub g_Stub = new Simple.Stub();
// RMI proxy
static Simple.Proxy g_Proxy = new Simple.Proxy();
DEFRMI_Simple_Chat(SimpleStub)
{
...
return true;
}
// Define logic to run when a client receives a chat message.
g_Stub.Chat = (...) =>
{
...
return true;
};
std::shared_ptr<Proud::CNetServer> srv(Proud::CNetServer::Create());
Nettention.Proud.NetServer srv = new Nettention.Proud.NetServer();
// 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)
{
...
};
// Sets the logic to run when a client connects to the server.
srv.ClientJoinHandler = (clientInfo) =>
{
...
};
// Sets the logic to run when the client server connection is lost.
srv.ClientLeaveHandler = (clientInfo, errorInfo, comment) =>
{
...
};
// Register the proxy and stub on the created CNetServer instance.
srv->AttachStub(&g_SimpleStub);
srv->AttachProxy(&g_SimpleProxy);
// Register the proxy and stub on the created CNetServer instance.
srv.AttachStub(g_Stub);
srv.AttachProxy(g_Proxy);
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);
var p1 = new Nettention.Proud.StartServerParameter();
// This must be the same to the client. => Must be the same as the client.
p1.protocolVersion = new Nettention.Proud.Guid(Vars.m_Version);
// TCP listening endpoint => TCP connection incoming PORT
p1.tcpPorts.Add(Vars.m_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);
extern Proud::Guid g_Version;
extern int g_ServerPort;
// 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;
namespace SimpleCSharp
{
public class Vars
{
// protocol version to apply equally to server and client
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;
}
}