5.Making a P2P group

넷텐션

클라이언트간 P2P 통신을 하려면 P2P 그룹을 서버에서 생성해야 합니다. 서버에서는 특정 키를 누르면 접속한 클라이언트들의 목록을 얻어서 이들이 모두 1개의 P2P 그룹을 맺어지게 합시다.

P2P 그룹에 대해서는 12. P2P Group을 참고하십시오.

while (true)
{
    // get user input
    cin >> userInput;

    if ( userInput == "1" )
    {
        // get all client HostID array.
        vector<HostID> clients;
        int noofClients = srv->GetClientCount();
        clients.resize(noofClients);
        int listCount = srv->GetClientHostIDs(&clients[0], noofClients);

        // create a P2P group where all clients are in.
        g_groupHostID = srv->CreateP2PGroup(&clients[0], clients.size());
}

위 루틴이 쓰는 전역 변수도 선언합시다.

// a P2P group where all clients are in.
HostID g_groupHostID = HostID_None;

클라이언트에서는 P2P 그룹이 맺어지면 그 상황을 출력하게 만듭시다. (참고: 12.1 Creating a new P2P group)

// set a routine for P2P member join (P2P available)
netClient->OnP2PMemberJoin = [&](HostID memberHostID, HostID groupHostID,
    int memberCount, const ByteArray &customField) {
    // memberHostID = P2P connected client ID
    // groupHostID = P2P group ID where the P2P peer is in.
    printf("[Client] P2P member %d joined group %d.\n", 
        memberHostID, groupHostID);
    recentP2PGroupHostID = groupHostID;
};

// called when a P2P member left.
netClient->OnP2PMemberLeave = 
    [](HostID memberHostID, HostID groupHostID,int memberCount)
{
    printf("[Client] P2P member %d left group %d.\n", 
        memberHostID, groupHostID);
};