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
db5e3b13
There was an error fetching the commit references. Please try again later.
Unverified
Commit
db5e3b13
authored
4 years ago
by
Bensong Liu
Browse files
Options
Downloads
Patches
Plain Diff
Plain inbound done
parent
d7f9371a
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
src/protocols/plain.hpp
+15
-16
15 additions, 16 deletions
src/protocols/plain.hpp
src/utils.hpp
+4
-0
4 additions, 0 deletions
src/utils.hpp
with
19 additions
and
16 deletions
src/protocols/plain.hpp
+
15
−
16
View file @
db5e3b13
...
...
@@ -26,46 +26,45 @@ namespace Protocols {
rlib_defer
([
&
]
{
close
(
listenFd
);});
auto
epollFd
=
epoll_create1
(
0
);
if
(
epollFd
==
-
1
)
throw
std
::
runtime_error
(
"Failed to create epoll fd."
);
dynamic_assert
(
epollFd
!=
-
1
,
"epoll_create1 failed"
);
epoll_add_fd
(
epollFd
,
listenFd
);
epoll_add_fd
(
epollFd
,
ipcPipeInboundEnd
);
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.
// ----------------------- Process an event ------------------------------
auto
udpSenderSocket
=
socket
(
AF_INET
,
SOCK_DGRAM
,
0
);
dynamic_assert
(
udpSenderSocket
>
0
,
"socket create failed."
);
std
::
string
msgBuffer
(
DGRAM_BUFFER_SIZE
,
"
\0
"
);
// WARN: If you want to modify this program to work for TCP, 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
);
auto
clientAddr
=
ConnectionMapping
::
parseClientId
(
targetClientId
);
auto
status
=
sendto
(
udpSenderSocket
,
msg
.
data
(),
msg
.
size
(),
clientAddr
.
addr
,
clientAddr
.
len
);
dynamic_assert
(
status
!=
-
1
,
"sendto failed"
);
}
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. "
);
auto
msgLength
=
recvfrom
(
activeFd
,
buffer
,
sizeof
(
buffer
),
0
,
&
clientAddr
.
addr
,
&
clientAddr
.
len
);
dynamic_assert
(
msgLength
!=
-
1
,
"recvfrom failed"
);
nextHop
->
handleMessage
(
msgBuffer
.
substr
(
msgLength
),
ConnectionMapping
::
makeClientId
(
clientAddr
));
}
};
// ----------------------- listener main loop ------------------------------
epoll_event
events
[
MAX_EVENTS
];
rlog
.
info
(
"PlainListener listening InboundPort [{}]:{} ..."
,
listenAddr
,
listenPort
);
while
(
true
)
{
auto
nfds
=
epoll_wait
(
epollFd
,
events
,
MAX_EVENTS
,
-
1
);
if
(
nfds
==
-
1
)
throw
std
::
runtime_error
(
"epoll_wait failed."
);
dynamic_assert
(
nfds
!=
-
1
,
"epoll_wait failed"
);
for
(
auto
cter
=
0
;
cter
<
nfds
;
++
cter
)
{
onEvent
(
events
[
cter
].
data
.
fd
);
}
}
}
private
:
...
...
This diff is collapsed.
Click to expand it.
src/utils.hpp
+
4
−
0
View file @
db5e3b13
...
...
@@ -89,6 +89,10 @@ inline auto mk_tcp_pipe() {
return
std
::
make_pair
(
connfd_cli_side
,
connfd_srv_side
);
}
#define dynamic_assert(expr, msg) do { \
if(!(expr)) { rlog.error("Runtime Assertion Failed: AT " __FILE__ ":" __LINE__ " F(" __func__ "), {}. Errno={}, strerror={}", (msg), errno, strerror(errno)); throw std::runtime_error("dynamic_assert failed. See rlog.error."); } \
} while(false)
#endif
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