| 1 |
#include "hkcp.h" |
| 2 |
|
| 3 |
#if WITH_KCP |
| 4 |
|
| 5 |
#include "hevent.h" |
| 6 |
#include "hlog.h" |
| 7 |
#include "hthread.h" |
| 8 |
|
| 9 |
static kcp_setting_t s_kcp_setting; |
| 10 |
|
| 11 |
static int __kcp_output(const char* buf, int len, ikcpcb* ikcp, void* userdata) { |
| 12 |
|
| 13 |
rudp_entry_t* rudp = (rudp_entry_t*)userdata; |
| 14 |
assert(rudp != NULL && rudp->io != NULL); |
| 15 |
int nsend = sendto(rudp->io->fd, buf, len, 0, &rudp->addr.sa, SOCKADDR_LEN(&rudp->addr)); |
| 16 |
|
| 17 |
return nsend; |
| 18 |
} |
| 19 |
|
| 20 |
static void __kcp_update_timer_cb(htimer_t* timer) { |
| 21 |
rudp_entry_t* rudp = (rudp_entry_t*)timer->privdata; |
| 22 |
assert(rudp != NULL && rudp->io != NULL && rudp->kcp.ikcp != NULL); |
| 23 |
ikcp_update(rudp->kcp.ikcp, (IUINT32)(rudp->io->loop->cur_hrtime / 1000)); |
| 24 |
} |
| 25 |
|
| 26 |
void kcp_release(kcp_t* kcp) { |
| 27 |
if (kcp->ikcp == NULL) return; |
| 28 |
if (kcp->update_timer) { |
| 29 |
htimer_del(kcp->update_timer); |
| 30 |
kcp->update_timer = NULL; |
| 31 |
} |
| 32 |
HV_FREE(kcp->readbuf.base); |
| 33 |
kcp->readbuf.len = 0; |
| 34 |
|
| 35 |
ikcp_release(kcp->ikcp); |
| 36 |
kcp->ikcp = NULL; |
| 37 |
} |
| 38 |
|
| 39 |
int hio_set_kcp(hio_t* io, kcp_setting_t* setting) { |
| 40 |
io->io_type = HIO_TYPE_KCP; |
| 41 |
io->kcp_setting = setting; |
| 42 |
return 0; |
| 43 |
} |
| 44 |
|
| 45 |
kcp_t* hio_get_kcp(hio_t* io, uint32_t conv) { |
| 46 |
rudp_entry_t* rudp = hio_get_rudp(io); |
| 47 |
assert(rudp != NULL); |
| 48 |
kcp_t* kcp = &rudp->kcp; |
| 49 |
if (kcp->ikcp != NULL) return kcp; |
| 50 |
if (io->kcp_setting == NULL) { |
| 51 |
io->kcp_setting = &s_kcp_setting; |
| 52 |
} |
| 53 |
kcp_setting_t* setting = io->kcp_setting; |
| 54 |
kcp->ikcp = ikcp_create(conv, rudp); |
| 55 |
|
| 56 |
kcp->ikcp->output = __kcp_output; |
| 57 |
kcp->conv = conv; |
| 58 |
if (setting->interval > 0) { |
| 59 |
ikcp_nodelay(kcp->ikcp, setting->nodelay, setting->interval, setting->fastresend, setting->nocwnd); |
| 60 |
} |
| 61 |
if (setting->sndwnd > 0 && setting->rcvwnd > 0) { |
| 62 |
ikcp_wndsize(kcp->ikcp, setting->sndwnd, setting->rcvwnd); |
| 63 |
} |
| 64 |
if (setting->mtu > 0) { |
| 65 |
ikcp_setmtu(kcp->ikcp, setting->mtu); |
| 66 |
} |
| 67 |
if (kcp->update_timer == NULL) { |
| 68 |
int update_interval = setting->update_interval; |
| 69 |
if (update_interval == 0) { |
| 70 |
update_interval = DEFAULT_KCP_UPDATE_INTERVAL; |
| 71 |
} |
| 72 |
kcp->update_timer = htimer_add(io->loop, __kcp_update_timer_cb, update_interval, INFINITE); |
| 73 |
kcp->update_timer->privdata = rudp; |
| 74 |
} |
| 75 |
|
| 76 |
return kcp; |
| 77 |
} |
| 78 |
|
| 79 |
static void hio_write_kcp_event_cb(hevent_t* ev) { |
| 80 |
hio_t* io = (hio_t*)ev->userdata; |
| 81 |
hbuf_t* buf = (hbuf_t*)ev->privdata; |
| 82 |
|
| 83 |
hio_write_kcp(io, buf->base, buf->len); |
| 84 |
|
| 85 |
HV_FREE(buf); |
| 86 |
} |
| 87 |
|
| 88 |
static int hio_write_kcp_async(hio_t* io, const void* data, size_t len) { |
| 89 |
hbuf_t* buf = NULL; |
| 90 |
HV_ALLOC(buf, sizeof(hbuf_t) + len); |
| 91 |
buf->base = (char*)buf + sizeof(hbuf_t); |
| 92 |
buf->len = len; |
| 93 |
memcpy(buf->base, data, len); |
| 94 |
|
| 95 |
hevent_t ev; |
| 96 |
memset(&ev, 0, sizeof(ev)); |
| 97 |
ev.cb = hio_write_kcp_event_cb; |
| 98 |
ev.userdata = io; |
| 99 |
ev.privdata = buf; |
| 100 |
hloop_post_event(io->loop, &ev); |
| 101 |
return len; |
| 102 |
} |
| 103 |
|
| 104 |
int hio_write_kcp(hio_t* io, const void* buf, size_t len) { |
| 105 |
if (hv_gettid() != io->loop->tid) { |
| 106 |
return hio_write_kcp_async(io, buf, len); |
| 107 |
} |
| 108 |
IUINT32 conv = io->kcp_setting ? io->kcp_setting->conv : 0; |
| 109 |
kcp_t* kcp = hio_get_kcp(io, conv); |
| 110 |
|
| 111 |
int nsend = ikcp_send(kcp->ikcp, (const char*)buf, len); |
| 112 |
|
| 113 |
if (nsend < 0) { |
| 114 |
hloge("ikcp_send error: %d", nsend); |
| 115 |
return nsend; |
| 116 |
} |
| 117 |
ikcp_update(kcp->ikcp, (IUINT32)io->loop->cur_hrtime / 1000); |
| 118 |
return len; |
| 119 |
} |
| 120 |
|
| 121 |
int hio_read_kcp (hio_t* io, void* buf, int readbytes) { |
| 122 |
IUINT32 conv = ikcp_getconv(buf); |
| 123 |
kcp_t* kcp = hio_get_kcp(io, conv); |
| 124 |
|
| 125 |
if (kcp->conv != conv) { |
| 126 |
hloge("recv invalid kcp packet!"); |
| 127 |
hio_close_rudp(io, io->peeraddr); |
| 128 |
return -1; |
| 129 |
} |
| 130 |
|
| 131 |
int ret = ikcp_input(kcp->ikcp, (const char*)buf, readbytes); |
| 132 |
|
| 133 |
if (ret != 0) { |
| 134 |
return 0; |
| 135 |
} |
| 136 |
if (kcp->readbuf.base == NULL || kcp->readbuf.len == 0) { |
| 137 |
kcp->readbuf.len = DEFAULT_KCP_READ_BUFSIZE; |
| 138 |
HV_ALLOC(kcp->readbuf.base, kcp->readbuf.len); |
| 139 |
} |
| 140 |
while (1) { |
| 141 |
int nrecv = ikcp_recv(kcp->ikcp, kcp->readbuf.base, kcp->readbuf.len); |
| 142 |
|
| 143 |
if (nrecv < 0) break; |
| 144 |
hio_read_cb(io, kcp->readbuf.base, nrecv); |
| 145 |
ret += nrecv; |
| 146 |
} |
| 147 |
return ret; |
| 148 |
} |
| 149 |
|
| 150 |
#endif |