This commit is contained in:
azw 2023-05-06 14:51:10 +00:00
parent 77fb8a70b2
commit c37b0c22d9
2 changed files with 11 additions and 6 deletions

View File

@ -18,8 +18,10 @@ const int MAX_RECV_BUFFERSIZE = 1024 * 64;
namespace a8
{
TcpClient::TcpClient()
TcpClient::TcpClient(const std::string& remote_ip, int remote_port)
{
remote_address_ = remote_ip;
remote_port_ = remote_port;
send_buffer_mutex_ = new std::mutex();
send_cond_mutex_ = new std::mutex();
send_cond_ = new std::condition_variable();
@ -111,8 +113,8 @@ namespace a8
sockaddr_in sa;
memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = inet_addr(remote_address.c_str());
sa.sin_port = htons(remote_port);
sa.sin_addr.s_addr = inet_addr(remote_address_.c_str());
sa.sin_port = htons(remote_port_);
if (::connect(socket_, (sockaddr*)&sa, sizeof(sa)) < 0) {
if (on_error) {
on_error(this, errno);

View File

@ -9,11 +9,11 @@ namespace a8
std::function<void (a8::TcpClient*)> on_connect;
std::function<void (a8::TcpClient*)> on_disconnect;
std::function<void (a8::TcpClient*, char*, unsigned int)> on_socketread;
std::string remote_address;
int remote_port = 0;
TcpClient();
TcpClient(const std::string& remote_ip, int remote_port);
virtual ~TcpClient();
const std::string& GetRemoteAddress() { return remote_address_; }
int GetRemotePort() { return remote_port_; }
void Open();
void Close();
@ -22,6 +22,9 @@ namespace a8
void SendBuff(const char* buff, unsigned int bufflen);
private:
std::string remote_address_;
int remote_port_ = 0;
volatile int socket_ = a8::INVALID_SOCKET;
volatile bool connected_ = false;
volatile bool sender_thread_shutdown_ = false;