Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
U
udp-forwarder-ex
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
udp-forwarder-ex
Commits
d7f9371a
There was an error fetching the commit references. Please try again later.
Unverified
Commit
d7f9371a
authored
4 years ago
by
Bensong Liu
Browse files
Options
Downloads
Patches
Plain Diff
more practicial framework now
parent
b1ce0d7a
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/forwarder.hpp
+1
-9
1 addition, 9 deletions
src/forwarder.hpp
src/protocols/plain.hpp
+28
-3
28 additions, 3 deletions
src/protocols/plain.hpp
src/utils.hpp
+27
-0
27 additions, 0 deletions
src/utils.hpp
with
56 additions
and
12 deletions
src/forwarder.hpp
+
1
−
9
View file @
d7f9371a
#ifndef UDP_FORWARDER_DYN_FORWARDER_HPP_
#define UDP_FORWARDER_DYN_FORWARDER_HPP_
#include
<unordered_map>
#include
<rlib/sys/sio.hpp>
#include
"utils.hpp"
...
...
@@ -10,14 +9,7 @@
using
std
::
string
;
struct
ConnectionMapping
{
std
::
unordered_map
<
string
,
fd_t
>
client2server
;
std
::
unordered_multimap
<
fd_t
,
string
>
server2client
;
static
string
clientInfoAsKey
(
string
ip
,
uint16_t
port
)
{
// Also works for ipv6. We just want to eliminate duplication, rather than make it easy to read.
return
ip
+
'@'
+
port
;
}
};
class
Forwarder
{
public:
...
...
This diff is collapsed.
Click to expand it.
src/protocols/plain.hpp
+
28
−
3
View file @
d7f9371a
...
...
@@ -34,11 +34,36 @@ namespace Protocols {
epoll_event
events
[
MAX_EVENTS
];
char
buffer
[
DGRAM_BUFFER_SIZE
];
// WARN: If you want to modify this program to work for both TCP and UDP, PLEASE use rlib::sockIO::recv instead of fixed buffer.
auto
onEvent
=
[
&
](
auto
activeFd
)
{
if
(
activeFd
==
ipcPipeInboundEnd
)
{
// Outbound gave me a message to forward! Send it.
auto
targetClientId
=
rlib
::
sockIO
::
recv_msg
(
activeFd
);
auto
msg
=
rlib
::
sockIO
::
recv_msg
(
activeFd
);
auto
[
clientAddr
,
clientPort
]
=
ConnectionMapping
::
parseClientId
(
targetClientId
);
}
else
if
(
activeFd
==
listenFd
)
{
SockAddr
clientAddr
;
auto
ret
=
recvfrom
(
activeFd
,
buffer
,
sizeof
(
buffer
),
0
,
&
clientAddr
.
addr
,
&
clientAddr
.
len
);
if
(
ret
==
-
1
)
throw
std
::
runtime_error
(
"recvfrom failed. "
);
}
};
rlog
.
info
(
"PlainListener listening InboundPort [{}]:{} ..."
,
listenAddr
,
listenPort
);
while
(
true
)
{
// ...
// epoll
auto
nfds
=
epoll_wait
(
epollFd
,
events
,
MAX_EVENTS
,
-
1
);
if
(
nfds
==
-
1
)
throw
std
::
runtime_error
(
"epoll_wait failed."
);
for
(
auto
cter
=
0
;
cter
<
nfds
;
++
cter
)
{
onEvent
(
events
[
cter
].
data
.
fd
);
}
}
}
...
...
This diff is collapsed.
Click to expand it.
src/utils.hpp
+
27
−
0
View file @
d7f9371a
...
...
@@ -4,6 +4,7 @@
#include
<rlib/sys/os.hpp>
#include
<rlib/sys/sio.hpp>
#include
<thread>
#include
<unordered_map>
#include
"common.hpp"
#if RLIB_OS_ID == OS_LINUX
...
...
@@ -12,6 +13,32 @@
#include
<wepoll.h>
#endif
struct
SockAddr
{
union
{
sockaddr_storage
addr_storage
;
sockaddr
addr
;
sockaddr_in
in4
;
sockaddr_in6
in6
;
};
int
len
;
};
struct
ConnectionMapping
{
std
::
unordered_map
<
string
,
fd_t
>
client2server
;
std
::
unordered_multimap
<
fd_t
,
string
>
server2client
;
static
string
makeClientId
(
const
SockAddr
&
osStrust
)
const
{
// ClientId is a binary string.
string
result
(
sizeof
(
osStruct
),
'\0'
);
std
::
memcpy
(
result
.
data
(),
&
osStruct
,
sizeof
(
osStrust
));
return
result
;
}
static
void
parseClientId
(
const
string
&
clientId
,
SockAddr
&
output
)
const
{
static_assert
(
sizeof
(
output
)
==
sizeof
(
SockAddr
),
"error: programming error detected."
);
if
(
clientId
.
size
()
!=
sizeof
(
output
))
throw
std
::
invalid_argument
(
"parseClientId, invalid input binary string length."
);
std
::
memcpy
(
&
output
,
clientId
.
data
(),
sizeof
(
output
));
}
};
inline
void
epoll_add_fd
(
fd_t
epollFd
,
fd_t
fd
)
{
epoll_event
event
{
...
...
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