public SocketConnection(TcpClient tcpClient) { if (tcpClient != null) { NetworkStream networkStream = tcpClient.GetStream(); inOutStream = new MixedStream(networkStream, Encoding.UTF8, 1024 * 1024 * 2); isBusy = false; } }
private Message ignoreProgress(MixedStream st) { Message message; State state; do { message = Message.read(st); state = message.getState(); }while (state == State.Progress || state == State.None); return(message); }
public static Attachment read(MixedStream st, long length, int position) { if (length < AttachmentMemoryLimit) { byte[] data = new byte[length]; st.Read(data); return(new ArrayAttachment(data, position)); } else { // TODO: read from stream into FileAttachment } return(null); }
public static Message read(MixedStream st) { byte[] len1 = new byte[8]; st.Read(len1); if (BitConverter.IsLittleEndian) { Array.Reverse(len1); } long lenHeader = BitConverter.ToInt64(len1, 0); byte[] len2 = new byte[8]; st.Read(len2); if (BitConverter.IsLittleEndian) { Array.Reverse(len2); } long lenAttachments = BitConverter.ToInt64(len2, 0); byte[] msg = new byte[lenHeader]; st.Read(msg); string header = Encoding.UTF8.GetString(msg, 0, (int)lenHeader); byte[] len3 = new byte[8]; st.Read(len3); if (BitConverter.IsLittleEndian) { Array.Reverse(len3); } long numAttachments = BitConverter.ToInt64(len3, 0); Attachment[] rawAttachments = new Attachment[numAttachments]; if (numAttachments > 0) { for (int i = 0; i < numAttachments; i++) { byte[] attLen = new byte[8]; st.Read(attLen); if (BitConverter.IsLittleEndian) { Array.Reverse(attLen); } long lenAtt = BitConverter.ToInt64(attLen, 0); rawAttachments[i] = Attachment.read(st, lenAtt, i + 1); } } return(new Message(header, rawAttachments)); }
public void writeTo(MixedStream st) { // important to access attachments before header is being BUILT: // attachment positions will be initialized IOrderedEnumerable <Attachment> attachmentList = getAttachments(); string headerString = JsonConvert.SerializeObject(getHeader()); byte[] headerBytes = Encoding.UTF8.GetBytes(headerString); byte[] lenHeader = BitConverter.GetBytes((long)headerBytes.Length); byte[] numAttachments = BitConverter.GetBytes((long)this.numAttachments()); long attachmentLen = this.lenAttachments() + (this.numAttachments() + 1) * 8; byte[] lenAttachments = BitConverter.GetBytes((long)attachmentLen); if (BitConverter.IsLittleEndian) { Array.Reverse(lenHeader); Array.Reverse(lenAttachments); Array.Reverse(numAttachments); } st.Write(lenHeader); st.Flush(); st.Write(lenAttachments); st.Flush(); st.Write(headerBytes); st.Flush(); st.Write(numAttachments); st.Flush(); if (this.numAttachments() > 0) { foreach (Attachment attachment in attachmentList) { byte[] lenAttachment = BitConverter.GetBytes((long)attachment.getLength()); if (BitConverter.IsLittleEndian) { Array.Reverse(lenAttachment); } st.Write(lenAttachment); st.Flush(); attachment.write(st); st.Flush(); } } }
public abstract void write(MixedStream st);
public override void write(MixedStream st) { // TODO: wriate file attachment to stream throw new NotImplementedException(); }
public override void write(MixedStream st) { st.Write(data); }