Skip to content

Commit dd9036c

Browse files
author
halx99
committed
Add -DYASIO_HAVE_KCP to makefile.
1 parent 77cafb2 commit dd9036c

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
config=release
22
TARGET=libyasio.so
33

4-
CXXFLAGS = -c -fPIC -Wall -Wno-unused-result -Wextra -Wundef -Wcast-align -Wcast-qual -Wno-old-style-cast -Wdouble-promotion -std=$(cxxstd)
4+
CXXFLAGS = -c -fPIC -Wall -Wno-unused-result -Wextra -Wundef -Wcast-align -Wcast-qual -Wno-old-style-cast -Wdouble-promotion -DYASIO_HAVE_KCP=1 -std=$(cxxstd)
55
CFLAGS = -c -fPIC
66

77
ifeq ($(CXX),clang++)

yasio/yasio.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ namespace
104104
enum
105105
{
106106
YERR_OK, // NO ERROR.
107-
YERR_SEND_TIMEOUT = -500, // Send timeout.
107+
YERR_INVALID_PACKET = -500, // Invalid packet.
108108
YERR_DPL_ILLEGAL_PDU, // Decode pdu length error.
109109
YERR_RESOLV_HOST_FAILED, // Resolve host failed.
110110
YERR_NO_AVAIL_ADDR, // No available address to connect.
@@ -332,7 +332,7 @@ int io_transport_posix::recv(int &error)
332332
error = xxsocket::get_last_errno();
333333
return n;
334334
}
335-
bool io_transport_posix::update(long long &max_wait_duration)
335+
bool io_transport_posix::flush(long long &max_wait_duration)
336336
{
337337
bool ret = false;
338338
do
@@ -412,14 +412,19 @@ int io_transport_kcp::recv(int &error)
412412
if (n > 0)
413413
{ // ikcp in event always in service thread, so no need to lock, TODO: confirm.
414414
// 0: ok, -1: again, -3: error
415-
::ikcp_input(kcp_, sbuf, n);
416-
n = ::ikcp_recv(kcp_, buffer_ + offset_, sizeof(buffer_) - offset_);
415+
if (0 == ::ikcp_input(kcp_, sbuf, n))
416+
n = ::ikcp_recv(kcp_, buffer_ + offset_, sizeof(buffer_) - offset_);
417+
else
418+
{ // current, simply regards -1,-3 as error and trigger connection lost event.
419+
n = 0;
420+
error = YERR_INVALID_PACKET;
421+
}
417422
}
418423
else
419424
error = xxsocket::get_last_errno();
420425
return n;
421426
}
422-
bool io_transport_kcp::update(long long &max_wait_duration)
427+
bool io_transport_kcp::flush(long long &max_wait_duration)
423428
{
424429
std::lock_guard<std::recursive_mutex> lck(send_mtx_);
425430

@@ -673,7 +678,7 @@ void io_service::perform_transports(fd_set *fds_array, long long &max_wait_durat
673678
for (auto iter = transports_.begin(); iter != transports_.end();)
674679
{
675680
auto transport = *iter;
676-
if (do_read(transport, fds_array, max_wait_duration) && transport->update(max_wait_duration))
681+
if (do_read(transport, fds_array, max_wait_duration) && transport->flush(max_wait_duration))
677682
++iter;
678683
else
679684
{
@@ -1558,8 +1563,6 @@ const char *io_service::strerror(int error)
15581563
{
15591564
case 0:
15601565
return "No error.";
1561-
case YERR_SEND_TIMEOUT:
1562-
return "Send timeout!";
15631566
case YERR_DPL_ILLEGAL_PDU:
15641567
return "Decode frame length failed!";
15651568
case YERR_RESOLV_HOST_FAILED:
@@ -1568,6 +1571,8 @@ const char *io_service::strerror(int error)
15681571
return "No available address!";
15691572
case YERR_LOCAL_SHUTDOWN:
15701573
return "An existing connection was shutdown by local host!";
1574+
case YERR_INVALID_PACKET:
1575+
return "Invalid packet!";
15711576
case -1:
15721577
return "Unknown error!";
15731578
default:

yasio/yasio.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,11 @@ class io_transport_base : public io_base
273273

274274
io_service &get_service() { return ctx_->get_service(); }
275275

276-
virtual void send(std::vector<char> data) = 0;
277-
virtual int recv(int &error) = 0;
278-
virtual bool update(long long &max_wait_duration) = 0;
276+
virtual void send(std::vector<char> data) = 0;
277+
virtual int recv(int &error) = 0;
278+
279+
// Try flush pending packet
280+
virtual bool flush(long long &max_wait_duration) = 0;
279281

280282
protected:
281283
io_transport_base(io_channel *ctx, std::shared_ptr<xxsocket> sock);
@@ -304,7 +306,7 @@ class io_transport_posix : public io_transport_base
304306
{}
305307
void send(std::vector<char> data) override;
306308
int recv(int &error) override;
307-
bool update(long long &max_wait_duration) override;
309+
bool flush(long long &max_wait_duration) override;
308310
std::deque<a_pdu_ptr> send_queue_;
309311
};
310312

@@ -316,7 +318,7 @@ class io_transport_kcp : public io_transport_base
316318
~io_transport_kcp();
317319
void send(std::vector<char> data) override;
318320
int recv(int &error) override;
319-
bool update(long long &max_wait_duration) override;
321+
bool flush(long long &max_wait_duration) override;
320322
ikcpcb *kcp_;
321323
};
322324
#endif

0 commit comments

Comments
 (0)