基于WCF的通道网络传输数据压缩技术的应用研究

简介:
本文及程序不是介绍WCF怎么用,而是研究如何在WCF通信时的通道两端自动进行数据压缩和解压缩,从而增加分布式数据传输速度。
 
而且,这个过程是完全透明的,用户及编程人员根本不需要知道它的存在,相当于HOOK在两端的一个组件。可以使用中网络带宽较小
的网络环境中。当WCF在两个实体间通讯的时候,便自动创建一个信息通道转接通讯,这个消息包含数据请求和相应。WCF使用特殊的
编码器将请求和响应数据转换成一系列的字节。
 
    我所带的项目里遇到大文件分布式传输问题,经过分析考虑采用WCF通道压缩技术来解决此问题。执行这样的编码是需要传输大文件(XML格式)由一台机器到另一台机器传输,而连接有速度限制。经过查看了一些国外英文网站,发现我不用写一个特殊的函数边压缩和边解压,而是配置传输通道可以做到这一点,这种方式压缩可重复使用的任何契约。我发现自己编写的消息编码器是最简单的方式来实现功能,真正的问题是如何编写信息编码器,在MSDN上没有找到任何关于此应用的实例。消息契约编码器的想法是Hook连接两端发送和接收信息的渠道。程序是采用Microsoft Visual Studio 2008 WCF设计。
              
                                   图1 WCF消息通道编码过程时序图    
 
发送方:代码中加入方法,该方法及其参数的序列化成SOAP消息,消息编码序列化的信息将成为一个字节数组,字节数组发送传输层。
 接收方:传输层接收字节数组,消息编码器并行化字节数组到一条消息,该方法及其参数并行化到一个SOAP消息,方法是被监听的。 当加入压缩信息编码器,该方法要求有一点改变:发送方:代码中加入方法,该方法及其参数的序列化成SOAP消息,消息契约编码让其内在的信息编码序列的信息成为一个字节数组,消息契约编码压缩的字节数组第二个字节数组,字节数组发送传输层。

     接收方:传输层接收字节数组,消息契约编码的字节数组解压到第二字节数组,消息契约编码让其内在的信息编码化的第二个字节数组消息,该方法及其参并行化到SOAP消息,方法是被监听的。
 
     这个消息契约编码分为几个类:

     CompactMessageEncoder //这个类提供了信息编码实施。

     CompactMessageEncoderFactory //这个类是负责提供契约信息编码实例。

     CompactMessageEncodingBindingElement //这个类负责通道的协议约束规范。
 
     CompactMessageEncodingElement //这个类使信息编码通过增加应用程序配置文件。
                 
                               图2 消息通道编码器静态类图 
 
     压缩方法:契约消息编码器是使用gzip压缩的NET Framework范围内执行的,是调用System.IO.Compression.GZipStream名字空间类中。

     加入引用CompactMessageEncoder.dll,修改app.config文件引用,应用程序必须要在客户端和服务器端。
压缩缓冲代码:
private   static  ArraySegment < byte >  CompressBuffer(ArraySegment < byte >  buffer, BufferManager bufferManager,  int  messageOffset)
        {
            
//  Create a memory stream for the final message
            MemoryStream memoryStream  =   new  MemoryStream();

            
//  Copy the bytes that should not be compressed into the stream
            memoryStream.Write(buffer.Array,  0 , messageOffset);

            
//  Compress the message into the stream
             using  (GZipStream gzStream  =   new  GZipStream(memoryStream, CompressionMode.Compress,  true ))
            {
                gzStream.Write(buffer.Array, messageOffset, buffer.Count);
            }

            
//  Convert the stream into a bytes array
             byte [] compressedBytes  =  memoryStream.ToArray();

            
//  Allocate a new buffer to hold the new bytes array
             byte [] bufferedBytes  =  bufferManager.TakeBuffer(compressedBytes.Length);

            
//  Copy the compressed data into the allocated buffer
            Array.Copy(compressedBytes,  0 , bufferedBytes,  0 , compressedBytes.Length);

             
//  Release the original buffer we got as an argument
            bufferManager.ReturnBuffer(buffer.Array);

            
//  Create a new ArraySegment that points to the new message buffer
            ArraySegment < byte >  byteArray  =   new  ArraySegment < byte > (bufferedBytes, messageOffset, compressedBytes.Length  -  messageOffset);

            
return  byteArray;
        }
解压缓冲代码:
Code
   
private   static  ArraySegment < byte >  DecompressBuffer(ArraySegment < byte >  buffer, BufferManager bufferManager)
        {
            
//  Create a new memory stream, and copy into it the buffer to decompress
            MemoryStream memoryStream  =   new  MemoryStream(buffer.Array, buffer.Offset, buffer.Count);

            
//  Create a memory stream to store the decompressed data
            MemoryStream decompressedStream  =   new  MemoryStream();

            
//  The totalRead stores the number of decompressed bytes
             int  totalRead  =   0 ;

            
int  blockSize  =   1024 ;

            
//  Allocate a temporary buffer to use with the decompression
             byte [] tempBuffer  =  bufferManager.TakeBuffer(blockSize);

            
//  Uncompress the compressed data
             using  (GZipStream gzStream  =   new  GZipStream(memoryStream, CompressionMode.Decompress))
            {
                
while  ( true )
                {
                    
//  Read from the compressed data stream
                     int  bytesRead  =  gzStream.Read(tempBuffer,  0 , blockSize);
                    
if  (bytesRead  ==   0 )
                        
break ;
                    
//  Write to the decompressed data stream
                    decompressedStream.Write(tempBuffer,  0 , bytesRead);
                    totalRead 
+=  bytesRead;
                }
            }
            
//  Release the temporary buffer
            bufferManager.ReturnBuffer(tempBuffer);

            
//  Convert the decompressed data stream into bytes array
             byte [] decompressedBytes  =  decompressedStream.ToArray();

            
//  Allocate a new buffer to store the message 
             byte [] bufferManagerBuffer  =  bufferManager.TakeBuffer(decompressedBytes.Length  +  buffer.Offset);

            
//  Copy the bytes that comes before the compressed message in the buffer argument
            Array.Copy(buffer.Array,  0 , bufferManagerBuffer,  0 , buffer.Offset);

            
//  Copy the decompressed data
            Array.Copy(decompressedBytes,  0 , bufferManagerBuffer, buffer.Offset, decompressedBytes.Length);

            
//  Create a new ArraySegment that points to the new message buffer
            ArraySegment < byte >  byteArray  =   new  ArraySegment < byte > (bufferManagerBuffer, buffer.Offset, decompressedBytes.Length);

            
//  Release the original message buffer
            bufferManager.ReturnBuffer(buffer.Array);

            
return  byteArray;
        }
改变服务端配置
    加入消息契约编码器之前 app.config 的实例:
<? xml version="1.0" encoding="utf-8"  ?>
< configuration >  
    
< system.serviceModel >  
        
< services >  
            
< service  name ="Server.MyService" >
                
< endpoint 
                    
address ="net.tcp://localhost:1234/MyService"  
                    binding
="netTcpBinding"
                    contract
="Server.IMyService"   />  
            
</ service >  
        
</ services >  
    
</ system.serviceModel >
</ configuration >
加入消息契约编码器后 app.config 的例子:
Code
<? xml version="1.0" encoding="utf-8"  ?>
< configuration >  
    
< system.serviceModel >  
        
< services >  
            
< service  name ="Server.MyService" >

            
<!--  Set the binding of the endpoint to customBinding  -->                  
            
< endpoint 
                    
address ="net.tcp://localhost:1234/MyService"  
                    binding
="customBinding"
                    contract
="Server.IMyService"   />  
            
</ service >  
        
</ services >  

        
<!--  Defines a new customBinding that contains the compactMessageEncoding  -->          
        
< bindings >  
            
< customBinding >  
                
< binding  name ="compactBinding" >  
                    
< compactMessageEncoding >

                
<!--  Defines the inner message encoder as binary encoder  -->                         
                
< binaryMessageEncoding  />  
                    
</ compactMessageEncoding >  
                    
< tcpTransport  />  
                
</ binding >  
            
</ customBinding >  
        
</ bindings >  

    
<!--  Adds the extension dll so the WCF can find the compactMessageEncoding  -->
        
< extensions >  
            
< bindingElementExtensions >  
                
< add  name ="compactMessageEncoding"  type ="Amib.WCF.CompactMessageEncodingElement, CompactMessageEncoder, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"   />
            
</ bindingElementExtensions >  
        
</ extensions >  

     
</ system.serviceModel >
</ configuration >
客户端配置变化
加入消息契约编码器之前 app.config 的实例:
ContractedBlock.gif Code
加入消息契约编码器后 app.config 的例子:
ContractedBlock.gif Code
  
这种压缩方法,消息堵塞的几率很小。 使用 CompactMessageEncoder 在同一台机器运行客户端和服务器上可能会降低效率。

 

本文转自高阳 51CTO博客,原文链接:http://blog.51cto.com/xiaoyinnet/196083 ,如需转载请自行联系原作者

相关文章
|
15天前
|
SQL 安全 算法
网络安全与信息安全:防御前线的关键技术与意识
【4月更文挑战第3天】在数字化时代,网络安全与信息安全已成为维护信息完整性、确保数据私密性和保障系统可用性的基石。本文深入探讨了网络安全漏洞的概念、加密技术的应用以及提升安全意识的重要性,旨在为读者提供全面的网络安全知识框架,以应对日益复杂的网络威胁。
|
9天前
|
数据采集 大数据 数据安全/隐私保护
掌握网络抓取技术:利用RobotRules库的Perl下载器一览小红书的世界
本文探讨了使用Perl和RobotRules库在遵循robots.txt规则下抓取小红书数据的方法。通过分析小红书的robots.txt文件,配合亿牛云爬虫代理隐藏真实IP,以及实现多线程抓取,提高了数据采集效率。示例代码展示了如何创建一个尊重网站规则的数据下载器,并强调了代理IP稳定性和抓取频率控制的重要性。
掌握网络抓取技术:利用RobotRules库的Perl下载器一览小红书的世界
|
2天前
|
数据采集 机器学习/深度学习 数据挖掘
网络数据处理中的NumPy应用实战
【4月更文挑战第17天】本文介绍了NumPy在网络数据处理中的应用,包括数据预处理、流量分析和模式识别。通过使用NumPy进行数据清洗、格式化和聚合,以及处理时间序列数据和计算统计指标,可以有效进行流量分析和异常检测。此外,NumPy还支持相关性分析、周期性检测和聚类分析,助力模式识别。作为强大的科学计算库,NumPy在处理日益增长的网络数据中发挥着不可或缺的作用。
|
9天前
|
存储 安全 网络安全
未来云计算与网络安全:技术创新与挑战
随着数字化时代的来临,云计算与网络安全成为了当今科技领域的焦点。本文从技术创新和挑战两个方面探讨了未来云计算与网络安全的发展趋势。在技术创新方面,人工智能、区块链和量子计算等新兴技术将为云计算和网络安全带来前所未有的发展机遇;而在挑战方面,隐私保护、数据泄露和网络攻击等问题也将不断考验着技术研究者和行业从业者的智慧和勇气。未来,只有不断创新,同时加强安全防护,才能实现云计算与网络安全的良性发展。
12 1
|
10天前
|
传感器 监控 安全
|
10天前
|
安全 SDN 数据中心
|
10天前
|
安全 网络安全 网络虚拟化
虚拟网络设备与网络安全:深入分析与实践应用
在数字化时代📲,网络安全🔒成为了企业和个人防御体系中不可或缺的一部分。随着网络攻击的日益复杂和频繁🔥,传统的物理网络安全措施已经无法满足快速发展的需求。虚拟网络设备🖧,作为网络架构中的重要组成部分,通过提供灵活的配置和强大的隔离能力🛡️,为网络安全提供了新的保障。本文将从多个维度深入分析虚拟网络设备是如何保障网络安全的,以及它们的实际意义和应用场景。
|
15天前
|
机器学习/深度学习 安全 网络安全
网络安全与信息安全:防御前沿的技术与策略
【4月更文挑战第4天】在数字化时代,数据成为了新的货币,而网络安全则是保护这种“货币”不被盗窃的关键。本文将深入探讨网络安全漏洞的概念、加密技术的最新进展以及提升个人和企业安全意识的策略。我们将分析当前网络威胁的面貌,探索如何通过多层次的防护手段来构建坚固的信息防线,并强调教育与培训在维护网络安全中的核心作用。
|
1月前
|
机器学习/深度学习 数据采集 人工智能
m基于深度学习网络的手势识别系统matlab仿真,包含GUI界面
m基于深度学习网络的手势识别系统matlab仿真,包含GUI界面
38 0
|
1月前
|
机器学习/深度学习 算法 计算机视觉
基于yolov2深度学习网络的火焰烟雾检测系统matlab仿真
基于yolov2深度学习网络的火焰烟雾检测系统matlab仿真