Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
rlib
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Recolic
rlib
Commits
9fd25a21
There was an error fetching the commit references. Please try again later.
Commit
9fd25a21
authored
5 years ago
by
Recolic Keghart
Browse files
Options
Downloads
Patches
Plain Diff
add udp support to sio
parent
23127cae
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
sys/sio.hpp
+46
-12
46 additions, 12 deletions
sys/sio.hpp
sys/unix_handy.hpp
+22
-5
22 additions, 5 deletions
sys/unix_handy.hpp
with
68 additions
and
17 deletions
sys/sio.hpp
+
46
−
12
View file @
9fd25a21
...
...
@@ -91,7 +91,7 @@ namespace rlib {
#if RLIB_OS_ID == OS_WINDOWS
template
<
bool
doNotWSAStartup
=
false
>
static
inline
sockfd_t
quick_listen
(
const
std
::
string
&
addr
,
uint16_t
port
)
{
static
inline
sockfd_t
quick_listen
(
const
std
::
string
&
addr
,
uint16_t
port
,
bool
use_udp
=
false
)
{
WSADATA
wsaData
;
sockfd_t
listenfd
=
INVALID_SOCKET
;
if
(
!
doNotWSAStartup
)
{
...
...
@@ -102,9 +102,15 @@ namespace rlib {
addrinfo
*
psaddr
;
addrinfo
hints
{
0
};
hints
.
ai_family
=
AF_UNSPEC
;
hints
.
ai_socktype
=
SOCK_STREAM
;
if
(
use_udp
)
{
hints
.
ai_socktype
=
SOCK_DGRAM
;
hints
.
ai_protocol
=
IPPROTO_UDP
;
}
else
{
hints
.
ai_socktype
=
SOCK_STREAM
;
hints
.
ai_protocol
=
IPPROTO_TCP
;
}
hints
.
ai_flags
=
AI_PASSIVE
;
/* For wildcard IP address */
hints
.
ai_protocol
=
IPPROTO_TCP
;
auto
_
=
getaddrinfo
(
addr
.
c_str
(),
std
::
to_string
(
port
).
c_str
(),
&
hints
,
&
psaddr
);
if
(
_
!=
0
)
{
WSACleanup
();
...
...
@@ -126,14 +132,17 @@ namespace rlib {
}
if
(
!
success
)
throw
std
::
runtime_error
(
"Failed to bind to any of these addr."
);
if
(
SOCKET_ERROR
==
::
listen
(
listenfd
,
16
))
throw
std
::
runtime_error
(
"listen failed. {}"
_format
(
strerror
(
errno
)));
if
(
!
use_udp
)
{
// UDP don't need to listen.
if
(
SOCKET_ERROR
==
::
listen
(
listenfd
,
16
))
throw
std
::
runtime_error
(
"listen failed. {}"
_format
(
strerror
(
errno
)));
}
freeaddrinfo
(
psaddr
);
return
listenfd
;
}
template
<
bool
doNotWSAStartup
=
false
>
static
inline
sockfd_t
quick_connect
(
const
std
::
string
&
addr
,
uint16_t
port
)
{
static
inline
sockfd_t
quick_connect
(
const
std
::
string
&
addr
,
uint16_t
port
,
bool
use_udp
=
false
)
{
WSADATA
wsaData
;
sockfd_t
sockfd
=
INVALID_SOCKET
;
if
(
!
doNotWSAStartup
)
{
...
...
@@ -145,8 +154,15 @@ namespace rlib {
addrinfo
hints
{
0
};
hints
.
ai_family
=
AF_UNSPEC
;
hints
.
ai_socktype
=
SOCK_STREAM
;
hints
.
ai_protocol
=
IPPROTO_TCP
;
if
(
use_udp
)
{
hints
.
ai_socktype
=
SOCK_DGRAM
;
hints
.
ai_protocol
=
IPPROTO_UDP
;
}
else
{
hints
.
ai_socktype
=
SOCK_STREAM
;
hints
.
ai_protocol
=
IPPROTO_TCP
;
}
auto
_
=
getaddrinfo
(
addr
.
c_str
(),
std
::
to_string
(
port
).
c_str
(),
&
hints
,
&
paddr
);
if
(
_
!=
0
)
{
WSACleanup
();
...
...
@@ -175,13 +191,20 @@ namespace rlib {
#else
// POSIX version
static
inline
fd_t
quick_listen
(
const
std
::
string
&
addr
,
uint16_t
port
)
{
static
inline
fd_t
quick_listen
(
const
std
::
string
&
addr
,
uint16_t
port
,
bool
use_udp
=
false
)
{
addrinfo
*
psaddr
;
addrinfo
hints
{
0
};
fd_t
listenfd
;
hints
.
ai_family
=
AF_UNSPEC
;
hints
.
ai_socktype
=
SOCK_STREAM
;
if
(
use_udp
)
{
hints
.
ai_socktype
=
SOCK_DGRAM
;
hints
.
ai_protocol
=
IPPROTO_UDP
;
}
else
{
hints
.
ai_socktype
=
SOCK_STREAM
;
hints
.
ai_protocol
=
IPPROTO_TCP
;
}
hints
.
ai_flags
=
AI_PASSIVE
;
/* For wildcard IP address */
auto
_
=
getaddrinfo
(
addr
.
c_str
(),
std
::
to_string
(
port
).
c_str
(),
&
hints
,
&
psaddr
);
if
(
_
!=
0
)
throw
std
::
runtime_error
(
"Failed to getaddrinfo. returnval={}, check `man getaddrinfo`'s return value."
_format
(
_
));
...
...
@@ -204,19 +227,30 @@ namespace rlib {
}
if
(
!
success
)
throw
std
::
runtime_error
(
"Failed to bind {}:{}."
_format
(
addr
,
port
));
if
(
-
1
==
::
listen
(
listenfd
,
16
))
throw
std
::
runtime_error
(
"listen failed. {}"
_format
(
strerror
(
errno
)));
if
(
!
use_udp
)
{
// UDP don't need to listen.
if
(
-
1
==
::
listen
(
listenfd
,
16
))
throw
std
::
runtime_error
(
"listen failed. {}"
_format
(
strerror
(
errno
)));
}
rlib_defer
([
psaddr
]
{
freeaddrinfo
(
psaddr
);
});
return
listenfd
;
}
static
inline
fd_t
quick_connect
(
const
std
::
string
&
addr
,
uint16_t
port
)
{
static
inline
fd_t
quick_connect
(
const
std
::
string
&
addr
,
uint16_t
port
,
bool
use_udp
=
false
)
{
addrinfo
*
paddr
;
addrinfo
hints
{
0
};
fd_t
sockfd
;
hints
.
ai_family
=
AF_UNSPEC
;
hints
.
ai_socktype
=
SOCK_STREAM
;
if
(
use_udp
)
{
hints
.
ai_socktype
=
SOCK_DGRAM
;
hints
.
ai_protocol
=
IPPROTO_UDP
;
}
else
{
hints
.
ai_socktype
=
SOCK_STREAM
;
hints
.
ai_protocol
=
IPPROTO_TCP
;
}
auto
_
=
getaddrinfo
(
addr
.
c_str
(),
std
::
to_string
(
port
).
c_str
(),
&
hints
,
&
paddr
);
if
(
_
!=
0
)
throw
std
::
runtime_error
(
"getaddrinfo failed. Check network connection to {}:{}; returnval={}, check `man getaddrinfo`'s return value."
_format
(
...
...
This diff is collapsed.
Click to expand it.
sys/unix_handy.hpp
+
22
−
5
View file @
9fd25a21
...
...
@@ -13,17 +13,26 @@
#error rlib/sys/unix_handy.hpp is not for Windows.
#endif
// Deprecated. Use sys/sio.hpp
#if 1+1 == 4
namespace
rlib
{
namespace
impl
{
using
rlib
::
literals
::
operator
""
_format
;
static
inline
fd
unix_quick_listen
(
const
std
::
string
&
addr
,
uint16_t
port
)
{
static
inline
fd
unix_quick_listen
(
const
std
::
string
&
addr
,
uint16_t
port
,
bool
use_udp
=
false
)
{
addrinfo
*
psaddr
;
addrinfo
hints
{
0
};
fd
listenfd
;
hints
.
ai_family
=
AF_UNSPEC
;
hints
.
ai_socktype
=
SOCK_STREAM
;
hints
.
ai_flags
=
AI_PASSIVE
;
/* For wildcard IP address */
if
(
use_udp
)
{
hints
.
ai_socktype
=
SOCK_DGRAM
;
hints
.
ai_protocol
=
IPPROTO_UDP
;
}
else
{
hints
.
ai_socktype
=
SOCK_STREAM
;
hints
.
ai_protocol
=
IPPROTO_TCP
;
}
hints
.
ai_flags
=
AI_PASSIVE
;
/* For listen */
auto
_
=
getaddrinfo
(
addr
.
c_str
(),
std
::
to_string
(
port
).
c_str
(),
&
hints
,
&
psaddr
);
if
(
_
!=
0
)
throw
std
::
runtime_error
(
"Failed to getaddrinfo. returnval={}, check `man getaddrinfo`'s return value."
_format
(
_
));
...
...
@@ -51,13 +60,20 @@ namespace rlib {
return
listenfd
;
}
static
inline
fd
unix_quick_connect
(
const
std
::
string
&
addr
,
uint16_t
port
)
{
static
inline
fd
unix_quick_connect
(
const
std
::
string
&
addr
,
uint16_t
port
,
bool
use_udp
=
false
)
{
addrinfo
*
paddr
;
addrinfo
hints
{
0
};
fd
sockfd
;
hints
.
ai_family
=
AF_UNSPEC
;
hints
.
ai_socktype
=
SOCK_STREAM
;
if
(
use_udp
)
{
hints
.
ai_socktype
=
SOCK_DGRAM
;
hints
.
ai_protocol
=
IPPROTO_UDP
;
}
else
{
hints
.
ai_socktype
=
SOCK_STREAM
;
hints
.
ai_protocol
=
IPPROTO_TCP
;
}
auto
_
=
getaddrinfo
(
addr
.
c_str
(),
std
::
to_string
(
port
).
c_str
(),
&
hints
,
&
paddr
);
if
(
_
!=
0
)
throw
std
::
runtime_error
(
"getaddrinfo failed. Check network connection to {}:{}; returnval={}, check `man getaddrinfo`'s return value."
_format
(
...
...
@@ -88,6 +104,7 @@ namespace rlib {
using
impl
::
unix_quick_connect
;
using
impl
::
unix_quick_listen
;
}
#endif
// Unfinished. I'm not sure if I must implement it.
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment