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
c0d10efa
Commit
c0d10efa
authored
Sep 05, 2021
by
gabime
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cleanup unix udp client
parent
fecb3f43
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
25 deletions
+26
-25
include/spdlog/details/udp_client.h
include/spdlog/details/udp_client.h
+26
-25
No files found.
include/spdlog/details/udp_client.h
View file @
c0d10efa
...
@@ -3,11 +3,13 @@
...
@@ -3,11 +3,13 @@
#pragma once
#pragma once
// Helper RAII over unix udp client socket.
// Will throw on construction if the socket creation failed.
#ifdef _WIN32
#ifdef _WIN32
#
error include udp_client-windows.h instead
#
error "include udp_client-windows.h instead"
#endif
#endif
// udp client helper
#include <spdlog/common.h>
#include <spdlog/common.h>
#include <spdlog/details/os.h>
#include <spdlog/details/os.h>
...
@@ -24,41 +26,46 @@ namespace details {
...
@@ -24,41 +26,46 @@ namespace details {
class
udp_client
class
udp_client
{
{
const
int
TX_BUFFER_SIZE
=
1024
0
;
static
constexpr
int
TX_BUFFER_SIZE
=
1024
*
1
0
;
int
socket_
=
-
1
;
int
socket_
=
-
1
;
struct
sockaddr_in
sockAddr_
;
struct
sockaddr_in
sockAddr_
;
public:
bool
init
(
const
std
::
string
&
host
,
uint16_t
port
)
void
cleanup_
()
{
if
(
socket_
!=
-
1
)
{
::
close
(
socket_
);
socket_
=
-
1
;
}
}
public:
udp_client
(
const
std
::
string
&
host
,
uint16_t
port
)
{
{
socket_
=
socket
(
PF_INET
,
SOCK_DGRAM
,
0
);
socket_
=
::
socket
(
PF_INET
,
SOCK_DGRAM
,
0
);
if
(
socket_
<
0
)
if
(
socket_
<
0
)
{
{
throw_spdlog_ex
(
"error: Create Socket Failed!"
);
throw_spdlog_ex
(
"error: Create Socket Failed!"
);
}
}
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
)
{
{
cl
ose
();
cl
eanup_
();
throw_spdlog_ex
(
"error: setsockopt(SO_SNDBUF) Failed!"
);
throw_spdlog_ex
(
"error: setsockopt(SO_SNDBUF) Failed!"
);
}
}
sockAddr_
.
sin_family
=
AF_INET
;
sockAddr_
.
sin_family
=
AF_INET
;
sockAddr_
.
sin_port
=
htons
(
port
);
sockAddr_
.
sin_port
=
htons
(
port
);
inet_aton
(
host
.
c_str
(),
&
sockAddr_
.
sin_addr
);
::
inet_aton
(
host
.
c_str
(),
&
sockAddr_
.
sin_addr
);
memset
(
sockAddr_
.
sin_zero
,
0x00
,
sizeof
(
sockAddr_
.
sin_zero
));
::
memset
(
sockAddr_
.
sin_zero
,
0x00
,
sizeof
(
sockAddr_
.
sin_zero
));
return
true
;
}
}
void
close
()
~
udp_client
()
{
{
if
(
socket_
!=
-
1
)
cleanup_
();
{
::
close
(
socket_
);
socket_
=
-
1
;
}
}
}
int
fd
()
const
int
fd
()
const
...
@@ -66,20 +73,14 @@ public:
...
@@ -66,20 +73,14 @@ public:
return
socket_
;
return
socket_
;
}
}
~
udp_client
()
{
close
();
}
// Send exactly n_bytes of the given data.
// Send exactly n_bytes of the given data.
// On error close the connection and throw.
// On error close the connection and throw.
void
send
(
const
char
*
data
,
size_t
n_bytes
)
void
send
(
const
char
*
data
,
size_t
n_bytes
)
{
{
ssize_t
toslen
=
0
;
ssize_t
toslen
=
0
;
socklen_t
tolen
=
sizeof
(
struct
sockaddr
);
socklen_t
tolen
=
sizeof
(
struct
sockaddr
);
if
((
toslen
=
sendto
(
socket_
,
data
,
n_bytes
,
0
,
(
struct
sockaddr
*
)
&
sockAddr_
,
tolen
))
==
-
1
)
if
((
toslen
=
::
sendto
(
socket_
,
data
,
n_bytes
,
0
,
(
struct
sockaddr
*
)
&
sockAddr_
,
tolen
))
==
-
1
)
{
{
close
();
throw_spdlog_ex
(
"sendto(2) failed"
,
errno
);
throw_spdlog_ex
(
"sendto(2) failed"
,
errno
);
}
}
}
}
...
...
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