-
Notifications
You must be signed in to change notification settings - Fork 8
/
comms_utils.h
125 lines (99 loc) · 3 KB
/
comms_utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#pragma once
// TODO: remove some?
#include <arpa/inet.h>
#include <assert.h>
#include <atomic>
#include <cstring>
#include <errno.h>
#include <fcntl.h>
#include <ifaddrs.h>
#include <iostream>
#include <mutex>
#include <netdb.h>
#include <netinet/in.h>
#include <signal.h>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <thread>
#include <unistd.h>
#include <vector>
#include "third_party/argparse/argparse.hpp"
namespace comms {
// C programmers are crazy...
using Socket = int;
#define PRINT_NAME_VAR(x) std::cout << #x << " :" << x << std::endl;
namespace internal {
struct SilentCoutInternal {
SilentCoutInternal()
{
oldCoutStreamBuf = std::cout.rdbuf();
std::cout.rdbuf(strCout.rdbuf());
}
~SilentCoutInternal()
{
std::cout.rdbuf(oldCoutStreamBuf);
}
std::ostringstream strCout;
std::streambuf* oldCoutStreamBuf = nullptr;
};
}
// TODO: logger
// #define SLIENT_COUT_CURRENT_SCOPE internal::SilentCoutInternal var_name_duplicated;
#define SLIENT_COUT_CURRENT_SCOPE
template <typename T>
char* cast_to_char_ptr(T* const ptr)
{
return reinterpret_cast<char*>(ptr);
}
// TODO: class
// These functions make sure class get destructive whenever ctrl+c get pressed.
namespace control {
std::atomic<bool> quit(false); // signal flag
void got_signal(int);
void set_gracefully_exit();
bool program_exit();
}
// Handing ICP data packging
// e.g. server: send(100byte), send(100byte)
// client: 20byte = recv(), 110byte = recv(), 70byte = recv
// The protocol is simplely a sperator for each message.
namespace package_sync {
char control_string[] = "I am just some magic number. like 123456789. \
I use it to singal the start of a message. \
make sure you dont type it in your message! \
I am curious how do people handle it in \
pratice? Special character? definitely \
not a super long string like this.";
bool send_control_package(int socket);
enum class SyncStatus {
success,
failure,
timeout
};
// Wait for the header
SyncStatus wait_for_control_package(int socket, char* buf, int& received_data);
}
// https://beej.us/guide/bgnet/html//index.html
bool sendall(int socket, char* buf, int len);
// https://beej.us/guide/bgnet/html//index.html
bool recv_all(int socket, char* buf, int want_size_byte);
void sigchld_handler(int s);
// get sockaddr, IPv4 or IPv6:
void* get_in_addr(struct sockaddr* sa);
bool kill_dead_processes();
void print_current_ip();
struct Arguments {
std::string ip;
std::string port;
std::string image_dir;
};
// Thank you hbristow!
Arguments tcp_ip_image_server_argparse(int argc, const char** argv);
// Thank you hbristow!
Arguments tcp_ip_argparse(int argc, const char** argv);
}