diff options
Diffstat (limited to 'client/main.cpp')
-rw-r--r-- | client/main.cpp | 72 |
1 files changed, 28 insertions, 44 deletions
diff --git a/client/main.cpp b/client/main.cpp index 30d7045..5c26107 100644 --- a/client/main.cpp +++ b/client/main.cpp @@ -1,54 +1,38 @@ #include <cstdio> #include <cstdlib> -#include <cstring> -#include <unistd.h> - -#include "puzbusv1.h" - -int send_message() { - const char* data = "Test message data!"; - struct pb_msg output = { - .addr = 0x39, - .data = (char*) data, - .length = strlen(data), - }; - - char* packed; - size_t size; - if (!pb_write(&output, &packed, &size)) { - printf("error writing!\n"); - return 1; - } +#include <cstdint> +#include <exception> - fwrite(packed, sizeof(packed[0]), size, stdout); - fflush(stdout); +#include "rl.h" +#include "sock.h" - return 0; -} +PBSocket* sock; -int read_message() { - freopen(NULL, "rb", stdin); // allow binary on stdin - struct pb_msg input; - - char buf[8]; // extremely small buffer to test chunked message parsing - size_t bytes = 0; - while ((bytes = fread(buf, sizeof(buf[0]), sizeof(buf), stdin)) > 0) { - if (!pb_read(&input, buf, bytes)) continue; - - printf("address: 0x%02x\n", input.addr); - printf("data: \"%.*s\"\n", input.length, input.data); - free(input.data); - return 0; +int main(int argc, char** argv) { + if (argc < 2) { + printf("usage: %s addr [port]\n", argv[0]); + return EXIT_FAILURE; } - return 1; -} + // parse arguments + char* addr = argv[1]; + uint16_t port = 9191; + if (argc >= 3) port = atoi(argv[2]); + + sock = new PBSocket(addr, port); + try { + // connect to TCP socket (automatically spawns thread) + sock->sock_connect(); + } catch (const std::exception& e) { + printf("error: %s\n", e.what()); + return EXIT_FAILURE; + } + + // enter main CLI (using GNU readline for comfyness) + int ret = cli_main(); + + delete sock; -int main() { - if (!isatty(fileno(stdout))) return send_message(); - if (!isatty(fileno(stdin))) return read_message(); - - printf("please pipe some data in or out to use this program\n"); - return 0; + return ret; } |