Exemplo n.º 1
0
        private byte[] ConvertRequestMessage(byte[] rawData)
        {
            byte[] fastinfosetData = null;
            byte[] compressedData  = null;

            //Fast Info Set
            if (isFastinfoset)
            {
                string message = Encoding.UTF8.GetString(rawData);
                fastinfosetData = FastinfosetUtil.GenerateFastInfoSet(message);
            }
            else
            {
                fastinfosetData = rawData;
            }

            //Gzip Compress
            if (isGzip)
            {
                compressedData = GzipUtil.Compress(fastinfosetData);
            }
            else
            {
                compressedData = fastinfosetData;
            }

            return(compressedData);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="message"></param>
        public void Send(string message)
        {
            byte[] rawData         = Encoding.UTF8.GetBytes(message);
            byte[] fastinfosetData = null;
            byte[] compressedData  = null;

            //Fast Info Set
            if (httpRequest.CommonHeaders.Keys.Contains("Content-Type") &&
                httpRequest.CommonHeaders["Content-Type"].Contains("application/fastinfoset"))
            {
                fastinfosetData = FastinfosetUtil.GenerateFastInfoSet(message);
            }
            else
            {
                fastinfosetData = rawData;
            }

            //Gzip Compress
            if (httpRequest.CommonHeaders.Keys.Contains("Accept-Encoding") &&
                httpRequest.CommonHeaders["Accept-Encoding"].Equals("gzip"))
            {
                compressedData = GzipUtil.Compress(fastinfosetData);
            }
            else
            {
                compressedData = fastinfosetData;
            }
            httpRequest.CommonHeaders["Content-Length"] = compressedData.Length.ToString();
            httpRequest.CommonHeaders["Pragma"]         = "requestnum=\"" + requestSeq + "\"";
            requestSeq++;

            //Conbine the Header and message body

            byte[]            headerData = Encoding.UTF8.GetBytes(httpRequest.GetHeaderString());
            Collection <byte> totalData  = new Collection <byte>();

            for (int i = 0; i < headerData.Length; i++)
            {
                totalData.Add(headerData[i]);
            }
            for (int i = 0; i < compressedData.Length; i++)
            {
                totalData.Add(compressedData[i]);
            }

            byte[] totalBytes    = totalData.ToArray();
            Stream requestStream = httpRequest.GetRequestStream(new IPEndPoint(Dns.GetHostEntry(hostAddress).AddressList[0], port));

            requestStream.Write(totalBytes, 0, totalBytes.Length);
        }