Browse Source

fix kcp conv

ithewei 4 years ago
parent
commit
6c4380a97a
5 changed files with 25 additions and 8 deletions
  1. 13 5
      event/hevent.c
  2. 1 1
      event/hevent.h
  3. 1 1
      event/hloop.h
  4. 1 0
      event/rudp.h
  5. 9 1
      examples/nc.c

+ 13 - 5
event/hevent.c

@@ -677,7 +677,7 @@ int hio_set_kcp(hio_t* io, kcp_setting_t* setting) {
     return 0;
 }
 
-kcp_t* hio_get_kcp(hio_t* io) {
+kcp_t* hio_get_kcp(hio_t* io, uint32_t conv) {
     rudp_entry_t* rudp = hio_get_rudp(io);
     assert(rudp != NULL);
     kcp_t* kcp = &rudp->kcp;
@@ -687,9 +687,10 @@ kcp_t* hio_get_kcp(hio_t* io) {
     }
     kcp_setting_t* setting = io->kcp_setting;
     assert(io->kcp_setting != NULL);
-    kcp->ikcp = ikcp_create(setting->conv, rudp);
-    // printf("ikcp_create ikcp=%p\n", kcp->ikcp);
+    kcp->ikcp = ikcp_create(conv, rudp);
+    printf("ikcp_create conv=%u ikcp=%p\n", conv, kcp->ikcp);
     kcp->ikcp->output = __kcp_output;
+    kcp->conv = conv;
     if (setting->interval > 0) {
         ikcp_nodelay(kcp->ikcp, setting->nodelay, setting->interval, setting->fastresend, setting->nocwnd);
     }
@@ -712,7 +713,8 @@ kcp_t* hio_get_kcp(hio_t* io) {
 }
 
 int hio_write_kcp(hio_t* io, const void* buf, size_t len) {
-    kcp_t* kcp = hio_get_kcp(io);
+    IUINT32 conv = io->kcp_setting ? io->kcp_setting->conv : 0;
+    kcp_t* kcp = hio_get_kcp(io, conv);
     int nsend = ikcp_send(kcp->ikcp, (const char*)buf, len);
     // printf("ikcp_send len=%d nsend=%d\n", (int)len, nsend);
     if (nsend < 0) {
@@ -724,7 +726,13 @@ int hio_write_kcp(hio_t* io, const void* buf, size_t len) {
 }
 
 int hio_read_kcp (hio_t* io, void* buf, int readbytes) {
-    kcp_t* kcp = hio_get_kcp(io);
+    IUINT32 conv = ikcp_getconv(buf);
+    kcp_t* kcp = hio_get_kcp(io, conv);
+    if (kcp->conv != conv) {
+        hloge("recv invalid kcp packet!");
+        hio_close_rudp(io, io->peeraddr);
+        return -1;
+    }
     // printf("ikcp_input len=%d\n", readbytes);
     ikcp_input(kcp->ikcp, (const char*)buf, readbytes);
     if (kcp->readbuf.base == NULL || kcp->readbuf.len == 0) {

+ 1 - 1
event/hevent.h

@@ -201,7 +201,7 @@ void hio_free_readbuf(hio_t* io);
 #if WITH_RUDP
 rudp_entry_t* hio_get_rudp(hio_t* io);
 #if WITH_KCP
-kcp_t*  hio_get_kcp(hio_t* io);
+kcp_t*  hio_get_kcp(hio_t* io, uint32_t conv);
 int     hio_write_kcp(hio_t* io, const void* buf, size_t len);
 int     hio_read_kcp (hio_t* io, void* buf, int readbytes);
 #endif

+ 1 - 1
event/hloop.h

@@ -521,7 +521,7 @@ HV_EXPORT int hio_close_rudp(hio_t* io, struct sockaddr* peeraddr DEFAULT(NULL))
 #if WITH_KCP
 typedef struct kcp_setting_s {
     // ikcp_create(conv, ...)
-    int conv;
+    unsigned int conv;
     // ikcp_nodelay(kcp, nodelay, interval, fastresend, nocwnd)
     int nodelay;
     int interval;

+ 1 - 0
event/rudp.h

@@ -17,6 +17,7 @@
 
 typedef struct kcp_s {
     ikcpcb*         ikcp;
+    uint32_t        conv;
     htimer_t*       update_timer;
     hbuf_t          readbuf;
 } kcp_t;

+ 9 - 1
examples/nc.c

@@ -208,7 +208,15 @@ Examples: nc 127.0.0.1 80\n\
         // udp
         sockio = hloop_create_udp_client(loop, host, port);
 #if TEST_KCP
-        hio_set_kcp(sockio, NULL);
+        static kcp_setting_t s_kcp_setting;
+        memset(&s_kcp_setting, 0, sizeof(kcp_setting_t));
+        s_kcp_setting.conv = 123456;
+        // fast mode
+        s_kcp_setting.nodelay = 1;
+        s_kcp_setting.interval = 10;
+        s_kcp_setting.fastresend = 2;
+        s_kcp_setting.nocwnd = 1;
+        hio_set_kcp(sockio, &s_kcp_setting);
 #endif
         hio_read(sockio);
     }