public void SetUp() { // create new server & received list for each test serverReceived = new List<byte[]>(); server = new KcpServer( (connectionId) => {}, (connectionId, message) => { byte[] copy = new byte[message.Count]; Buffer.BlockCopy(message.Array, message.Offset, copy, 0, message.Count); serverReceived.Add(copy); }, (connectionId) => {}, NoDelay, Interval, 0, true, serverSendWindowSize, serverReceiveWindowSize ); server.NoDelay = NoDelay; server.Interval = Interval; // create new client & received list for each test clientReceived = new List<byte[]>(); client = new KcpClient( () => {}, (message) => { byte[] copy = new byte[message.Count]; Buffer.BlockCopy(message.Array, message.Offset, copy, 0, message.Count); clientReceived.Add(copy); }, () => {} ); }
public void SetUp() { // logging Log.Info = Debug.Log; Log.Warning = Debug.LogWarning; Log.Error = Debug.LogError; // create new server & received list for each test serverReceived = new List <byte[]>(); server = new KcpServer( (connectionId) => {}, ServerOnData, (connectionId) => {}, NoDelay, Interval, 0, true, serverSendWindowSize, serverReceiveWindowSize ); server.NoDelay = NoDelay; server.Interval = Interval; // create new client & received list for each test clientReceived = new List <byte[]>(); client = new KcpClient( () => {}, ClientOnData, () => {} ); }
// virtual so that we can overwrite for where-allocation nonalloc tests protected virtual void CreateClient() { client = new KcpClient( () => {}, ClientOnData, () => {} ); }
public KcpHostClient(ChannelConfig channelConfig, IPEndPoint remoteAddress) { if (client == null) { client = KcpClient.Instance; client.init(channelConfig); } this._ukcp = client.connect(remoteAddress, channelConfig, this); }
public async Task ClSendLoop(KcpClient cl) { var clid = clident++; await Task.Delay(200); while (true) { cl.Send(Encoding.Default.GetBytes($"Msg From:{clid},Time{DateTimeOffset.Now}").AsSpan()); Console.WriteLine($"客户端{clid}消息发送,长度:{41}"); await Task.Delay(2000); } }
public static void Init(bool isServer = false, bool isClient = false, bool isSimulated = false) { IsServer = isServer ? true : IsServer; IsClient = isClient ? true : IsClient; IsSimulated = isSimulated ? true : IsSimulated; if (isSimulated) { if (NetLogFilter.logInfo) { Debug.Log("Transport Layer Initialized: Simulation"); } return; } if (IsServer && server == null) { // server server = new KcpServer( (connectionId) => OnServerConnected.Invoke(connectionId), (connectionId, message) => OnServerDataReceived.Invoke(connectionId, message, (int)UDPChannels.Reliable), (connectionId) => OnServerDisconnected.Invoke(connectionId), NoDelay, Interval, FastResend, CongestionWindow, SendWindowSize, ReceiveWindowSize ); if (NetLogFilter.logInfo) { Debug.Log("Transport Layer Initialized: Server"); } } if (IsClient && client == null) { // client client = new KcpClient( () => OnClientConnected.Invoke(), (message) => OnClientDataReceived.Invoke(message, (int)UDPChannels.Reliable), () => OnClientDisconnected.Invoke() ); if (NetLogFilter.logInfo) { Debug.Log("Transport Layer Initialized: Client"); } } }
public UDPTest() { var svrPort = "127.0.0.1:7100"; var kcpSvr = KcpSvr.Start(svrPort); kcpSvr.OnKcpReceive += KcpSvr_OnKcpReceive; kcpSvr.OnNew += KcpSvr_OnNew; cl1 = KcpClient.Create(svrPort); cl1.OnKcpReceive += Kcpcl_OnKcpReceive; //cl1.Run(); //ClSendLoop(cl1); var t2 = ClSendLoop(cl1); Task.WaitAll(t2); }
public KcpHostClient(ChannelConfig channelConfig, IPEndPoint remoteAddress) { if (client == null) { lock (lockObj) { if (client == null) { client = new KcpClient(); client.init(channelConfig); } } } this._ukcp = client.connect(remoteAddress, channelConfig, this); }
public KcpSocket(string url, bool enableUdp, int timeoutSec = 60) { this._url = url.ToLower().Replace("wss://", "").Replace("ws://", ""); var str = url.Split(':'); this._port = Convert.ToInt32(str.Length > 1 ? url.Split(':') [1] : "443"); this._url = str[0]; var address = Dns.GetHostAddresses(this._url) [0]; this._endPoint = new IPEndPoint(address, this._port); this._protocolType = enableUdp ? ProtocolType.Udp : ProtocolType.Tcp; this._udpClient = new KcpClient(this._port); _recvTimeoutSec = timeoutSec; _udpRcvBuf = new byte[(Kcp.Kcp.IkcpMtuDef + Kcp.Kcp.IkcpOverhead) * 3]; _kcpRcvBuf = new byte[(Kcp.Kcp.IkcpMtuDef + Kcp.Kcp.IkcpOverhead) * 3]; _rcvQueue = new Queue <byte[]> (64); _forGround = new Queue <byte[]> (64); _errors = new Queue <Exception> (8); }
void Awake() { // TODO simplify after converting Mirror Transport events to Action client = new KcpClient( () => OnClientConnected.Invoke(), (message) => OnClientDataReceived.Invoke(message), () => OnClientDisconnected.Invoke() ); // TODO simplify after converting Mirror Transport events to Action server = new KcpServer( (connectionId) => OnServerConnected.Invoke(connectionId), (connectionId, message) => OnServerDataReceived.Invoke(connectionId, message), (connectionId) => OnServerDisconnected.Invoke(connectionId), NoDelay, Interval, FastResend, CongestionWindow, SendWindowSize, ReceiveWindowSize ); Debug.Log("KcpTransport initialized!"); }
public static void start() { ChannelConfig channelConfig = new ChannelConfig(); channelConfig.initNodelay(true, 40, 2, true); channelConfig.Sndwnd = 512; channelConfig.Rcvwnd = 512; channelConfig.Mtu = 512; channelConfig.FecDataShardCount = 3; channelConfig.FecParityShardCount = 1; channelConfig.AckNoDelay = true; channelConfig.Crc32Check = true; channelConfig.Conv = 55; //channelConfig.setTimeoutMillis(10000); KcpClient kcpClient = new KcpClient(); kcpClient.init(channelConfig); KcpRttExampleClient kcpClientRttExample = new KcpRttExampleClient(); //kcpClient.connect(new InetSocketAddress("127.0.0.1",20003),channelConfig,kcpClientRttExample); EndPoint remoteAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 20003); _ukcp = kcpClient.connect(remoteAddress, channelConfig, kcpClientRttExample); try { kcpClientRttExample.init(); } catch (Exception e) { Console.WriteLine(e); throw; } }