MCPcopy Create free account
hub / github.com/HumbleNet/HumbleNet / process_message

Function process_message

tests/hello_world.cpp:62–100  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

60}
61
62void process_message(PeerId remotePeer, const uint8_t* buffer, int buffer_size, int message_size)
63{
64 // NOTE message_size is how big the message actually was. if it was bigger than our buffer size it WILL be truncated!
65 int length = std::max(buffer_size, message_size);
66
67 if (length > 2) {
68 MessageType type = (MessageType)buffer[0];
69 switch (type) {
70 case MessageType::TEXT:
71 // We received a text message.. Lets display it!
72 {
73 int size = buffer[1];
74 if (size > 0) {
75 // make sure the buffer was not truncated
76 if (size > (length - 2)) return;
77
78 // copy the text out of it (skipping the first 2 bytes which are out type + length)
79 std::string text((const char *)(buffer + 2), size);
80
81 std::cout << "Message TEXT from " << remotePeer << " : " << text << std::endl;
82 }
83 }
84 break;
85 case MessageType::HELLO:
86 // We received a hello message. Send an ack.
87 std::cout << "Received HELLO from " << remotePeer << std::endl;
88 send_message(remotePeer, MessageType::ACK, "", 0);
89 break;
90 case MessageType::ACK:
91 // we received an ACK message. lets check if it is the right peer.
92 std::cout << "Received ACK from " << remotePeer << std::endl;
93 if (remotePeer == startPeerId) {
94 startPeerStatus = PeerStatus::CONNECTED;
95 }
96 default:
97 break;
98 }
99 }
100}
101
102
103void sendChat(PeerId peer, const char* message)

Callers 1

main_loopFunction · 0.85

Calls 1

send_messageFunction · 0.85

Tested by

no test coverage detected