func (this *Tunneld) pollServerReadyRead(ch *Channel) { // TODO 使用内存池 rbuf := make([]byte, rdbufsz) debug.Println("copying server to client:", ch.chidsrv, ch.chidsrv, ch.conv) // 使用kcp的mtu设置了,这里不再需要限制读取的包大小 for { n, err := ch.conn.Read(rbuf) if err != nil { errl.Println(err, ch.chidsrv, ch.chidsrv, ch.conv) break } // 控制kcp.WaitSnd()的大小 for { if uint32(ch.kcp.WaitSnd()) < ch.kcp.snd_wnd*5 { // this.processServerReadyRead(ch, rbuf, n) sendbuf := gopp.BytesDup(rbuf[:n]) this.serverReadyReadChan <- ServerReadyReadEvent{ch, sendbuf, n} break } else { time.Sleep(3 * time.Millisecond) } } } // 连接结束 debug.Println("connection closed, cleaning up...:", ch.chidcli, ch.chidsrv, ch.conv) ch.server_socket_close = true this.serverCloseChan <- ServerCloseEvent{ch} appevt.Trigger("connact", -1) }
func (this *Tunnelc) pollClientReadyRead(ch *Channel) { // 使用kcp的mtu设置了,这里不再需要限制读取的包大小 rbuf := make([]byte, rdbufsz) for { n, err := ch.conn.Read(rbuf) if err != nil { info.Println("chan read:", err, ch.chidcli, ch.chidsrv, ch.conv) break } // 应用层控制kcp.WaitSnd()的大小 for { if uint32(ch.kcp.WaitSnd()) < ch.kcp.snd_wnd*5 { sendbuf := gopp.BytesDup(rbuf[:n]) // this.processClientReadyRead(ch, rbuf, n) this.clientReadyReadChan <- ClientReadyReadEvent{ch, sendbuf, n} break } else { time.Sleep(3 * time.Millisecond) } } } // 连接结束 debug.Println("connection closed, cleaning up...:", ch.chidcli, ch.chidsrv, ch.conv) ch.client_socket_close = true this.clientCloseChan <- ClientCloseEvent{ch} appevt.Trigger("connact", -1) }