internal async Task ReadPayload(Frame frame) { ulong len = frame.FullPayloadLen; if (len > Constant.MaxFrameSize) { throw new InvalidDataException("Payload length exceeds max frame length"); } var payLoad = await ReadBytesAsync(len); if (frame.Mask && payLoad.Length > 0) { Crypto.DecryptPayload(payLoad, frame.MaskKey); } switch (frame.OpCode) { case OpCode.Text: var str = Constant.GetString(payLoad); m_parent.ClientString(this, str); break; case OpCode.Binary: m_parent.ClientData(this, payLoad); break; case OpCode.Close: if (!m_requestClose) { await SendCloseAsync(payLoad); //Echo the payload } ForceClose(); break; case OpCode.Ping: await SendPongAsync(payLoad); break; case OpCode.Pong: m_pingPongTime = DateTime.Now; break; case OpCode.Cont: throw new InvalidDataException("No support for fragmented messages yet"); } }
private async Task <string> ReadLineAsync() { var sb = new StringBuilder(); string lastRead = string.Empty; while (true) { var buf = await ReadBytesAsync(1); string curRead = Constant.GetString(buf); if (curRead != "\r" && curRead != "\n") { sb.Append(curRead); } if (lastRead == "\r" && curRead == "\n") { return(sb.ToString()); } lastRead = curRead; } }