| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- #ifndef HV_UDP_CLIENT_HPP_
- #define HV_UDP_CLIENT_HPP_
- #include "hsocket.h"
- #include "EventLoopThread.h"
- #include "Callback.h"
- #include "Channel.h"
- namespace hv {
- class UdpClient {
- public:
- UdpClient() {
- }
- virtual ~UdpClient() {
- }
- const EventLoopPtr& loop() {
- return loop_thread.loop();
- }
- //@retval >=0 sockfd, <0 error
- int createsocket(int port, const char* host = "127.0.0.1") {
- hio_t* io = hloop_create_udp_client(loop_thread.hloop(), host, port);
- if (io == NULL) return -1;
- channel.reset(new SocketChannel(io));
- return channel->fd();
- }
- void start(bool wait_threads_started = true) {
- loop_thread.start(wait_threads_started,
- [this]() {
- assert(channel != NULL);
- channel->onread = [this](Buffer* buf) {
- if (onMessage) {
- onMessage(channel, buf);
- }
- };
- channel->onwrite = [this](Buffer* buf) {
- if (onWriteComplete) {
- onWriteComplete(channel, buf);
- }
- };
- channel->startRead();
- return 0;
- }
- );
- }
- void stop(bool wait_threads_stopped = true) {
- loop_thread.stop(wait_threads_stopped);
- }
- int sendto(const void* data, int size) {
- if (channel == NULL) return 0;
- return channel->write(data, size);
- }
- int sendto(Buffer* buf) {
- if (channel == NULL) return 0;
- return channel->write(buf);
- }
- int sendto(const std::string& str) {
- if (channel == NULL) return 0;
- return channel->write(str);
- }
- public:
- SocketChannelPtr channel;
- // Callback
- MessageCallback onMessage;
- WriteCompleteCallback onWriteComplete;
- private:
- EventLoopThread loop_thread;
- };
- }
- #endif // HV_UDP_CLIENT_HPP_
|