Skip to content

Commit a94269d

Browse files
committed
Explicit retrieve error code when recv < 0.
Optimize lock.
1 parent dd9036c commit a94269d

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

yasio/yasio.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,12 @@ namespace
103103
// error code
104104
enum
105105
{
106-
YERR_OK, // NO ERROR.
106+
YERR_OK, // NO ERROR.
107107
YERR_INVALID_PACKET = -500, // Invalid packet.
108-
YERR_DPL_ILLEGAL_PDU, // Decode pdu length error.
109-
YERR_RESOLV_HOST_FAILED, // Resolve host failed.
110-
YERR_NO_AVAIL_ADDR, // No available address to connect.
111-
YERR_LOCAL_SHUTDOWN, // Local shutdown the connection.
108+
YERR_DPL_ILLEGAL_PDU, // Decode pdu length error.
109+
YERR_RESOLV_HOST_FAILED, // Resolve host failed.
110+
YERR_NO_AVAIL_ADDR, // No available address to connect.
111+
YERR_LOCAL_SHUTDOWN, // Local shutdown the connection.
112112
};
113113

114114
// event mask
@@ -324,12 +324,14 @@ io_transport_base::io_transport_base(io_channel *ctx, std::shared_ptr<xxsocket>
324324
void io_transport_posix::send(std::vector<char> data)
325325
{
326326
a_pdu_ptr pdu(new a_pdu(std::move(data)));
327+
send_mtx_.lock();
327328
send_queue_.push_back(pdu);
329+
send_mtx_.unlock();
328330
}
329331
int io_transport_posix::recv(int &error)
330332
{
331333
int n = socket_->recv_i(buffer_ + offset_, sizeof(buffer_) - offset_);
332-
error = xxsocket::get_last_errno();
334+
error = n < 0 ? xxsocket::get_last_errno() : 0;
333335
return n;
334336
}
335337
bool io_transport_posix::flush(long long &max_wait_duration)
@@ -403,6 +405,7 @@ io_transport_kcp::~io_transport_kcp() { ikcp_release(this->kcp_); }
403405

404406
void io_transport_kcp::send(std::vector<char> data)
405407
{
408+
std::lock_guard<std::recursive_mutex> lck(send_mtx_);
406409
ikcp_send(kcp_, data.data(), static_cast<int>(data.size()));
407410
}
408411
int io_transport_kcp::recv(int &error)
@@ -416,7 +419,7 @@ int io_transport_kcp::recv(int &error)
416419
n = ::ikcp_recv(kcp_, buffer_ + offset_, sizeof(buffer_) - offset_);
417420
else
418421
{ // current, simply regards -1,-3 as error and trigger connection lost event.
419-
n = 0;
422+
n = 0;
420423
error = YERR_INVALID_PACKET;
421424
}
422425
}
@@ -873,7 +876,6 @@ void io_service::register_descriptor(const socket_native_type fd, int flags)
873876
if (maxfdp_ < static_cast<int>(fd) + 1)
874877
maxfdp_ = static_cast<int>(fd) + 1;
875878
}
876-
877879
void io_service::unregister_descriptor(const socket_native_type fd, int flags)
878880
{
879881
if ((flags & YEM_POLLIN) != 0)
@@ -891,12 +893,8 @@ int io_service::write(transport_ptr transport, std::vector<char> data)
891893
{
892894
if (!data.empty())
893895
{
894-
transport->send_mtx_.lock();
895896
transport->send(data);
896-
transport->send_mtx_.unlock();
897-
898897
this->interrupt();
899-
900898
return static_cast<int>(data.size());
901899
}
902900
return 0;

0 commit comments

Comments
 (0)