-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add UDP group #480
Open
x1244
wants to merge
1
commit into
ithewei:master
Choose a base branch
from
x1244:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+256
−2
Open
Add UDP group #480
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
#ifndef HV_UDP_GROUP_HPP_ | ||
#define HV_UDP_GROUP_HPP_ | ||
#include "hsocket.h" | ||
|
||
#include "EventLoopThreadPool.h" | ||
#include "Channel.h" | ||
|
||
namespace hv { | ||
|
||
template<class TSocketChannel = SocketChannel> | ||
class UdpGroupEventLoopTmpl { | ||
public: | ||
typedef std::shared_ptr<TSocketChannel> TSocketChannelPtr; | ||
|
||
UdpGroupEventLoopTmpl(EventLoopPtr loop = NULL) { | ||
loop_ = loop ? loop : std::make_shared<EventLoop>(); | ||
port = 0; | ||
#if WITH_KCP | ||
kcp_setting = NULL; | ||
#endif | ||
} | ||
|
||
virtual ~UdpGroupEventLoopTmpl() { | ||
#if WITH_KCP | ||
HV_FREE(kcp_setting); | ||
#endif | ||
} | ||
|
||
const EventLoopPtr& loop() { | ||
return loop_; | ||
} | ||
|
||
//use createsocket() for server OR use createsocketRemote() for client | ||
//@retval >=0 bindfd, <0 error | ||
int createsocket(int port, const char* host = "0.0.0.0") { | ||
hio_t* io = hloop_create_udp_server(loop_->loop(), host, port); | ||
if (io == NULL) return -1; | ||
this->host = host; | ||
this->port = port; | ||
channel = std::make_shared<TSocketChannel>(io); | ||
return channel->fd(); | ||
} | ||
|
||
// join group | ||
int joinGroup(const char* g){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 看了下,就添加了joinGroup、leaveGroup两个方法,其它代码都是拷贝自UdpServer,不如直接加在UdpServer里,没必要单独搞个UdpGroup类了吧 |
||
if(channel == NULL || channel->isClosed()){ | ||
return -1; | ||
} | ||
return udp_joingroupv4(channel->fd(), g, host.c_str()); | ||
} | ||
// leave group | ||
int leaveGroup(const char* g){ | ||
if(channel == NULL || channel->isClosed()){ | ||
return -1; | ||
} | ||
return udp_leavegroupv4(channel->fd(), g, host.c_str()); | ||
} | ||
|
||
// closesocket thread-safe | ||
void closesocket() { | ||
if (channel) { | ||
channel->close(true); | ||
} | ||
} | ||
|
||
int startRecv() { | ||
if (channel == NULL || channel->isClosed()) { | ||
return -1; | ||
} | ||
channel->onread = [this](Buffer* buf) { | ||
if (onMessage) { | ||
onMessage(channel, buf); | ||
} | ||
}; | ||
#if WITH_KCP | ||
if (kcp_setting) { | ||
hio_set_kcp(channel->io(), kcp_setting); | ||
} | ||
#endif | ||
return channel->startRead(); | ||
} | ||
|
||
int stopRecv() { | ||
if (channel == NULL) return -1; | ||
return channel->stopRead(); | ||
} | ||
|
||
// start thread-safe | ||
void start() { | ||
loop_->runInLoop(std::bind(&UdpGroupEventLoopTmpl::startRecv, this)); | ||
} | ||
|
||
#if WITH_KCP | ||
void setKcp(kcp_setting_t* setting) { | ||
if (setting == NULL) { | ||
HV_FREE(kcp_setting); | ||
return; | ||
} | ||
if (kcp_setting == NULL) { | ||
HV_ALLOC_SIZEOF(kcp_setting); | ||
} | ||
*kcp_setting = *setting; | ||
} | ||
#endif | ||
|
||
public: | ||
std::string host; | ||
int port; | ||
TSocketChannelPtr channel; | ||
#if WITH_KCP | ||
kcp_setting_t* kcp_setting; | ||
#endif | ||
// Callback | ||
std::function<void(const TSocketChannelPtr&, Buffer*)> onMessage; | ||
private: | ||
EventLoopPtr loop_; | ||
}; | ||
|
||
template<class TSocketChannel = SocketChannel> | ||
class UdpGroupTmpl : private EventLoopThread, public UdpGroupEventLoopTmpl<TSocketChannel> { | ||
public: | ||
UdpGroupTmpl(EventLoopPtr loop = NULL) | ||
: EventLoopThread(loop) | ||
, UdpGroupEventLoopTmpl<TSocketChannel>(EventLoopThread::loop()) | ||
, is_loop_owner(loop == NULL) | ||
{} | ||
virtual ~UdpGroupTmpl() { | ||
stop(true); | ||
} | ||
|
||
const EventLoopPtr& loop() { | ||
return EventLoopThread::loop(); | ||
} | ||
|
||
// join group | ||
int joinGroup(const char* g){ | ||
return UdpGroupEventLoopTmpl<TSocketChannel>::joinGroup(g); | ||
} | ||
|
||
// leave group | ||
int leaveGroup(const char* g){ | ||
return UdpGroupEventLoopTmpl<TSocketChannel>::leaveGroup(g); | ||
} | ||
|
||
// start thread-safe | ||
void start(bool wait_threads_started = true) { | ||
if (isRunning()) { | ||
UdpGroupEventLoopTmpl<TSocketChannel>::start(); | ||
} else { | ||
EventLoopThread::start(wait_threads_started, std::bind(&UdpGroupTmpl::startRecv, this)); | ||
} | ||
} | ||
|
||
// stop thread-safe | ||
void stop(bool wait_threads_stopped = true) { | ||
UdpGroupEventLoopTmpl<TSocketChannel>::closesocket(); | ||
if (is_loop_owner) { | ||
EventLoopThread::stop(wait_threads_stopped); | ||
} | ||
} | ||
|
||
private: | ||
bool is_loop_owner; | ||
}; | ||
|
||
typedef UdpGroupTmpl<SocketChannel> UdpGroup; | ||
|
||
} | ||
|
||
#endif // HV_UDP_GROUP_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* UdpGroupDest_test.cpp | ||
* | ||
* @build make evpp | ||
* @client bin/UdpClient_test 9997 230.1.1.25 | ||
* @server bin/UdpGroupDest_test 9997 230.1.1.25 | ||
* | ||
*/ | ||
|
||
#include <iostream> | ||
|
||
#include "UdpGroup.h" | ||
|
||
using namespace hv; | ||
|
||
int main(int argc, char* argv[]) { | ||
if (argc < 2) { | ||
printf("Usage: %s port\n", argv[0]); | ||
return -10; | ||
} | ||
int port = atoi(argv[1]); | ||
const char* group = "230.1.1.25"; | ||
if (argc > 2) { | ||
group = argv[2]; | ||
} | ||
|
||
UdpGroup ug; | ||
int bindfd = ug.createsocket(port); | ||
if (bindfd < 0) { | ||
return -20; | ||
} | ||
int rst = ug.joinGroup(group); | ||
printf("udp group bind on port %d, bindfd=%d rst=%d ...\n", port, bindfd, rst); | ||
ug.onMessage = [](const SocketChannelPtr& channel, Buffer* buf) { | ||
// echo | ||
printf(" >>> %.*s\n", (int)buf->size(), (char*)buf->data()); | ||
}; | ||
ug.start(); | ||
|
||
std::string str; | ||
while (std::getline(std::cin, str)) { | ||
if (str == "close") { | ||
ug.closesocket(); | ||
break; | ||
} else if (str == "leave") { | ||
int c = ug.leaveGroup(group); | ||
printf("leave group rst=%d\n", c); | ||
} else if (str == "join") { | ||
int c = ug.joinGroup(group); | ||
printf("join group rst=%d\n", c); | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
return 0; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ULONG是windows平台下特有的类型定义,其它平台没有,CI已经报错了