Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
spdlog
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
spdlog
Commits
9bb66c00
Commit
9bb66c00
authored
Sep 05, 2021
by
gabime
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cleanup windows udp client
parent
1ec50cdc
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
61 deletions
+26
-61
include/spdlog/details/udp_client-windows.h
include/spdlog/details/udp_client-windows.h
+23
-55
include/spdlog/sinks/udp_sink.h
include/spdlog/sinks/udp_sink.h
+3
-6
No files found.
include/spdlog/details/udp_client-windows.h
View file @
9bb66c00
...
@@ -3,16 +3,12 @@
...
@@ -3,16 +3,12 @@
#pragma once
#pragma once
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif // WIN32_LEAN_AND_MEAN
// udp client helper
#include <spdlog/common.h>
#include <spdlog/common.h>
#include <spdlog/details/os.h>
#include <spdlog/details/os.h>
#include <spdlog/details/windows_include.h>
#ifdef _WIN32
#include <winsock2.h>
#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdio.h>
...
@@ -26,28 +22,14 @@ namespace spdlog {
...
@@ -26,28 +22,14 @@ namespace spdlog {
namespace
details
{
namespace
details
{
class
udp_client
class
udp_client
{
{
const
int
TX_BUFFER_SIZE
=
1024
0
;
static
constexpr
int
TX_BUFFER_SIZE
=
1024
*
1
0
;
SOCKET
socket_
=
INVALID_SOCKET
;
SOCKET
socket_
=
INVALID_SOCKET
;
sockaddr_in
addr_
=
{
0
};
sockaddr_in
addr_
=
{
0
};
static
bool
winsock_initialized_
()
{
SOCKET
s
=
socket
(
PF_INET
,
SOCK_DGRAM
,
IPPROTO_UDP
);
if
(
s
==
INVALID_SOCKET
)
{
return
false
;
}
else
{
closesocket
(
s
);
return
true
;
}
}
static
void
init_winsock_
()
static
void
init_winsock_
()
{
{
WSADATA
wsaData
;
WSADATA
wsaData
;
auto
rv
=
WSAStartup
(
MAKEWORD
(
2
,
2
),
&
wsaData
);
auto
rv
=
::
WSAStartup
(
MAKEWORD
(
2
,
2
),
&
wsaData
);
if
(
rv
!=
0
)
if
(
rv
!=
0
)
{
{
throw_winsock_error_
(
"WSAStartup failed"
,
::
WSAGetLastError
());
throw_winsock_error_
(
"WSAStartup failed"
,
::
WSAGetLastError
());
...
@@ -63,52 +45,46 @@ class udp_client
...
@@ -63,52 +45,46 @@ class udp_client
throw_spdlog_ex
(
fmt
::
format
(
"udp_sink - {}: {}"
,
msg
,
buf
));
throw_spdlog_ex
(
fmt
::
format
(
"udp_sink - {}: {}"
,
msg
,
buf
));
}
}
public:
void
cleanup_
()
bool
is_init
()
const
{
{
return
socket_
!=
INVALID_SOCKET
;
if
(
socket_
!=
INVALID_SOCKET
)
{
::
closesocket
(
socket_
);
}
socket_
=
INVALID_SOCKET
;
::
WSACleanup
();
}
}
bool
init
(
const
std
::
string
&
host
,
uint16_t
port
)
public:
udp_client
(
const
std
::
string
&
host
,
uint16_t
port
)
{
{
// initialize winsock if needed
init_winsock_
();
if
(
!
winsock_initialized_
())
{
init_winsock_
();
}
addr_
.
sin_family
=
PF_INET
;
addr_
.
sin_family
=
PF_INET
;
addr_
.
sin_port
=
htons
(
port
);
addr_
.
sin_port
=
htons
(
port
);
addr_
.
sin_addr
.
s_addr
=
INADDR_ANY
;
addr_
.
sin_addr
.
s_addr
=
INADDR_ANY
;
InetPton
(
PF_INET
,
TEXT
(
host
.
c_str
()),
&
addr_
.
sin_addr
.
s_addr
);
InetPton
(
PF_INET
,
TEXT
(
host
.
c_str
()),
&
addr_
.
sin_addr
.
s_addr
);
socket_
=
socket
(
PF_INET
,
SOCK_DGRAM
,
0
);
socket_
=
::
socket
(
PF_INET
,
SOCK_DGRAM
,
0
);
if
(
socket_
==
INVALID_SOCKET
)
if
(
socket_
==
INVALID_SOCKET
)
{
{
int
last_error
=
::
WSAGetLastError
();
int
last_error
=
::
WSAGetLastError
();
WSACleanup
();
::
WSACleanup
();
throw_winsock_error_
(
"error: Create Socket failed"
,
last_error
);
throw_winsock_error_
(
"error: Create Socket failed"
,
last_error
);
}
}
int
option_value
=
TX_BUFFER_SIZE
;
int
option_value
=
TX_BUFFER_SIZE
;
if
(
setsockopt
(
socket_
,
SOL_SOCKET
,
SO_SNDBUF
,
(
char
*
)
&
option_value
,
sizeof
(
option_value
))
<
0
)
if
(
::
setsockopt
(
socket_
,
SOL_SOCKET
,
SO_SNDBUF
,
reinterpret_cast
<
const
char
*>
(
&
option_value
)
,
sizeof
(
option_value
))
<
0
)
{
{
int
last_error
=
::
WSAGetLastError
();
int
last_error
=
::
WSAGetLastError
();
cl
ose
();
cl
eanup_
();
throw_winsock_error_
(
"error: setsockopt(SO_SNDBUF) Failed!"
,
last_error
);
throw_winsock_error_
(
"error: setsockopt(SO_SNDBUF) Failed!"
,
last_error
);
}
}
return
true
;
}
}
void
close
()
~
udp_client
()
{
{
if
(
socket_
!=
-
1
)
cleanup_
();
{
::
closesocket
(
socket_
);
}
socket_
=
INVALID_SOCKET
;
WSACleanup
();
}
}
SOCKET
fd
()
const
SOCKET
fd
()
const
...
@@ -116,22 +92,14 @@ public:
...
@@ -116,22 +92,14 @@ public:
return
socket_
;
return
socket_
;
}
}
~
udp_client
()
{
close
();
}
void
send
(
const
char
*
data
,
size_t
n_bytes
)
void
send
(
const
char
*
data
,
size_t
n_bytes
)
{
{
socklen_t
tolen
=
sizeof
(
struct
sockaddr
);
socklen_t
tolen
=
sizeof
(
struct
sockaddr
);
if
(
sendto
(
socket_
,
data
,
static_cast
<
int
>
(
n_bytes
),
0
,
(
struct
sockaddr
*
)
&
addr_
,
tolen
)
==
-
1
)
if
(
::
sendto
(
socket_
,
data
,
static_cast
<
int
>
(
n_bytes
),
0
,
(
struct
sockaddr
*
)
&
addr_
,
tolen
)
==
-
1
)
{
{
close
();
throw_spdlog_ex
(
"sendto(2) failed"
,
errno
);
throw_spdlog_ex
(
"sendto(2) failed"
,
errno
);
}
}
}
}
};
};
}
// namespace details
}
// namespace details
}
// namespace spdlog
}
// namespace spdlog
#endif // _WIN32
include/spdlog/sinks/udp_sink.h
View file @
9bb66c00
...
@@ -41,10 +41,8 @@ public:
...
@@ -41,10 +41,8 @@ public:
// host can be hostname or ip address
// host can be hostname or ip address
explicit
udp_sink
(
udp_sink_config
sink_config
)
explicit
udp_sink
(
udp_sink_config
sink_config
)
:
config_
{
std
::
move
(
sink_config
)}
:
client_
{
sink_config
.
server_host
,
sink_config
.
server_port
}
{
{}
client_
.
init
(
config_
.
server_host
,
config_
.
server_port
);
}
~
udp_sink
()
override
=
default
;
~
udp_sink
()
override
=
default
;
...
@@ -56,8 +54,7 @@ protected:
...
@@ -56,8 +54,7 @@ protected:
client_
.
send
(
formatted
.
data
(),
formatted
.
size
());
client_
.
send
(
formatted
.
data
(),
formatted
.
size
());
}
}
void
flush_
()
override
{}
void
flush_
()
override
{}
udp_sink_config
config_
;
details
::
udp_client
client_
;
details
::
udp_client
client_
;
};
};
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment