-
-
Notifications
You must be signed in to change notification settings - Fork 724
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[refactor] Move server common functions/constants to commonly accessi…
…ble location
- Loading branch information
Grégoire Roussel
committed
Jul 17, 2024
1 parent
0fc10a5
commit 3d235b8
Showing
9 changed files
with
149 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
#include "TracyProtocolServer.hpp" | ||
#include <cassert> | ||
#include <cstring> | ||
#include <iostream> | ||
|
||
namespace tracy | ||
{ | ||
std::optional<tracy::BroadcastMessage> ParseBroadcastMessage(const char* msg, size_t msgLen) | ||
{ | ||
if (msgLen < sizeof(uint16_t)) | ||
{ | ||
std::cout << "Received too short broadcast message" << std::endl; | ||
return std::nullopt; | ||
} | ||
uint16_t broadcastVersion; | ||
memcpy(&broadcastVersion, msg, sizeof(uint16_t)); | ||
if (broadcastVersion > tracy::BroadcastVersion) | ||
{ | ||
std::cout << "Received broadcast message with unsupported version: " << broadcastVersion << std::endl; | ||
return std::nullopt; | ||
} | ||
switch (broadcastVersion) | ||
{ | ||
case 3: | ||
{ | ||
if (msgLen > sizeof(tracy::BroadcastMessage)) | ||
{ | ||
std::cout << "Received unexpected size broadcast v3 message" << std::endl; | ||
return std::nullopt; | ||
} | ||
tracy::BroadcastMessage bm; | ||
memcpy(&bm, msg, msgLen); | ||
return bm; | ||
break; | ||
} | ||
case 2: | ||
{ | ||
if (msgLen > sizeof(tracy::BroadcastMessage_v2)) | ||
{ | ||
std::cout << "Received unexpected size broadcast v2 message" << std::endl; | ||
return std::nullopt; | ||
} | ||
tracy::BroadcastMessage_v2 bm; | ||
memcpy(&bm, msg, msgLen); | ||
|
||
tracy::BroadcastMessage out; | ||
out.broadcastVersion = broadcastVersion; | ||
out.protocolVersion = bm.protocolVersion; | ||
out.activeTime = bm.activeTime; | ||
out.listenPort = bm.listenPort; | ||
strcpy(out.programName, bm.programName); | ||
out.pid = 0; | ||
return out; | ||
break; | ||
} | ||
case 1: | ||
{ | ||
if (msgLen > sizeof(tracy::BroadcastMessage_v1)) | ||
{ | ||
std::cout << "Received unexpected size broadcast v1 message" << std::endl; | ||
return std::nullopt; | ||
} | ||
tracy::BroadcastMessage_v1 bm; | ||
memcpy(&bm, msg, msgLen); | ||
|
||
tracy::BroadcastMessage out; | ||
out.broadcastVersion = broadcastVersion; | ||
out.protocolVersion = bm.protocolVersion; | ||
out.activeTime = bm.activeTime; | ||
out.listenPort = bm.listenPort; | ||
strcpy(out.programName, bm.programName); | ||
out.pid = 0; | ||
return out; | ||
break; | ||
} | ||
case 0: | ||
{ | ||
if (msgLen > sizeof(tracy::BroadcastMessage_v0)) | ||
{ | ||
std::cout << "Received unexpected size broadcast v0 message" << std::endl; | ||
return std::nullopt; | ||
} | ||
tracy::BroadcastMessage_v0 bm; | ||
memcpy(&bm, msg, msgLen); | ||
|
||
tracy::BroadcastMessage out; | ||
out.broadcastVersion = broadcastVersion; | ||
out.protocolVersion = bm.protocolVersion; | ||
out.activeTime = bm.activeTime; | ||
out.listenPort = tracy::DEFAULT_CLIENT_DATA_TCP_PORT; | ||
strcpy(out.programName, bm.programName); | ||
out.pid = 0; | ||
return out; | ||
break; | ||
} | ||
default: | ||
assert(false); | ||
break; | ||
} | ||
} | ||
|
||
uint64_t ClientUniqueID(tracy::IpAddress const& addr, uint16_t port) | ||
{ | ||
return uint64_t(addr.GetNumber()) | (uint64_t(port) << 32); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// server-side functions supporting the protocol | ||
#ifndef __TRACYPROTOCOLSERVER_HPP__ | ||
#define __TRACYPROTOCOLSERVER_HPP__ | ||
|
||
#include <optional> | ||
#include "TracyProtocol.hpp" | ||
#include "TracySocket.hpp" | ||
|
||
namespace tracy | ||
{ | ||
// create the latest version of broadcast message, migrating older versions if possible | ||
std::optional<tracy::BroadcastMessage> ParseBroadcastMessage(const char* msg, size_t msgLen); | ||
// internal unique ID for a client | ||
uint64_t ClientUniqueID(tracy::IpAddress const& addr, uint16_t port); | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters