xtaci/kcp-go

多客户端连接连接至同一个服务器的时候,如果有一个客户端断开了,服务端就会停止广播,即使客户端重新连接,服务端也收不到任何的连接信息,需要服务端重启才能解决此问题

FlashFeiFei opened this issue · 6 comments

go:kcp-go作为服务端
java:kcp-base作为客户端

go作为游戏数据转发

服务端代码
`package main

import (
"github.com/xtaci/kcp-go/v5"
room2 "kcp/demo3/room"
"log"
"time"
)

func main() {

listener, err := kcp.ListenWithOptions("127.0.0.1:12345", nil, 10, 3)
listener.SetDeadline(time.Now().Add(1 * time.Second))
if err != nil {
	log.Fatal(err)
}

room := room2.NewGameRoom()
go room.Run()
for {
	s, err := listener.AcceptKCP()
	if err != nil {
		continue
	}
	room.JoinChan() <- s
}

}
`

客户端代码
`package com.laughingZhu.api;

import com.backblaze.erasure.FecAdapt;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.Unpooled;
import kcp.ChannelConfig;
import kcp.KcpClient;
import kcp.KcpListener;
import kcp.Ukcp;

import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;

/**

  • 与go版本兼容的客户端

  • Created by JinMiao

  • 2019/11/29.
    */
    public class Kcp4GoExampleClient implements KcpListener {

    private final ByteBuf data = Unpooled.buffer(200);

    private final long startTime = System.currentTimeMillis();
    private volatile int count;

    public static void main(String[] args) {
    ChannelConfig channelConfig = new ChannelConfig();
    channelConfig.nodelay(true, 10, 2, true);
    channelConfig.setSndwnd(1024);
    channelConfig.setRcvwnd(1024);
    channelConfig.setMtu(1400);
    channelConfig.setFecAdapt(new FecAdapt(10, 3));
    channelConfig.setAckNoDelay(false);
    channelConfig.setTimeoutMillis(10000);

     //禁用参数
     channelConfig.setCrc32Check(false);
     channelConfig.setAckMaskSize(0);
    
    
     KcpClient kcpClient = new KcpClient();
     kcpClient.init(channelConfig);
    
    
     Kcp4GoExampleClient kcpGoExampleClient = new Kcp4GoExampleClient();
     Ukcp ukcp = kcpClient.connect(new InetSocketAddress("127.0.0.1", 12345), channelConfig, kcpGoExampleClient);
    

    }

    @OverRide
    public void onConnected(Ukcp ukcp) {

     //链接成功发送数据
     String msg = "hello!!!!!2222222222";
     byte[] bytes = msg.getBytes();
     ByteBuf byteBuf = ByteBufAllocator.DEFAULT.ioBuffer(bytes.length);
     byteBuf.writeBytes(bytes);
     ukcp.write(byteBuf);
    

    }

    @OverRide
    public void handleReceive(ByteBuf byteBuf, Ukcp ukcp) {
    //读取数据
    byte[] bytes = new byte[byteBuf.readableBytes()];
    byteBuf.getBytes(byteBuf.readerIndex(),bytes);
    System.out.println("收到消息: "+new String(bytes,StandardCharsets.UTF_8));

     //写入数据
    

// buf.writeShort(++count);
// buf.writeInt((int) (System.currentTimeMillis() - startTime));
++count;
String msg = "--发送数据java" + count;
ByteBuf buf = Unpooled.buffer(msg.length());
buf.writeCharSequence(msg, StandardCharsets.UTF_8);
ukcp.write(buf);
}

@Override
public void handleException(Throwable ex, Ukcp ukcp) {
    ex.printStackTrace();
}

@Override
public void handleClose(Ukcp ukcp) {
    System.out.println("链接关闭: " + ukcp);
    ukcp.close();

}

}`

先运行服务端,然后运行客户端,一切正常
微信图片_20240413005401

这时候,断开客户端重新运行客户端之后,数据会发送几秒,之后就不在发送数据了,卡主了;重新开启一个客户端,debug服务端发现s, err := listener.AcceptKCP()没有链接进来
微信图片_20240413005859

求个c++的例子