private void ParseContent(RMessage m, Socket s, byte[] b, int headEndIndex, int totalReceiveCount) { int contentBeginIndex = headEndIndex + 4 + 1; byte[] b2 = null; int b2Length; if (totalReceiveCount > contentBeginIndex) { b2Length = totalReceiveCount - contentBeginIndex; b2 = new byte[b2Length]; for (int i = 0; i < b2.Length; i++) { b2[i] = b[contentBeginIndex + i]; } } m.Content = new ContentStream(s, b2, m.ContentLength); }
private void ParseHead(RMessage m, string head) { string[] tokens = head.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); string t; string[] tokens2; Dictionary <string, string> dic = new Dictionary <string, string>(); for (int i = 0; i < tokens.Length; i++) { t = tokens[i]; tokens2 = t.Split(new char[] { ':' }); if (tokens2.Length < 2) { throw new RPCException("Header 应包含 Name 和 Value 。 Name 和 Value 在 冒号 : 的 两边 。"); } dic.Add(tokens2[0].Trim(), tokens2[1].Trim()); } string p; if (dic.TryGetValue("Parameters", out p)) { tokens = p.Split(new char[] { '&' }); string name; string value; for (int i = 0; i < tokens.Length; i++) { t = tokens[i]; if (string.IsNullOrWhiteSpace(t)) { continue; } tokens2 = t.Split(new char[] { '=' }); if (tokens2.Length < 2) { throw new RPCException("参数应包含 Name 和 Value 。 Name 和 Value 在 等号 = 的 两边 。"); } name = tokens2[0]; value = tokens2[1]; name = HttpUtility.UrlDecode(name); if (m.Parameters.ContainsKey(name)) { throw new Exception("重复的 参数名 : \"" + name + "\" 。"); } value = HttpUtility.UrlDecode(value); m.Parameters.Add(name, value); } } string e; if (dic.TryGetValue("Error", out e)) { m.Error = HttpUtility.UrlDecode(e); } string c; if (dic.TryGetValue("Content-Length", out c)) { m.ContentLength = long.Parse(c); } }
public RMessage Parse(Socket s) { byte[] b = new byte[headSize]; int headEndIndex = -1; int receiveCount; int totalReceiveCount = 0; int beginIndex = 0; while (true) { receiveCount = s.Receive(b, totalReceiveCount, b.Length - totalReceiveCount, SocketFlags.None); if (receiveCount == 0) { throw new RPCException("receiveCount = 0 。 对方主机已断开连接 。"); } totalReceiveCount += receiveCount; if (receiveCount > 0) { headEndIndex = FindHeadEnd(b, totalReceiveCount, ref beginIndex); } if (headEndIndex != -1) { break; } if (totalReceiveCount >= b.Length) { break; } } if (headEndIndex == -1) { throw new RPCException("Bad Request . 找不到 Head 结束符 。"); } string head = Encoding.ASCII.GetString(b, 0, headEndIndex + 1); RMessage m = new RMessage(); ParseHead(m, head); if (m.ContentLength > 0) { ParseContent(m, s, b, headEndIndex, totalReceiveCount); } return(m); }
private SMessage OnMessageArrived(RMessage m) { return(this.messageCallback(m)); }