protected override void MessageReady() { if (mode == Modes.Sip || mode == Modes.WebSocket || mode == Modes.Ajax) { if (SipReader.IsValid(Id)) { SipReader.Value.SetArray(base.Header.Array); } } if (mode == Modes.Http || mode == Modes.Ajax) { if (HttpReaderx.IsValid(Id)) { HttpReaderx.Value.SetArray(base.Header.Array); } } wsMaskIndex = 0; wsWaitHeader = true; }
protected override ParseResult Parse(ArraySegment <byte> data) { switch (mode) { case Modes.Sip: { if (keepAliveCount != 0) { return(SkipKeepAlive(data)); } else { var sipReader = SipReader.GetEmpty(); int headerLength = sipReader.Parse(data.Array, data.Offset, data.Count); if (sipReader.IsFinal) { return(ParseResult.HeaderDone(headerLength, sipReader.HasContentLength ? sipReader.ContentLength : 0)); } if (sipReader.IsError) { if (IsKeepAliveByte(data.Array[data.Offset], 0)) { return(SkipKeepAlive(data)); } else { mode = Modes.Http; goto case Modes.Http; } } return(ParseResult.NotEnoughData()); } } case Modes.Http: { var httpReader = HttpReaderx.GetEmpty(); httpHeaderLength = httpReader.Parse(data.Array, data.Offset, data.Count); if (httpReader.IsFinal) { httpContentLength = httpReader.HasContentLength ? httpReader.ContentLength : 0; if (AjaxWebsocket.IsAjax(httpReader, data.Array)) { mode = Modes.Ajax; goto case Modes.Ajax; } return(ParseResult.HeaderDone(httpHeaderLength, httpContentLength)); } if (httpReader.IsError) { return(ParseResult.Error()); } return(ParseResult.NotEnoughData()); } case Modes.WebSocket: { if (wsWaitHeader) { int used = wsHeader.Parse(data); if (wsHeader.IsDone) { wsWaitHeader = false; } return(ParseResult.Skip(used)); } else { if (IsSipWebSocket == false) { return(ParseResult.HeaderDone(0, wsHeader.PayloadLength)); } else { var sipReader = SipReader.GetEmpty(); int headerLength = sipReader.Parse(data.Array, data.Offset, data.Count); if (sipReader.IsFinal) { return(ParseResult.HeaderDone(headerLength, sipReader.HasContentLength ? sipReader.ContentLength : 0)); } if (sipReader.IsError) { return(ParseResult.Error()); } return(ParseResult.NotEnoughData()); } } } case Modes.Ajax: { if (httpContentLength > 0) { if (httpHeaderLength < data.Count) { var sipReader = SipReader.GetEmpty(); int sipHeaderLength = sipReader.Parse(data.Array, data.Offset + httpHeaderLength, data.Count - httpHeaderLength); if (sipReader.IsFinal) { return(ParseResult.HeaderDone(httpHeaderLength + sipHeaderLength, sipReader.HasContentLength ? sipReader.ContentLength : 0)); } if (sipReader.IsError || httpContentLength <= data.Count - httpHeaderLength) { return(ParseResult.Error()); } } HttpReaderx.Invalidate(); return(ParseResult.NotEnoughData()); } else { return(ParseResult.HeaderDone(httpHeaderLength, 0)); } } default: throw new NotImplementedException(); } }