This commit is contained in:
aozhiwei 2024-05-01 19:37:02 +08:00
parent 60818be648
commit 2f1dca6913
2 changed files with 24 additions and 6 deletions

16
third_party/a8/a8/constant.cc vendored Normal file
View File

@ -0,0 +1,16 @@
module;
export module a8m.constant;
export namespace a8m {
constexpr int INVALID_FD() { return -1; }
constexpr int INVALID_SOCKET() { return -1; }
constexpr int INVALID_SOCKET_HANDLE() { return 0; }
constexpr int TIMER_EXEC_EVENT() { return 1; }
constexpr int TIMER_DELETE_EVENT() { return 2; }
constexpr int TIMER_DESTORY_EVENT() { return 3; }
constexpr int TIMER_USER_EVENT() { return 66; }
constexpr float A8_PI() { return 3.1415926f; }
}

View File

@ -15,6 +15,8 @@
const int MAX_RECV_BUFFERSIZE = 1024 * 64; const int MAX_RECV_BUFFERSIZE = 1024 * 64;
import a8m.constant;
namespace a8 namespace a8
{ {
TcpClient::TcpClient(const std::string& remote_ip, int remote_port) TcpClient::TcpClient(const std::string& remote_ip, int remote_port)
@ -53,7 +55,7 @@ namespace a8
bool TcpClient::IsActive() bool TcpClient::IsActive()
{ {
return socket_ != a8::INVALID_SOCKET; return socket_ != a8m::INVALID_SOCKET();
} }
bool TcpClient::Connected() bool TcpClient::Connected()
@ -103,7 +105,7 @@ namespace a8
bool TcpClient::ActiveStart() bool TcpClient::ActiveStart()
{ {
socket_ = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); socket_ = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (INVALID_SOCKET == socket_) { if (a8m::INVALID_SOCKET() == socket_) {
if (on_error) { if (on_error) {
on_error(this, errno); on_error(this, errno);
} }
@ -119,7 +121,7 @@ namespace a8
on_error(this, errno); on_error(this, errno);
} }
::close(socket_); ::close(socket_);
socket_ = INVALID_SOCKET; socket_ = a8m::INVALID_SOCKET();
return false; return false;
} }
//set nodelay //set nodelay
@ -145,7 +147,7 @@ namespace a8
void TcpClient::ActiveStop() void TcpClient::ActiveStop()
{ {
connected_ = false; connected_ = false;
if (socket_ != INVALID_SOCKET) { if (socket_ != a8m::INVALID_SOCKET()) {
shutdown(socket_, 2); shutdown(socket_, 2);
::close(socket_); ::close(socket_);
} }
@ -155,7 +157,7 @@ namespace a8
delete worker_thread_; delete worker_thread_;
worker_thread_ = nullptr; worker_thread_ = nullptr;
} }
socket_ = INVALID_SOCKET; socket_ = a8m::INVALID_SOCKET();
} }
void TcpClient::WorkerThreadProc() void TcpClient::WorkerThreadProc()
@ -193,7 +195,7 @@ namespace a8
senderthread->join(); senderthread->join();
delete senderthread; delete senderthread;
senderthread = nullptr; senderthread = nullptr;
socket_ = INVALID_SOCKET; socket_ = a8m::INVALID_SOCKET();
} }
void TcpClient::SenderThreadProc() void TcpClient::SenderThreadProc()