public dynamic Decode(IChannel channel, PacketBuffer buffer) { buffer.BeginBufferIndex(); if(buffer.AvailableBytes() > MaxBufferSize) { channel.Disconnect(); return null; } if (buffer.AvailableBytes() == 0) return null; Request request; try { request = Request.Parse(buffer); } catch (Exception) { buffer.ResetBufferIndex(); return null; } buffer.EndBufferIndex(); return request; }
public object Decode(IChannel channel, PacketBuffer buffer) { //버퍼 읽기 시작을 알림 buffer.BeginBufferIndex(); if (buffer.AvailableBytes() < 6) //버퍼길이가 5미만이면 리턴 return null; buffer.ReadByte(); uint len = buffer.ReadUInt32(); if (len > buffer.AvailableBytes()) { //버퍼의 길이가 실제 패킷 길이보다 모자름으로, 리셋후 리턴 buffer.ResetBufferIndex(); return null; } var data = new byte[len]; buffer.ReadBytes(data); buffer.EndBufferIndex(); var stream = new MemoryStream(data); var res = Unpacker.Create(stream).Unpack<MessagePackObject>(); stream.Dispose(); return res; }
/// <summary> /// BSON 패킷 구조를 따르는 PacketBuffer을 BSON Data로 변환 시키는 메서드 /// </summary> /// <param name="buffer">BSON 패킷 구조를 따르는 Packet Buffer</param> /// <returns>BSON Data</returns> public dynamic Decode(IChannel channel, PacketBuffer buffer) { //버퍼 읽기 시작을 알림 buffer.BeginBufferIndex(); if (buffer.AvailableBytes() < 5) //버퍼길이가 5미만이면 리턴 return null; uint len = buffer.ReadUInt32(); if (len > buffer.AvailableBytes()) { //버퍼의 길이가 실제 패킷 길이보다 모자름으로, 리셋후 리턴 buffer.ResetBufferIndex(); return null; } var data = new byte[len]; buffer.ReadBytes(data); buffer.EndBufferIndex(); var stream = new MemoryStream(data); dynamic res = Serializer.Deserialize(new BsonReader(stream)); stream.Dispose(); return res; }
private void LoadPostData(string contentType, PacketBuffer buffer, string name = null) { if (contentType == null) { string data = buffer.ReadString((int) buffer.AvailableBytes()); if(name != null) _postData.Add(name, data); } else if (contentType == "application/x-www-form-urlencoded") { string stringData = buffer.ReadString((int) buffer.AvailableBytes()); foreach (string q in stringData.Split('&')) { int valueStartPoint = q.IndexOf("=", System.StringComparison.Ordinal); if (valueStartPoint == -1 && q.Length > valueStartPoint + 1) continue; _postData.Add(q.Substring(0, valueStartPoint), q.Substring(valueStartPoint + 1)); } } else if (contentType.StartsWith("multipart/form-data;")) { byte[] p = System.Text.Encoding.UTF8.GetBytes(string.Format("\r\n--{0}", contentType.Substring(contentType.IndexOf("boundary=") + 9))); long len = 0; while ((len = buffer.FindBytes(p)) > -1) { var buf = new PacketBuffer(); buf.WriteBytes(buffer.ReadBytes((int)len)); buffer.ReadBytes(p.Length); buf.BeginBufferIndex(); buf.ReadLine(); var hDictionary = new Dictionary<string, string>(); string h = ""; while((h = buf.ReadLine()) != "") { if (h == null) break; int valueStartIndex = h.IndexOf(": ", System.StringComparison.Ordinal); if (valueStartIndex == -1) continue; string key = h.Substring(0, valueStartIndex).ToLower(); string value = h.Substring(valueStartIndex + 2, h.Length - valueStartIndex - 2); hDictionary.Add(key, value); } if (hDictionary.Count != 0) { string pname = hDictionary["content-disposition"].Substring( hDictionary["content-disposition"].IndexOf("name") + 6); pname = pname.Substring(0, pname.Length - 1); string type = null; try { type = hDictionary["content-type"]; } catch (Exception) { } LoadPostData(type, buf, pname); } buf.Dispose(); } } else if (name != null) { string tempName = Path.GetTempFileName(); var stream = new FileStream(tempName, FileMode.OpenOrCreate, FileAccess.Write); byte[] data = new byte[1024]; int len; while((len = buffer.Read(data, 0, 1024)) > 0) stream.Write(data, 0, len); stream.Close(); ((Dictionary<string, string>)_postData["FILES"]).Add(name, tempName); } }
public bool SetPostData(PacketBuffer buffer) { if (Convert.ToInt64(GetHeader("Content-Length")) > buffer.AvailableBytes() + 1) return false; var buf = buffer.GetStream() as MemoryStream; if(buf != null) _lowPostData = new MemoryStream(buf.GetBuffer(), (int)buf.Position, (int)(buf.Length - buf.Position)); LoadPostData(GetHeader("Content-Type"), buffer); return true; }
public dynamic Decode(IChannel channel, PacketBuffer buffer) { buffer.BeginBufferIndex(); var stream = new MemoryStream(); while (true) { if (buffer.AvailableBytes() < 2) return null; byte frameH = buffer.ReadByte(); byte frameP = buffer.ReadByte(); int len = frameP & 0x7F; if (len > 0x7D) { if (buffer.AvailableBytes() < 2) return null; for (var i = 0; i < 2; i++) len = (len << 8) + buffer.ReadByte(); if ((frameP & 0x7F) == 0x7F) { if (buffer.AvailableBytes() < 2) return null; for (var i = 0; i < 2; i++) len = (len << 8) + buffer.ReadByte(); } } if (buffer.AvailableBytes() < 4 + len) return null; byte[] key = (frameP & 0x80) == 0x80 ? buffer.ReadBytes(4) : null; byte[] data = null; if (key == null) { data = buffer.ReadBytes(len); } else { data = new byte[len]; for (int i = 0; i < len; i++) data[i] = (byte) (buffer.ReadByte() ^ key[i%4]); } stream.Write(data, 0, len); if ((frameH & 0xF) == 8) { buffer.EndBufferIndex(); return new ConnectionClose(); } if ((frameH & 0xF) == 9) { buffer.EndBufferIndex(); return new Ping(); } if((frameH & 0xF) == 10) { buffer.EndBufferIndex(); return new Pong(); } if((frameH & 0x80) == 128) break; } buffer.EndBufferIndex(); return stream.ToArray(); }