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
458 changes: 234 additions & 224 deletions wish/cpp/CMakeLists.txt

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions wish/cpp/examples/h2_hello_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ int main(int argc, char** argv) {
return 1;
}

client.SetOnError([&client]() {
LOG(ERROR) << "Client error or handshake failed";

client.Stop();
});

client.SetOnOpen([&client](WebStream* stream) {
LOG(INFO) << "OnOpen";

Expand Down Expand Up @@ -79,6 +85,12 @@ int main(int argc, char** argv) {
client.Stop();
});

stream->SetOnError([&client]() {
LOG(ERROR) << "Stream error";

client.Stop();
});

stream->SendText("Hello web-stream text over HTTP/2!");
stream->SendBinary("Hello web-stream binary over HTTP/2!");
stream->SendMetadata("Hello web-stream metadata over HTTP/2!");
Expand Down
12 changes: 12 additions & 0 deletions wish/cpp/examples/h2_tls_hello_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ int main(int argc, char** argv) {
return 1;
}

client.SetOnError([&client]() {
LOG(ERROR) << "Client error or handshake failed";

client.Stop();
});

client.SetOnOpen([&client](WebStream* stream) {
LOG(INFO) << "OnOpen";

Expand Down Expand Up @@ -101,6 +107,12 @@ int main(int argc, char** argv) {
client.Stop();
});

stream->SetOnError([&client]() {
LOG(ERROR) << "Stream error";

client.Stop();
});

stream->SendText("Hello web-stream text over HTTP/2+TLS!");
stream->SendBinary("Hello web-stream binary over HTTP/2+TLS!");
stream->SendMetadata("Hello web-stream metadata over HTTP/2+TLS!");
Expand Down
12 changes: 12 additions & 0 deletions wish/cpp/examples/plain_hello_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ int main(int argc, char** argv) {
return 1;
}

client.SetOnError([&client]() {
LOG(ERROR) << "Client error or handshake failed";

client.Stop();
});

client.SetOnOpen([&client](WebStream* stream) {
LOG(INFO) << "OnOpen";

Expand Down Expand Up @@ -79,6 +85,12 @@ int main(int argc, char** argv) {
client.Stop();
});

stream->SetOnError([&client]() {
LOG(ERROR) << "Stream error";

client.Stop();
});

stream->SendText("Hello web-stream text!");
stream->SendBinary("Hello web-stream binary!");
stream->SendMetadata("Hello web-stream metadata!");
Expand Down
216 changes: 114 additions & 102 deletions wish/cpp/examples/tls_hello_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,105 +12,117 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <absl/flags/flag.h>
#include <absl/flags/parse.h>
#include <absl/log/initialize.h>
#include <absl/log/log.h>

#include <string>

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

ABSL_FLAG(std::string, host, "127.0.0.1", "Server host address");
ABSL_FLAG(int, port, 8080, "Server port");

ABSL_FLAG(std::string,
ca_cert,
"certs/ca.crt",
"Path to CA certificate file");
ABSL_FLAG(std::string,
client_cert,
"certs/client.crt",
"Path to client certificate file");
ABSL_FLAG(std::string,
client_key,
"certs/client.key",
"Path to client private key file");

int main(int argc, char** argv) {
absl::ParseCommandLine(argc, argv);
absl::InitializeLog();

const std::string host = absl::GetFlag(FLAGS_host);
const int port = absl::GetFlag(FLAGS_port);

const std::string ca_cert = absl::GetFlag(FLAGS_ca_cert);
const std::string client_cert = absl::GetFlag(FLAGS_client_cert);
const std::string client_key = absl::GetFlag(FLAGS_client_key);

event_base* base = event_base_new();
if (!base) {
LOG(ERROR) << "Failed to create event_base";

return 1;
}

{
TlsClient client(base,
host,
port,
ca_cert,
client_cert,
client_key);

if (!client.Init()) {
LOG(INFO) << "Init() failed";

event_base_free(base);

return 1;
}

client.SetOnOpen([&client](WebStream* stream) {
LOG(INFO) << "OnOpen";

stream->SetOnMessage([](uint8_t opcode, const std::string& msg) {
std::string type;
switch (opcode) {
case WEB_STREAM_OPCODE_TEXT:
type = "TEXT";
break;
case WEB_STREAM_OPCODE_BINARY:
type = "BINARY";
break;
case WEB_STREAM_OPCODE_METADATA:
type = "METADATA";
break;
default:
type = "UNKNOWN(" + std::to_string(opcode) + ")";
break;
}

LOG(INFO) << "OnMessage (opcode: " << type << ", message: " << msg << ")";
});

stream->SetOnClose([&client]() {
LOG(INFO) << "OnClose";

client.Stop();
});

stream->SendText("Hello web-stream text!");
stream->SendBinary("Hello web-stream binary!");
stream->SendMetadata("Hello web-stream metadata!");
stream->Close();
});

client.Run();
}

event_base_free(base);

return 0;
}
#include <absl/flags/flag.h>
#include <absl/flags/parse.h>
#include <absl/log/initialize.h>
#include <absl/log/log.h>

#include <string>

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

ABSL_FLAG(std::string, host, "127.0.0.1", "Server host address");
ABSL_FLAG(int, port, 8080, "Server port");

ABSL_FLAG(std::string,
ca_cert,
"certs/ca.crt",
"Path to CA certificate file");
ABSL_FLAG(std::string,
client_cert,
"certs/client.crt",
"Path to client certificate file");
ABSL_FLAG(std::string,
client_key,
"certs/client.key",
"Path to client private key file");

int main(int argc, char** argv) {
absl::ParseCommandLine(argc, argv);
absl::InitializeLog();

const std::string host = absl::GetFlag(FLAGS_host);
const int port = absl::GetFlag(FLAGS_port);

const std::string ca_cert = absl::GetFlag(FLAGS_ca_cert);
const std::string client_cert = absl::GetFlag(FLAGS_client_cert);
const std::string client_key = absl::GetFlag(FLAGS_client_key);

event_base* base = event_base_new();
if (!base) {
LOG(ERROR) << "Failed to create event_base";

return 1;
}

{
TlsClient client(base,
host,
port,
ca_cert,
client_cert,
client_key);

if (!client.Init()) {
LOG(INFO) << "Init() failed";

event_base_free(base);

return 1;
}

client.SetOnError([&client]() {
LOG(ERROR) << "Client error or handshake failed";

client.Stop();
});

client.SetOnOpen([&client](WebStream* stream) {
LOG(INFO) << "OnOpen";

stream->SetOnMessage([](uint8_t opcode, const std::string& msg) {
std::string type;
switch (opcode) {
case WEB_STREAM_OPCODE_TEXT:
type = "TEXT";
break;
case WEB_STREAM_OPCODE_BINARY:
type = "BINARY";
break;
case WEB_STREAM_OPCODE_METADATA:
type = "METADATA";
break;
default:
type = "UNKNOWN(" + std::to_string(opcode) + ")";
break;
}

LOG(INFO) << "OnMessage (opcode: " << type << ", message: " << msg << ")";
});

stream->SetOnClose([&client]() {
LOG(INFO) << "OnClose";

client.Stop();
});

stream->SetOnError([&client]() {
LOG(ERROR) << "Stream error";

client.Stop();
});

stream->SendText("Hello web-stream text!");
stream->SendBinary("Hello web-stream binary!");
stream->SendMetadata("Hello web-stream metadata!");
stream->Close();
});

client.Run();
}

event_base_free(base);

return 0;
}
Loading