private bool CheckPrefixHeadComplated(WSDataToken dataToken, byte[] buffer, ref int offset) { if (dataToken.ByteArrayForPrefix == null || dataToken.ByteArrayForPrefix.Length != PreByteLength) { dataToken.ByteArrayForPrefix = new byte[PreByteLength]; } if (PreByteLength - dataToken.PrefixBytesDone > buffer.Length - offset) { Buffer.BlockCopy(buffer, offset, dataToken.ByteArrayForPrefix, dataToken.PrefixBytesDone, buffer.Length - offset); dataToken.PrefixBytesDone += buffer.Length - offset; return(false); } int count = dataToken.ByteArrayForPrefix.Length - dataToken.PrefixBytesDone; if (count > 0) { Buffer.BlockCopy(buffer, offset, dataToken.ByteArrayForPrefix, dataToken.PrefixBytesDone, count); dataToken.PrefixBytesDone += count; offset += count; } if (dataToken.HeadFrame == null) { //build message head dataToken.HeadFrame = MessageHeadFrame.Parse(dataToken.ByteArrayForPrefix); } return(true); }
public override bool TryReadMeaage(WSDataToken dataToken, byte[] buffer, out List <DataMessage> messageList) { if (CheckVersion((WebSocket)dataToken.Socket)) { return(base.TryReadMeaage(dataToken, buffer, out messageList)); } messageList = new List <DataMessage>(); int offset = 0; do { //receive buffer is complated if (!CheckPrefixHeadComplated(dataToken, buffer, ref offset) || !dataToken.HeadFrame.CheckRSV || !CheckPayloadHeadComplated(dataToken, buffer, ref offset) || !CheckPayloadDataComplated(dataToken, buffer, ref offset)) { return(false); } byte[] data = dataToken.HeadFrame.HasMask ? DecodeMask(dataToken.ByteArrayForMessage, dataToken.ByteArrayMask, 0, dataToken.MessageLength) : dataToken.ByteArrayForMessage; if (!dataToken.HeadFrame.FIN) { dataToken.DataFrames.Add(new DataSegmentFrame() { Head = dataToken.HeadFrame, Data = new ArraySegment <byte>(data) }); } else { //frame complated sbyte opCode; if (dataToken.DataFrames.Count > 0) { dataToken.DataFrames.Add(new DataSegmentFrame() { Head = dataToken.HeadFrame, Data = new ArraySegment <byte>(data) }); opCode = dataToken.DataFrames[0].Head.OpCode; data = CombineDataFrames(dataToken.DataFrames); } else { opCode = dataToken.HeadFrame.OpCode; } messageList.Add(new DataMessage() { Data = data, Opcode = opCode }); dataToken.DataFrames.Clear(); } dataToken.Reset(); } while (offset < buffer.Length); return(true); }
public override bool TryReadMeaage(WSDataToken dataToken, byte[] buffer, out List <DataMessage> messageList) { messageList = new List <DataMessage>(); int offset = 0; do { //check close flag if (CheckCloseFlag(buffer, ref offset)) { messageList.Add(new DataMessage() { Data = buffer, Opcode = Opcode.Close }); dataToken.Reset(); return(true); } //receive buffer is complated if (!CheckPrefixHeadComplated(dataToken, buffer, ref offset) || !CheckDataComplated(dataToken, buffer, ref offset)) { return(false); } byte[] data = dataToken.ByteArrayForMessage; if (data != null) { messageList.Add(new DataMessage() { Data = data, Opcode = Opcode.Text }); } dataToken.Reset(); } while (offset < buffer.Length); return(true); }
public WebSocketListener(WebSocketListenerSetting setting) { if (setting == null) { throw new ArgumentNullException(nameof(setting)); } this.setting = setting; if (setting.Version >= 13) { handshakeProcessor = new Rfc6455HandshakeProcessor(this, setting.Encoding); messageProcessor = new Rfc6455MessageProcessor(); } else if (setting.Version >= 8) { handshakeProcessor = new Hybi10HandshakeProcessor(this, setting.Encoding); messageProcessor = new Hybi10MessageProcessor(); } else { handshakeProcessor = new Hybi00HandshakeProcessor(this, setting.Encoding); messageProcessor = new Hybi00MessageProcessor(); } acceptEventArgsPool = new BlockingCollection <SocketAsyncEventArgs>(); for (int i = 0; i < setting.MaxAcceptOps; i++) { var acceptEventArgs = new SocketAsyncEventArgs(); acceptEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(Accept_Completed); acceptEventArgsPool.Add(acceptEventArgs); } var numOfSaeaForRecSend = setting.MaxConnections * 2; bufferManager = new BufferManager(numOfSaeaForRecSend, setting.BufferSize); ioEventArgsPool = new ConcurrentStack <SocketAsyncEventArgs>(); for (int i = 0; i < numOfSaeaForRecSend; i++) { var ioEventArgs = new SocketAsyncEventArgs(); ioEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(IO_Completed); bufferManager.SetBuffer(ioEventArgs); var dataToken = new WSDataToken(); ioEventArgs.UserToken = dataToken; ioEventArgsPool.Push(ioEventArgs); } }
private bool CheckPayloadDataComplated(WSDataToken dataToken, byte[] buffer, ref int offset) { int copyByteCount = dataToken.RemainByte; if (buffer.Length - offset >= copyByteCount) { Buffer.BlockCopy(buffer, offset, dataToken.ByteArrayForMessage, dataToken.MessageBytesDone, copyByteCount); dataToken.MessageBytesDone += copyByteCount; offset += copyByteCount; } else { Buffer.BlockCopy(buffer, offset, dataToken.ByteArrayForMessage, dataToken.MessageBytesDone, buffer.Length - offset); dataToken.MessageBytesDone += buffer.Length - offset; offset += buffer.Length - offset; } return(dataToken.IsMessageReady); }
private bool CheckPrefix2Complated(WSDataToken dataToken, byte[] buffer, ref int offset, int size) { if (dataToken.ByteArrayForPrefix2 == null || dataToken.ByteArrayForPrefix2.Length != size) { dataToken.ByteArrayForPrefix2 = new byte[size]; } if (size - dataToken.PrefixBytesDone2 > buffer.Length - offset) { Buffer.BlockCopy(buffer, offset, dataToken.ByteArrayForPrefix2, dataToken.PrefixBytesDone2, buffer.Length - offset); dataToken.PrefixBytesDone2 += buffer.Length - offset; return(false); } int count = dataToken.ByteArrayForPrefix2.Length - dataToken.PrefixBytesDone2; if (count > 0) { Buffer.BlockCopy(buffer, offset, dataToken.ByteArrayForPrefix2, dataToken.PrefixBytesDone2, count); dataToken.PrefixBytesDone2 += count; offset += count; } return(true); }
internal HandshakeResult Receive(SocketAsyncEventArgs ioEventArgs, WSDataToken dataToken, byte[] data) { if (dataToken.ByteArrayForHandshake == null) { dataToken.ByteArrayForHandshake = new List <byte>(data); } else { dataToken.ByteArrayForHandshake.AddRange(data); } var socket = (WebSocket)dataToken.Socket; var buffer = dataToken.ByteArrayForHandshake.ToArray(); int headLength = MathUtils.IndexOf(buffer, HandshakeEndBytes); if (headLength < 0) { //data not complate, wait receive return(HandshakeResult.Wait); } headLength += HandshakeEndBytes.Length; byte[] headBytes = new byte[headLength]; Buffer.BlockCopy(buffer, 0, headBytes, 0, headBytes.Length); string message = Encoding.GetString(headBytes); HandshakeData handshakeData = socket.Handshake; if (TryParseHandshake(message, handshakeData, out string error)) { if (handshakeData.ParamItems.ContainsKey(HandshakeHeadKeys.SecKey1) && handshakeData.ParamItems.ContainsKey(HandshakeHeadKeys.SecKey2)) { int remainBytesNum = buffer.Length - headLength; if (!handshakeData.IsClient && remainBytesNum == 8) { byte[] secKey3Bytes = new byte[remainBytesNum]; Buffer.BlockCopy(buffer, headBytes.Length, secKey3Bytes, 0, secKey3Bytes.Length); handshakeData.ParamItems[HandshakeHeadKeys.SecKey3] = secKey3Bytes; } else if (handshakeData.IsClient && remainBytesNum == 16) { byte[] secKey3Bytes = new byte[remainBytesNum]; Buffer.BlockCopy(buffer, headBytes.Length, secKey3Bytes, 0, secKey3Bytes.Length); handshakeData.ParamItems[HandshakeHeadKeys.SecAccept] = secKey3Bytes; } else { //data not complate, wait receive return(HandshakeResult.Wait); } } if (!handshakeData.IsClient) { if (!ResponseHandshake(socket, handshakeData)) { //TraceLog.ReleaseWriteDebug("Client {0} handshake fail, message:\r\n{2}", session.Socket.RemoteEndPoint, message); return(HandshakeResult.Close); } dataToken.ByteArrayForHandshake = null; socket.Handshake.Handshaked = true; return(HandshakeResult.Success); } if (CheckSignKey(handshakeData)) { dataToken.ByteArrayForHandshake = null; socket.Handshake.Handshaked = true; return(HandshakeResult.Success); } return(HandshakeResult.Close); } logger.Warn("Client {0} handshake {1} error, detail\r\n{2}", socket.RemoteEndPoint, error, message); return(HandshakeResult.Close); }
public abstract bool TryReadMeaage(WSDataToken dataToken, byte[] buffer, out List <DataMessage> messageList);
private bool CheckPayloadHeadComplated(WSDataToken dataToken, byte[] buffer, ref int offset) { try { if (dataToken.ByteArrayForMessage == null) { int size = 0; int payloadLenght = dataToken.HeadFrame.PayloadLenght; switch (payloadLenght) { case 126: size = 2; //uint16 2bit if (!CheckPrefix2Complated(dataToken, buffer, ref offset, size)) { return(false); } UInt16 len = (UInt16)(dataToken.ByteArrayForPrefix2[0] << 8 | dataToken.ByteArrayForPrefix2[1]); dataToken.ByteArrayForMessage = new byte[len]; break; case 127: size = 8; //uint64 8bit if (!CheckPrefix2Complated(dataToken, buffer, ref offset, size)) { return(false); } UInt64 len64 = BitConverter.ToUInt64(dataToken.ByteArrayForPrefix2.Reverse().ToArray(), 0); dataToken.ByteArrayForMessage = new byte[len64]; break; default: dataToken.ByteArrayForMessage = new byte[payloadLenght]; break; } dataToken.MessageLength = dataToken.ByteArrayForMessage.Length; } if (dataToken.HeadFrame.HasMask) { if (dataToken.ByteArrayMask == null || dataToken.ByteArrayMask.Length != MaskLength) { dataToken.ByteArrayMask = new byte[MaskLength]; } if (MaskLength - dataToken.MaskBytesDone > buffer.Length - offset) { Buffer.BlockCopy(buffer, offset, dataToken.ByteArrayMask, dataToken.MaskBytesDone, buffer.Length - offset); dataToken.MaskBytesDone += buffer.Length - offset; return(false); } int count = dataToken.ByteArrayMask.Length - dataToken.MaskBytesDone; if (count > 0) { Buffer.BlockCopy(buffer, offset, dataToken.ByteArrayMask, dataToken.MaskBytesDone, count); dataToken.MaskBytesDone += count; offset += count; } } return(true); } catch (Exception ex) { throw ex; } }