Skip to content

Commit

Permalink
Let kcp internal to slice message
Browse files Browse the repository at this point in the history
  • Loading branch information
halx99 committed Jan 31, 2024
1 parent cdc76a4 commit 5797b8a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
12 changes: 6 additions & 6 deletions tests/echo_client/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ void run_echo_client(const char* ip, int port, const char* protocol)

auto packet = std::move(event->packet());
total_bytes_transferred += static_cast<int>(packet.size());
printf("[latency: %lf(ms), bytes=%zu]", diff / 1000.0, packet.size());
fwrite(packet.data(), packet.size(), 1, stdout);
printf("latency: %lf(ms)\n", diff / 1000.0);
printf("\n");
fflush(stdout);
break;
}
Expand All @@ -50,13 +51,12 @@ void run_echo_client(const char* ip, int port, const char* protocol)
send_timer.async_wait([transport, index, protocol](io_service& service) -> bool {
obstream obs;
obs.write_byte('[');
obs.write_bytes(protocol, strlen(protocol));
obs.write_bytes(protocol, static_cast<int>(strlen(protocol)));
obs.write_bytes("] Hello, ");
auto n = 4096 - obs.length();
obs.fill_bytes(n - 1, '1');
obs.write_byte(',');
printf("Sending %zu bytes ...\n", obs.length());
auto n = 4096 - static_cast<int>(obs.length());
obs.fill_bytes(n, 'a');
service.write(transport, std::move(obs.buffer()));
printf("sent %zu bytes ...\n", obs.length());
s_last_send_time[index] = highp_clock();
return false;
});
Expand Down
13 changes: 7 additions & 6 deletions tests/speed/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ Test detail, please see: https://github.com/yasio/yasio/blob/master/benchmark.md
#endif

// speedtest kcp mtu to max mss of udp (65535 - 20(ip_hdr) - 8(udp_hdr))
#define SPEEDTEST_KCP_MTU 65507
#define SPEEDTEST_UDP_MSS 65507
#define SPEEDTEST_KCP_MTU SPEEDTEST_UDP_MSS
#define SPEEDTEST_KCP_MSS (SPEEDTEST_KCP_MTU - 24)

namespace speedtest
Expand Down Expand Up @@ -91,7 +92,7 @@ static long long s_recv_total_bytes = 0;
static double s_send_speed = 0; // bytes/s
static double s_recv_speed = 0;

static const long long s_kcp_send_interval = 1000; // (ms) in milliseconds
static const long long s_kcp_send_interval = 100; // (us) in milliseconds
static const uint32_t s_kcp_conv = 8633; // can be any, but must same with two endpoint

static const char* proto_name(int myproto)
Expand Down Expand Up @@ -135,9 +136,9 @@ static void print_speed_detail(double interval, double time_elapsed)
#if defined(YASIO_ENABLE_KCP)
void setup_kcp_transfer(transport_handle_t handle)
{
// auto kcp_handle = static_cast<io_transport_kcp*>(handle)->internal_object();
//::ikcp_setmtu(kcp_handle, SPEEDTEST_KCP_MTU);
//::ikcp_wndsize(kcp_handle, 256, 1024);
auto kcp_handle = static_cast<io_transport_kcp*>(handle)->internal_object();
::ikcp_setmtu(kcp_handle, SPEEDTEST_KCP_MTU);
::ikcp_wndsize(kcp_handle, 256, 1024);
}
#endif

Expand Down Expand Up @@ -166,7 +167,7 @@ void kcp_send_repeated(io_service* service, transport_handle_t thandle, obstream
static double time_elapsed = 0;
static double last_print_time = 0;

highp_timer_ptr ignored_ret = service->schedule(std::chrono::milliseconds(s_kcp_send_interval), [=](io_service&) {
highp_timer_ptr ignored_ret = service->schedule(std::chrono::microseconds(s_kcp_send_interval), [=](io_service&) {
s_send_total_bytes += service->write(thandle, obs->buffer());
time_elapsed = (yasio::highp_clock<>() - time_start) / 1000000.0;
s_send_speed = s_send_total_bytes / time_elapsed;
Expand Down
20 changes: 9 additions & 11 deletions yasio/io_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -691,17 +691,15 @@ void io_transport_kcp::set_primitives()
io_transport_udp::set_primitives();
underlaying_write_cb_ = write_cb_;
write_cb_ = [this](const void* data, int len, const ip::endpoint*, int& error) {
int nsent = ::ikcp_send(kcp_, static_cast<const char*>(data), (std::min)(static_cast<int>(kcp_->mss), len));
if (nsent > 0)
{
::ikcp_flush(kcp_);
expire_time_ = 0;
}
else if (nsent == -2)
error = EWOULDBLOCK;
else
error = yasio::errc::invalid_packet;
return nsent;
int nsent = ::ikcp_send(kcp_, static_cast<const char*>(data), len /*(std::min)(static_cast<int>(kcp_->mss), len)*/);
if (nsent > 0)
{
::ikcp_flush(kcp_);
expire_time_ = 0;
}
else
error = EMSGSIZE; // emit message too long
return nsent;
};
}
bool io_transport_kcp::do_write(highp_time_t& wait_duration)
Expand Down

0 comments on commit 5797b8a

Please sign in to comment.