| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- #ifndef HV_TCP_CLIENT_HPP_
- #define HV_TCP_CLIENT_HPP_
- #include "hsocket.h"
- #include "hssl.h"
- #include "hlog.h"
- #include "EventLoopThread.h"
- #include "Channel.h"
- namespace hv {
- template<class TSocketChannel = SocketChannel>
- class TcpClientEventLoopTmpl {
- public:
- typedef std::shared_ptr<TSocketChannel> TSocketChannelPtr;
- TcpClientEventLoopTmpl(EventLoopPtr loop = NULL) {
- loop_ = loop ? loop : std::make_shared<EventLoop>();
- connect_timeout = HIO_DEFAULT_CONNECT_TIMEOUT;
- tls = false;
- tls_setting = NULL;
- reconn_setting = NULL;
- unpack_setting = NULL;
- }
- virtual ~TcpClientEventLoopTmpl() {
- HV_FREE(tls_setting);
- HV_FREE(reconn_setting);
- HV_FREE(unpack_setting);
- }
- const EventLoopPtr& loop() {
- return loop_;
- }
- //NOTE: By default, not bind local port. If necessary, you can call system api bind() after createsocket().
- //@retval >=0 connfd, <0 error
- int createsocket(int remote_port, const char* remote_host = "127.0.0.1") {
- memset(&remote_addr, 0, sizeof(remote_addr));
- int ret = sockaddr_set_ipport(&remote_addr, remote_host, remote_port);
- if (ret != 0) {
- return -1;
- }
- this->remote_host = remote_host;
- this->remote_port = remote_port;
- return createsocket(&remote_addr.sa);
- }
- int createsocket(struct sockaddr* remote_addr) {
- int connfd = socket(remote_addr->sa_family, SOCK_STREAM, 0);
- // SOCKADDR_PRINT(remote_addr);
- if (connfd < 0) {
- perror("socket");
- return -2;
- }
- hio_t* io = hio_get(loop_->loop(), connfd);
- assert(io != NULL);
- hio_set_peeraddr(io, remote_addr, SOCKADDR_LEN(remote_addr));
- channel.reset(new TSocketChannel(io));
- return connfd;
- }
- // closesocket thread-safe
- void closesocket() {
- if (channel) {
- loop_->runInLoop([this](){
- if (channel) {
- setReconnect(NULL);
- channel->close();
- }
- });
- }
- }
- int startConnect() {
- if (channel == NULL || channel->isClosed()) {
- int connfd = createsocket(&remote_addr.sa);
- if (connfd < 0) {
- hloge("createsocket %s:%d return %d!\n", remote_host.c_str(), remote_port, connfd);
- return connfd;
- }
- }
- if (channel == NULL || channel->status >= SocketChannel::CONNECTING) {
- return -1;
- }
- if (connect_timeout) {
- channel->setConnectTimeout(connect_timeout);
- }
- if (tls) {
- channel->enableSSL();
- if (tls_setting) {
- int ret = channel->newSslCtx(tls_setting);
- if (ret != 0) {
- hloge("new SSL_CTX failed: %d", ret);
- closesocket();
- return ret;
- }
- }
- if (!is_ipaddr(remote_host.c_str())) {
- channel->setHostname(remote_host);
- }
- }
- channel->onconnect = [this]() {
- if (unpack_setting) {
- channel->setUnpack(unpack_setting);
- }
- channel->startRead();
- if (onConnection) {
- onConnection(channel);
- }
- if (reconn_setting) {
- reconn_setting_reset(reconn_setting);
- }
- };
- channel->onread = [this](Buffer* buf) {
- if (onMessage) {
- onMessage(channel, buf);
- }
- };
- channel->onwrite = [this](Buffer* buf) {
- if (onWriteComplete) {
- onWriteComplete(channel, buf);
- }
- };
- channel->onclose = [this]() {
- if (onConnection) {
- onConnection(channel);
- }
- // reconnect
- if (reconn_setting) {
- startReconnect();
- }
- };
- return channel->startConnect();
- }
- int startReconnect() {
- if (!reconn_setting) return -1;
- if (!reconn_setting_can_retry(reconn_setting)) return -2;
- uint32_t delay = reconn_setting_calc_delay(reconn_setting);
- hlogi("reconnect... cnt=%d, delay=%d", reconn_setting->cur_retry_cnt, reconn_setting->cur_delay);
- loop_->setTimeout(delay, [this](TimerID timerID){
- startConnect();
- });
- return 0;
- }
- // start thread-safe
- void start() {
- loop_->runInLoop(std::bind(&TcpClientEventLoopTmpl::startConnect, this));
- }
- bool isConnected() {
- if (channel == NULL) return false;
- return channel->isConnected();
- }
- // send thread-safe
- int send(const void* data, int size) {
- if (!isConnected()) return -1;
- return channel->write(data, size);
- }
- int send(Buffer* buf) {
- return send(buf->data(), buf->size());
- }
- int send(const std::string& str) {
- return send(str.data(), str.size());
- }
- int withTLS(hssl_ctx_opt_t* opt = NULL) {
- tls = true;
- if (opt) {
- if (tls_setting == NULL) {
- HV_ALLOC_SIZEOF(tls_setting);
- }
- opt->endpoint = HSSL_CLIENT;
- *tls_setting = *opt;
- }
- return 0;
- }
- void setConnectTimeout(int ms) {
- connect_timeout = ms;
- }
- void setReconnect(reconn_setting_t* setting) {
- if (setting == NULL) {
- HV_FREE(reconn_setting);
- return;
- }
- if (reconn_setting == NULL) {
- HV_ALLOC_SIZEOF(reconn_setting);
- }
- *reconn_setting = *setting;
- }
- bool isReconnect() {
- return reconn_setting && reconn_setting->cur_retry_cnt > 0;
- }
- void setUnpack(unpack_setting_t* setting) {
- if (setting == NULL) {
- HV_FREE(unpack_setting);
- return;
- }
- if (unpack_setting == NULL) {
- HV_ALLOC_SIZEOF(unpack_setting);
- }
- *unpack_setting = *setting;
- }
- public:
- TSocketChannelPtr channel;
- std::string remote_host;
- int remote_port;
- sockaddr_u remote_addr;
- int connect_timeout;
- bool tls;
- hssl_ctx_opt_t* tls_setting;
- reconn_setting_t* reconn_setting;
- unpack_setting_t* unpack_setting;
- // Callback
- std::function<void(const TSocketChannelPtr&)> onConnection;
- std::function<void(const TSocketChannelPtr&, Buffer*)> onMessage;
- // NOTE: Use Channel::isWriteComplete in onWriteComplete callback to determine whether all data has been written.
- std::function<void(const TSocketChannelPtr&, Buffer*)> onWriteComplete;
- private:
- EventLoopPtr loop_;
- };
- template<class TSocketChannel = SocketChannel>
- class TcpClientTmpl : private EventLoopThread, public TcpClientEventLoopTmpl<TSocketChannel> {
- public:
- TcpClientTmpl(EventLoopPtr loop = NULL)
- : EventLoopThread(loop)
- , TcpClientEventLoopTmpl<TSocketChannel>(EventLoopThread::loop())
- {}
- virtual ~TcpClientTmpl() {
- stop(true);
- }
- const EventLoopPtr& loop() {
- return EventLoopThread::loop();
- }
- // start thread-safe
- void start(bool wait_threads_started = true) {
- if (isRunning()) {
- TcpClientEventLoopTmpl<TSocketChannel>::start();
- } else {
- EventLoopThread::start(wait_threads_started, std::bind(&TcpClientTmpl::startConnect, this));
- }
- }
- // stop thread-safe
- void stop(bool wait_threads_stopped = true) {
- TcpClientEventLoopTmpl<TSocketChannel>::closesocket();
- EventLoopThread::stop(wait_threads_stopped);
- }
- };
- typedef TcpClientTmpl<SocketChannel> TcpClient;
- }
- #endif // HV_TCP_CLIENT_HPP_
|