/// <summary> /// 当接收到远程端的数据时,将触发此方法 /// 此方法用于处理和分析收到的数据 /// 如果得到一个数据包,将触发OnRecvComplete方法 /// [注]这里只需处理一个数据包的流程 /// </summary> /// <param name="client">客户端</param> /// <param name="recvBuilder">接收到的历史数据</param> /// <returns>如果不够一个数据包,则请返回null</returns> protected override Hybi13Packet OnReceive(SocketAsync <Hybi13Packet> client, ByteBuilder recvBuilder) { if (client.IsConnected == false) { return(null); } // 获取处理接收的数据的容器 var resultBuilder = client.TagBag.ResultBuilder as ByteBuilder; // 说明不是第一次握手请求 if (resultBuilder != null) { return(RequestPacket.GetPacket(recvBuilder, resultBuilder)); } // 设置ResultBuilder client.TagBag.ResultBuilder = new ByteBuilder(); // 处理握手请求 var request = HandshakeRequest.Parse(recvBuilder.ToArrayThenClear()); if (request == null || this.CheckHandshake(client, request) == false) { this.CloseClient(client, CloseStatus.ProtocolError); } else { var packet = new ResponsePacket(); packet.SetHandshake(request.ToHandshake()); client.Send(packet); } return(null); }
private void sendMsg(long peerId, IExtensible msg) { var frame = PBSerializer.Serialize(msg); var builder = new ByteBuilder(); builder.Add(frame.Length, Endians.Little); builder.Add(frame); svr.SendBytes(peerId, builder.ToArrayThenClear()); }
public void Send(IExtensible msg) { Debug.Assert(peerId != 0); var frame = PBSerializer.Serialize(msg); var builder = new ByteBuilder(); builder.Add(frame.Length, Endians.Little); builder.Add(frame); cli.SendBytes(peerId, builder.ToArrayThenClear()); }
/// <summary> /// 解析收到的数据 /// </summary> /// <param name="builder">数据</param> /// <returns></returns> public static PolicyPacket GetPacket(ByteBuilder builder) { if (builder.Length == 0) { return(null); } var bytes = builder.ToArrayThenClear(); return(new PolicyPacket { Bytes = bytes }); }
public void Send(string msg) { if (peerId == 0) { this.Warn("not conect yet"); return; } var frame = Encoding.ASCII.GetBytes(msg); var builder = new ByteBuilder(); builder.Add(frame); cli.SendBytes(peerId, builder.ToArrayThenClear()); }
/// <summary> /// 解析请求的数据 /// 返回请求数据包 /// </summary> /// <param name="builder">所有收到的数据</param> /// <param name="resultBuilder">用于保存数理后的数据</param> /// <returns></returns> public unsafe static Hybi13Packet GetPacket(ByteBuilder builder, ByteBuilder resultBuilder) { if (builder.Length < 2) { return(null); } var isFinal = (builder.ToByte(0) & 128) != 0; var reservedBits = builder.ToByte(0) & 112; var frameType = (FrameTypes)(builder.ToByte(0) & 15); var isMasked = (builder.ToByte(1) & 128) != 0; var length = builder.ToByte(1) & 127; if (isMasked == false || Enum.IsDefined(typeof(FrameTypes), frameType) == false || reservedBits != 0) { return(null); } // 计算数据长度和mask索引 var maskIndex = 2; if (length == 127) { if (builder.Length < maskIndex + 8) { return(null); } length = (int)builder.ToUInt64(maskIndex, Endians.Big); maskIndex = maskIndex + 8; } else if (length == 126) { if (builder.Length < maskIndex + 2) { return(null); } length = (int)builder.ToUInt16(maskIndex, Endians.Big); maskIndex = maskIndex + 2; } // 检查数据长度 if (builder.Length < maskIndex + Math.Max(4, length)) { return(null); } // 数据内容的索引位置 var dataIndex = maskIndex + 4; if (length > 0) { fixed(byte *pdata = &builder.Source[dataIndex], pmask = &builder.Source[maskIndex]) { for (var i = 0; i < length; i++) { *(pdata + i) = (byte)(*(pdata + i) ^ *(pmask + i % 4)); } } } // 将数据放到resultBuilder resultBuilder.Add(builder.Source, dataIndex, length); // 清除已分析的数据 builder.Remove(dataIndex + length); // 检查数据是否传输完成 if (isFinal == true && frameType != FrameTypes.Continuation) { var bytes = resultBuilder.ToArrayThenClear(); return(new RequestPacket(frameType, bytes)); } else { return(null); } }