tcpquery.h 1.13 KB
//
//  tcpquery.h
//
//  Created by cairui01 on 20/05/11.
//  Copyright (c) 2020年 baidu. All rights reserved.
//

#ifndef SDT_SRC_CHECKIMPL_TCPQUERY_H_
#define SDT_SRC_CHECKIMPL_TCPQUERY_H_

#include "sdt.h"

#include "mutex.h"
#include "unix_socket.h"
#include "netbuffer.h"
#include "socketselect.h"

class NetCheckTrafficMonitor;

namespace apirequest {
namespace sdt {

class TcpQuery {

    enum TcpStatus {
        kTcpInit = 0,
        kTcpInitErr,
        kTcpConnectErr,
        kTcpConnected,
        kTcpDisConnected,
    };

  public:
    TcpQuery(const char* _ip, uint16_t _port, unsigned int connTimeout, NetCheckTrafficMonitor* trafficMonitor = NULL);
    virtual ~TcpQuery();

  public:
    CheckErrCode tcp_send(const unsigned char* buff, unsigned int unSize, int timeoutMs);
    CheckErrCode tcp_receive(NetBuffer& recvBuf, unsigned int unSize, int timeoutMs);

    void send_break();
    std::string getStatus();
    int getErrorCode();
  private:
    char* ip_;
    uint16_t port_;
    SOCKET sock_;
    SocketBreaker pipe_;
    SocketSelect select_;
    TcpStatus status_;
    int errcode_;
    unsigned int conn_timeout_;
};

}}

#endif