public void Send(byte[] datas, IPEndPoint endPoint) { if (endPoint == null) { return; } _currentEndPoint = endPoint; _kcp?.Send(datas); }
public bool Send(byte[] bytes, int len) { if (!IsActived()) { Debuger.LogWarning("Session已经不活跃了!"); return(false); } return(m_Kcp.Send(bytes, len) > 0); }
public bool Send(FSPFrameData frame) { if (!IsActived()) { Debuger.LogWarning("Session已经不活跃了!"); return(false); } tempSendData.frame = frame; int len = ProtoBuffUtility.Serialize(tempSendData, sendBufferTemp); return(kcp.Send(sendBufferTemp, len) == 0); }
public bool SendFSP(int clientFrameID, int cmd, int[] args) { if (isRunning) { FSPMessage msg = tempSendData.msg; msg.cmd = cmd; msg.clientFrameID = clientFrameID; msg.args = args; int length = ProtoBuffUtility.Serialize(tempSendData, sendBufferTemp); kcp.Send(sendBufferTemp, length); return(length > 0); } return(false); }
public void Send(UInt32 id, UInt32 protocol, Int32 cmd, byte[] msg) { //UnityEngine.Main.Debug("msg:" + msg.Length); uint body_key = Convert.ToUInt32(CRC32Cls.GetCRC32(msg)); byte[] id_bytes = BitConverter.GetBytes(id); //id byte[] protocol_bytes = BitConverter.GetBytes(protocol); //protocol byte[] cmd_bytes = BitConverter.GetBytes(cmd); //cmd byte[] body_key_bytes = BitConverter.GetBytes(body_key); //cmd UInt32 total_length = (UInt32)(id_bytes.Length + protocol_bytes.Length + cmd_bytes.Length + body_key_bytes.Length + msg.Length + 4); //UnityEngine.Main.Debug("send3333"); //消息体结构:消息体长度+消息体 byte[] data = new byte[total_length]; BitConverter.GetBytes(total_length).CopyTo(data, 0); id_bytes.CopyTo(data, 4); protocol_bytes.CopyTo(data, 8); cmd_bytes.CopyTo(data, 12); body_key_bytes.CopyTo(data, 16); msg.CopyTo(data, 20); //UnityEngine.Main.Debug("send44444"); //socket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(send_back), null); string str = ""; for (int i = 0; i < data.Length; i++) { str += data[i].ToString(); } //UnityEngine.Main.Debug("发送数据:" + str); //UnityEngine.Main.Debug("数据长度:" + data.Length); m_Kcp.Send(data); _send = true; m_NeedUpdateFlag = true; }
/// <summary> /// 异步发送 /// </summary> /// <param name="data">发送数据</param> /// <returns> </returns> public bool Send(byte[] data) { if (kcp == null || !connected) { return(false); } if (kcp.WaitSnd() < MaxWaitSnd) { kcp.Send(data); return(true); } return(false); }
/// <summary> /// 发送数据。发送时,默认在头部加上4字节的key /// </summary> /// <param name="buf"></param> public void Send(byte[] buf) { byte[] newbuf = new byte[buf.Length + 4]; //把key附上,服务端合法性检测用 KCP.ikcp_encode32u(newbuf, 0, (uint)m_Key); Array.Copy(buf, 0, newbuf, 4, buf.Length); m_LastSendTimestamp = UnityEngine.Time.time; m_Kcp.Send(newbuf); m_NeedUpdateFlag = true; var e = Event; if (e != null) { e(UdpClientEvents.Send, buf); } }
public bool SendFSP(int clientFrameId, int cmd, int[] args) { Debuger.Log("clientFrameId:{0}, cmd:{1}, args:{2}", clientFrameId, cmd, args.ToListString()); if (m_IsRunning) { FSPMessage msg = m_TempSendData.msgs[0]; msg.cmd = cmd; msg.clientFrameId = clientFrameId; msg.args = args; m_TempSendData.msgs.Clear(); m_TempSendData.msgs.Add(msg); int len = PBSerializer.NSerialize(m_TempSendData, m_SendBufferTemp); m_Kcp.Send(m_SendBufferTemp, len); return(len > 0); } return(false); }
public int Send(byte[] data, int index, int length) { if (mSocket == null) { return(-1); } if (mKCP.WaitSnd >= mKCP.SndWnd) { return(0); } mNextUpdateTime = 0; var n = mKCP.Send(data, index, length); if (mKCP.WaitSnd >= mKCP.SndWnd || !WriteDelay) { mKCP.Flush(false); } return(n); }
/// <summary> /// 异步发送 /// </summary> /// <param name="data">发送数据</param> /// <returns>异步等待接口</returns> public IAwait Send(byte[] data) { var code = kcp.Send(data); if (code < 0) { Exception ex; if (!kcpExceptionDict.TryGetValue(code, out ex)) { ex = kcpExceptionDict[int.MinValue]; } Trigger(SocketEvents.Error, ex); } else { Trigger(SocketEvents.Sent, data); } return(new InternalRuntime { Result = code >= 0, IsDone = true }); }
public override int Send(byte[] data) { if (mSocket == null) { return(-1); } if (mKCP == null) { return(-1); } if (mKCP.WaitSnd >= mKCP.SndWnd) { return(0); } mNextUpdateTime = 0; var n = mKCP.Send(data); if (mKCP.WaitSnd >= mKCP.SndWnd) { mKCP.Flush(true); } return(n); }
protected override void Send(ByteBuffer buffer) { _kcp.Send(buffer.data, buffer.readableBytes); }
override public bool Send(byte[] pBuffer, int pSize) { return(kcpObject.Send(pBuffer, pSize) >= 0); }
public bool AddSend(byte[] buff, int size) { return(kcpObject.Send(buff, size) >= 0); }
// 测试用例 static void KCPTest(int mode) { // 创建模拟网络:丢包率10%,Rtt 60ms~125ms vnet = new LatencySimulator(10, 60, 125); // 创建两个端点的 kcp对象,第一个参数 conv是会话编号,同一个会话需要相同 // 最后一个是 user参数,用来传递标识 var kcp1 = new KCP(0x11223344, 1); var kcp2 = new KCP(0x11223344, 2); // 设置kcp的下层输出,这里为 udp_output,模拟udp网络输出函数 kcp1.SetOutput(udp_output); kcp2.SetOutput(udp_output); UInt32 current = Utils.iclock(); UInt32 slap = current + 20; UInt32 index = 0; UInt32 next = 0; Int64 sumrtt = 0; int count = 0; int maxrtt = 0; // 配置窗口大小:平均延迟200ms,每20ms发送一个包, // 而考虑到丢包重发,设置最大收发窗口为128 kcp1.WndSize(128, 128); kcp2.WndSize(128, 128); if (mode == 0) // 默认模式 { kcp1.NoDelay(0, 10, 0, 0); kcp2.NoDelay(0, 10, 0, 0); } else if (mode == 1) // 普通模式,关闭流控等 { kcp1.NoDelay(0, 10, 0, 1); kcp2.NoDelay(0, 10, 0, 1); } else // 启动快速模式 { // 第1个参数 nodelay-启用以后若干常规加速将启动 // 第2个参数 interval为内部处理时钟,默认设置为 10ms // 第3个参数 resend为快速重传指标,设置为2 // 第4个参数 为是否禁用常规流控,这里禁止 kcp1.NoDelay(1, 10, 2, 1); kcp2.NoDelay(1, 10, 2, 1); kcp1.SetMinRTO(10); kcp1.SetFastResend(1); } var buffer = new byte[2000]; int hr = 0; UInt32 ts1 = Utils.iclock(); while (true) { Thread.Sleep(1); current = Utils.iclock(); kcp1.Update(current); kcp2.Update(current); // 每隔 20ms,kcp1发送数据 for (; current >= slap; slap += 20) { KCP.ikcp_encode32u(buffer, 0, index++); KCP.ikcp_encode32u(buffer, 4, current); // 发送上层协议包 kcp1.Send(buffer, 0, 8); } // 处理虚拟网络:检测是否有udp包从p1->p2 while (true) { hr = vnet.Recv(1, buffer, 2000); if (hr < 0) { break; } // 如果 p2收到udp,则作为下层协议输入到kcp2 hr = kcp2.Input(buffer, 0, hr); Debug.Assert(hr >= 0); } // 处理虚拟网络:检测是否有udp包从p2->p1 while (true) { hr = vnet.Recv(0, buffer, 2000); if (hr < 0) { break; } // 如果 p1收到udp,则作为下层协议输入到kcp1 hr = kcp1.Input(buffer, 0, hr); Debug.Assert(hr >= 0); } // kcp2接收到任何包都返回回去 while (true) { hr = kcp2.Recv(buffer, 0, 10); if (hr < 0) { break; } // 如果收到包就回射 hr = kcp2.Send(buffer, 0, hr); Debug.Assert(hr >= 0); } // kcp1收到kcp2的回射数据 while (true) { hr = kcp1.Recv(buffer, 0, 10); if (hr < 0) // 没有收到包就退出 { break; } int offset = 0; UInt32 sn = KCP.ikcp_decode32u(buffer, ref offset); UInt32 ts = KCP.ikcp_decode32u(buffer, ref offset); UInt32 rtt = current - ts; if (sn != next) { // 如果收到的包不连续 Console.WriteLine(String.Format("ERROR sn {0}<->{1}", count, next)); return; } next++; sumrtt += rtt; count++; if (rtt > maxrtt) { maxrtt = (int)rtt; } Console.WriteLine(String.Format("[RECV] mode={0} sn={1} rtt={2}", mode, sn, rtt)); } if (next > 1000) { break; } } ts1 = Utils.iclock() - ts1; var names = new string[3] { "default", "normal", "fast" }; Console.WriteLine("{0} mode result ({1}ms):", names[mode], ts1); Console.WriteLine("avgrtt={0} maxrtt={1} tx={2}", sumrtt / count, maxrtt, vnet.tx1); Console.WriteLine("Press any key to next..."); Console.Read(); }
//应用层--->KCP private void SendPacketData(byte[] data) { kcp.Send(data); }
// Test case static void KCPTest(int mode) { // Create an analog network: 10% packet loss rate,Rtt 60ms~125ms vnet = new LatencySimulator(10, 60, 125); // Create a kcp object with two endpoints, the first parameter conv is the session number, the same session needs to be the same // The last one is the user parameter, which is used to pass the identifier. var kcp1 = new KCP(0x11223344, 1); var kcp2 = new KCP(0x11223344, 2); // Set the lower output of kcp, here udp_output, simulate udp network output function kcp1.SetOutput(udp_output); kcp2.SetOutput(udp_output); UInt32 current = Utils.iclock(); UInt32 slap = current + 20; UInt32 index = 0; UInt32 next = 0; Int64 sumrtt = 0; int count = 0; int maxrtt = 0; // Configuration window size: average delay 200ms, send a packet every 20ms, // Considering the packet loss retransmission, set the maximum receiving and sending window to 128. kcp1.WndSize(128, 128); kcp2.WndSize(128, 128); if (mode == 0) //Default mode { kcp1.NoDelay(0, 10, 0, 0); kcp2.NoDelay(0, 10, 0, 0); } else if (mode == 1) // Normal mode, turn off flow control, etc. { kcp1.NoDelay(0, 10, 0, 1); kcp2.NoDelay(0, 10, 0, 1); } else //Start fast mode { // 1st parameter nodelay-enabled after some regular acceleration will start // The second parameter interval is the internal processing clock. The default setting is 10ms. // The third parameter resend is the fast retransmission indicator, set to 2 // The fourth parameter is whether to disable regular flow control, this is forbidden here. kcp1.NoDelay(1, 10, 2, 1); kcp2.NoDelay(1, 10, 2, 1); kcp1.SetMinRTO(10); kcp1.SetFastResend(1); } var buffer = new byte[2000]; int hr = 0; UInt32 ts1 = Utils.iclock(); while (true) { Thread.Sleep(1); current = Utils.iclock(); kcp1.Update(current); kcp2.Update(current); // Kcp1 sends data every 20ms for (; current >= slap; slap += 20) { KCP.ikcp_encode32u(buffer, 0, index++); KCP.ikcp_encode32u(buffer, 4, current); // Send the upper layer protocol packet kcp1.Send(buffer, 0, 8); } //Handling virtual networks: detecting if there are udp packets from p1->p2 while (true) { hr = vnet.Recv(1, buffer, 2000); if (hr < 0) { break; } //If p2 receives udp, it is input to kcp2 as the underlying protocol. hr = kcp2.Input(buffer, 0, hr); Debug.Assert(hr >= 0); } // Handling virtual networks: detecting if there are udp packets from p2->p1 while (true) { hr = vnet.Recv(0, buffer, 2000); if (hr < 0) { break; } // If p1 receives udp, it is input to kcp1 as the underlying protocol. hr = kcp1.Input(buffer, 0, hr); Debug.Assert(hr >= 0); } // Kcp2 receives any package and returns it. while (true) { hr = kcp2.Recv(buffer, 0, 10); if (hr < 0) { break; } //If you receive the package, it will return hr = kcp2.Send(buffer, 0, hr); Debug.Assert(hr >= 0); } // Kcp1 receives the echo data of kcp2 while (true) { hr = kcp1.Recv(buffer, 0, 10); if (hr < 0) // Exit without receiving the package { break; } int offset = 0; UInt32 sn = KCP.ikcp_decode32u(buffer, ref offset); UInt32 ts = KCP.ikcp_decode32u(buffer, ref offset); UInt32 rtt = current - ts; if (sn != next) { // If the received packet is not continuous Console.WriteLine(String.Format("ERROR sn {0}<->{1}", count, next)); return; } next++; sumrtt += rtt; count++; if (rtt > maxrtt) { maxrtt = (int)rtt; } Console.WriteLine(String.Format("[RECV] mode={0} sn={1} rtt={2}", mode, sn, rtt)); } if (next > 1000) { break; } } ts1 = Utils.iclock() - ts1; var names = new string[3] { "default", "normal", "fast" }; Console.WriteLine("{0} mode result ({1}ms):", names[mode], ts1); Console.WriteLine("avgrtt={0} maxrtt={1} tx={2}", sumrtt / count, maxrtt, vnet.tx1); Console.WriteLine("Press any key to next..."); Console.Read(); }
public void Send(byte[] buff) { _kcp.Send(buff); }
public void Send(byte[] data) { mKcp.Send(data); mNeedUpdateFlag = true; }