Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions wish/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,19 @@ add_library(wish_handler
${libevent_SOURCE_DIR}/bufferevent_ssl.c
src/tls_context.cc
src/tls_context.h
src/tls_server.cc
src/tls_server.h
src/tls_client.cc
src/tls_client.h
)
# BoringSSL targets: ssl and crypto
target_link_libraries(wish_handler wslay event ssl crypto)

add_executable(wish-server examples/server.cc)
target_link_libraries(wish-server wish_handler)
add_executable(echo_server examples/echo_server.cc)
target_link_libraries(echo_server wish_handler)

add_executable(wish-client examples/client.cc)
target_link_libraries(wish-client wish_handler)
Comment on lines -58 to -62
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removing the wish- prefix for now for simplicity

add_executable(hello_client examples/hello_client.cc)
target_link_libraries(hello_client wish_handler)

add_executable(benchmark_client examples/benchmark_client.cc)
target_link_libraries(benchmark_client wish_handler)
9 changes: 9 additions & 0 deletions wish/cpp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# WiSH C++ Implementation

This directory contains a C++ implementation of WiSH protocol together with a thin TLS stack enabling mTLS.

# Development Conventions

## Coding style

We basically follow [Google C++ style guide](https://google.github.io/styleguide/cppguide.html).
48 changes: 48 additions & 0 deletions wish/cpp/examples/benchmark_client.cc
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding a very simple base for benchmarking

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <chrono>
#include <iostream>
#include <string>
#include <vector>

#include "../src/tls_client.h"
#include "../src/wish_handler.h"

int main() {
TlsClient client("../certs/ca.crt", "../certs/client.crt",
"../certs/client.key", "127.0.0.1", 8080);

if (!client.Init()) {
std::cerr << "Failed to initialize client" << std::endl;
return 1;
}

const int kTotalMessages = 1000;
int messages_received = 0;
auto start_time = std::chrono::high_resolution_clock::now();

client.SetOnOpen([&start_time, kTotalMessages](WishHandler* handler) {
std::cout << "Connected! Starting benchmark..." << std::endl;

start_time = std::chrono::high_resolution_clock::now();

for (int i = 0; i < kTotalMessages; ++i) {
handler->SendText("Benchmark message " + std::to_string(i));
}
});

client.SetOnMessage([&messages_received, kTotalMessages,
&start_time](uint8_t opcode, const std::string& msg) {
messages_received++;
if (messages_received == kTotalMessages) {
auto end_time = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(
end_time - start_time);
std::cout << "Benchmark complete: Received " << kTotalMessages
<< " messages in " << duration.count() << " ms." << std::endl;
exit(0);
}
});

client.Run();

return 0;
}
80 changes: 0 additions & 80 deletions wish/cpp/examples/client.cc

This file was deleted.

64 changes: 64 additions & 0 deletions wish/cpp/examples/echo_server.cc
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renamed to clarify that this is an echo server implementation

Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <iostream>
#include <string>

#include "../src/tls_server.h"
#include "../src/wish_handler.h"

int main(int argc, char** argv) {
int port = 8080;

TlsServer server("../certs/ca.crt", "../certs/server.crt",
"../certs/server.key", port);

if (!server.Init()) {
std::cerr << "Failed to initialize server" << std::endl;
return 1;
}

server.SetOnConnection([](struct bufferevent* bev) {
std::cout << "Client connected." << std::endl;

WishHandler* handler = new WishHandler(bev, true);

handler->SetOnMessage([handler](uint8_t opcode, const std::string& msg) {
std::string type;
switch (opcode) {
case WISH_OPCODE_TEXT:
type = "TEXT";
break;
case WISH_OPCODE_BINARY:
type = "BINARY";
break;
case WISH_OPCODE_TEXT_METADATA:
type = "TEXT_METADATA";
break;
case WISH_OPCODE_BINARY_METADATA:
type = "BINARY_METADATA";
break;
default:
type = "UNKNOWN(" + std::to_string(opcode) + ")";
break;
}
std::cout << "Received [" << type << "]: " << msg << std::endl;

// Echo back
if (opcode == WISH_OPCODE_TEXT)
handler->SendText(msg);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed to not prefixing received data with "Echo: "

else if (opcode == WISH_OPCODE_BINARY)
handler->SendBinary(msg);
else if (opcode == WISH_OPCODE_TEXT_METADATA)
handler->SendTextMetadata(msg);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed to echo back using the same opcode respectively

else if (opcode == WISH_OPCODE_BINARY_METADATA)
handler->SendBinaryMetadata(msg);
else {
std::cerr << "Unknown opcode, cannot echo." << std::endl;
}
});

handler->Start();
});

server.Run();

return 0;
}
50 changes: 50 additions & 0 deletions wish/cpp/examples/hello_client.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <iostream>
#include <string>

#include "../src/tls_client.h"
#include "../src/wish_handler.h"

int main() {
TlsClient client("../certs/ca.crt", "../certs/client.crt",
"../certs/client.key", "127.0.0.1", 8080);

if (!client.Init()) {
std::cerr << "Failed to initialize client" << std::endl;
return 1;
}

client.SetOnOpen([](WishHandler* handler) {
std::cout << "Connected and Handshake Complete!" << std::endl;

handler->SendText("Hello WiSH Text!");
handler->SendBinary("Hello WiSH Binary!");
handler->SendTextMetadata("Hello WiSH Metadata!");
handler->SendBinaryMetadata("Hello WiSH Binary Metadata!");
});

client.SetOnMessage([](uint8_t opcode, const std::string& msg) {
std::string type;
switch (opcode) {
case WISH_OPCODE_TEXT:
type = "TEXT";
break;
case WISH_OPCODE_BINARY:
type = "BINARY";
break;
case WISH_OPCODE_TEXT_METADATA:
type = "TEXT_METADATA";
break;
case WISH_OPCODE_BINARY_METADATA:
type = "BINARY_METADATA";
break;
default:
type = "UNKNOWN(" + std::to_string(opcode) + ")";
break;
}
std::cout << "Server says [" << type << "]: " << msg << std::endl;
});

client.Run();

return 0;
}
Loading