-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.h
42 lines (34 loc) · 871 Bytes
/
sync.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
#pragma once
#ifdef _WIN32
# include <winsock2.h>
#else
# include <sys/socket.h>
# include <netinet/in.h>
# include <arpa/inet.h>
#endif
#include <cstdarg>
#include <cstdio>
#include <iostream>
#include <mutex>
#include <string>
#include <sstream>
#include <thread>
using Mutex = std::mutex;
using Guard = std::lock_guard<Mutex>;
static Mutex g_lock;
static void
log(const char* format, ...) {
Guard guard{g_lock};
std::clog << "[" << std::this_thread::get_id() << "] ";
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
}
static std::string
endpoint_to_string(const sockaddr_in& address) {
Guard guard{g_lock};
std::ostringstream buffer;
buffer << ::inet_ntoa(address.sin_addr) << ':' << ::ntohs(address.sin_port);
return buffer.str();
}