代码改变世界

Light weight Framework (AnyBase) -- 通信模块说明

2009-10-24 21:32  Animax!  阅读(1183)  评论(1编辑  收藏  举报

Communications通讯接口
  通信接口均以BackgroundService为基础,BackgroundService是提供后台服务的基础模块。
      IBackgroundService 为后台服务组件的接口,BackgroundServiceBase后台服务组件基类。
      IBackgroundServicesExecutor为后台服务执行者的接口,BackgroundServicesExecutor后台服务执行者基类。

           ContinuousBackgroundServiceBase :后台服务组件,继承于BackgroundServiceBase,提供连续循环执行服务。
        TimingBackgroundServiceBase :后台服务组件,继承于BackgroundServiceBase,提供间隔指定时间循环执行服务。

 

RESTCommunicationREST通信方式

服务端:

>> 配置:

         配置路径:Config/RESTServiceConfig.xml

         配置例子:

<?xml version="1.0" encoding="utf-8" ?>
<root>
 <DefaultPort>10012</DefaultPort> <!--默认端口-->
 <HostAddress>animax-pc:100</HostAddress> <!--监听路径-->
 <HostAddress>127.0.0.1:100</HostAddress>
 <HostAddress>192.168.1.20:100</HostAddress>
 <ResourceHost>DemoApplication.RESTHostA</ResourceHost> <!--资源主机路径-->
 <ResourceHost>DemoApplication.RESTHostB</ResourceHost>
</root>

         上面的配置可以等价于:

    RESTServicesExecutor.ExecutorInstance.DefaultPort = 10012;
    RESTServicesExecutor.ExecutorInstance.Hosts.Add(new RESTServicesExecutor.HostAddress() { Port = 100, URLHost = "animax-pc" });
    RESTServicesExecutor.ExecutorInstance.Hosts.Add(new RESTServicesExecutor.HostAddress() { Port = 100, URLHost = "127.0.0.1" });
    RESTServicesExecutor.ExecutorInstance.Hosts.Add(new RESTServicesExecutor.HostAddress() { Port = 100, URLHost = "192.168.1.20" });
 
    
RESTServicesExecutor.ExecutorInstance.SetHost("DemoApplication.RESTHostA");
    RESTServicesExecutor.ExecutorInstance.SetHost("DemoApplication.RESTHostB");
    或者
    RESTServicesExecutor.ExecutorInstance.RESTResourceHosts.Add(new RESTHostA ());
  RESTServicesExecutor.ExecutorInstance.RESTResourceHosts.Add(new RESTHostB ());

 

>>  Host逻辑类:
     上面例子的RESTHostA RESTHostB 均继承于RESTResourceHost
    继承于RESTResourceHost的类可重载OnInitialization ,来实现初始化需要的设定。要使用RESTResourceHost 必须设置属性 ListenUri ,这个属性指定了需要监听的URL地址。

例如:
    RESTServicesExecutor Hosts 设置了”animax-pc“ RESTResourceHost ListenUri设置成”test” ,这个Host便会监听 http:// animax-pc/ test 这个路径。
    RESTResourceHost会根据请求的类型( get, post, put, delete )把请求分配到不同的方法中,Host可以根据需求重载DoGETResponseDoPOSTResponse等方法。其中方法DoCustomizeResponse 是当使用自定义请求类型时调用的方法。
         使用时可以重载这些方法来实现对应的逻辑。

        

>> 调用:

可以在BackgroundServicesExecutor中统一开启或关闭REST主机:

    // 开启所有已配置在RESTServicesExecutor中的host
    RESTServicesExecutor.ExecutorInstance.Start();
    // 关闭所有已配置在RESTServicesExecutor中的host
    RESTServicesExecutor.ExecutorInstance.Stop();

 

客户端:

>> 配置:

         配置路径:Config/RESTClientHostConfig.xml

         配置例子:

<?xml version="1.0" encoding="utf-8" ?>
<root>
 <HostUrl>animax-pc:100</HostUrl><!--服务器地址-->
 <ReserveHostUrl>127.0.0.1</ReserveHostUrl><!--备用服务器地址-->
 <ReserveHostUrl>localhost</ReserveHostUrl>
</root>    

         上面的配置等价于在每一个RESTClient上都修改 HostUrl ReserveHostUrl属性,如:

    RESTClientFactory.GetClient<DemoRESTClient>().HostUrl = "animax-pc:100";
    RESTClientFactory.GetClient<DemoRESTClient>().ReserveHostUrl.Add("127.0.0.1");
    RESTClientFactory.GetClient<DemoRESTClient>().ReserveHostUrl.Add("localhost");

 

>>  Client逻辑类:

         要实现RESTClient可以继承RESTClientBase类,然后写入属性ResourceUrl
         RESTClient将会结合HostUrl属性来确定服务器的访问地址。        

        

>> 调用:

         调用方式大致如下:

    // 构造请求信息
    RequestInfo _requestInfo = new RequestInfo();
    _requestInfo.Method = "POST";
    _requestInfo.QueryStringParameters = new StringParameters(); // URL参数
    _requestInfo.QueryStringParameters["urlParam"] = "123456";
    _requestInfo.PostType = PostDataType.StringParameters; // Post参数, PostType选择上传post参数的形式
    _requestInfo.PostStringParameters = new StringParameters();
    _requestInfo.PostStringParameters["postParam"] = "123456";

    
TestClient client = RESTClientFactory.GetClient<TestClient>();
    // 直接请求
    client.RunRequest(_requestInfo, (stream, contentType, info) =>
        {
            // 这里处理调用成功的操作.
            // (WebResponse)stream 为调用成功后获取到的数据流,
            // (string)contentType 翻回的数据类型标识,
            // (RequestInfo)info 请求的信息
        },
        (info, ex) =>
        {
            // 这里处理调用失败的操作.
            // (RequestInfo)info 请求的信息,
            // (Exception)ex 异常信息
        });
    // 异步请求
    client.RunRequestAsyn(_requestInfo, (stream, contentType, info) =>
        {
            // 这里处理调用成功的操作.
        },
        (info, ex) =>
        {
            // 这里处理调用失败的操作.
        });

    
//
直接请求并返回结果(ClientResponseResult)
    client.GetRequestResult(_requestInfo);
    // 异步请求并返回(ClientResponseResult)结果
    client.GetRequestResultAsyn(_requestInfo, (result) =>
        {
            // 处理返回的ClientResponseResult信息.
        });     

        

TCPCommunicationREST通信方式

服务端:

>> 配置:

         配置路径:Config/ TCPServiceConfig.xml

         配置例子:

<?xml version="1.0" encoding="utf-8" ?>
<root>
 <TCPHost>
    <HostAddress>127.0.0.1</HostAddress>
    <HostPort>10000</HostPort>
    <HostType>DemoApplication.TCPHostA</HostType>
 </TCPHost>
 <TCPHost>
    <HostAddress>127.0.0.1</HostAddress>
    <HostPort>10001</HostPort>
    <HostType>DemoApplication.TCPHostB</HostType>
 </TCPHost>
</root>

这个配置指明需要监听什么服务器的什么端口,并且逻辑的操作是由什么处理的。

         上面的配置等价于:

    TCPServicesExecutor.ExecutorInstance.SetHost("DemoApplication.TCPHostA", new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10000));
    TCPServicesExecutor.ExecutorInstance.SetHost("DemoApplication.TCPHostB", new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10000));


>>  Host逻辑类:

         上面配置文件中的TCPHostATCPHostB均是继承于TCPResourceHost类的,继承TCPResourceHost的类可以重载StringActionsBytesActions方法来处理传入的请求,而使用bytes还是使用string 的方式处理请求可以通过配置属性IsStringActions来确定。

        

>> 调用:

         可以在TCPServicesExecutor中统一开启或关闭TCP主机

    TCPServicesExecutor.ExecutorInstance.Start();
    TCPServicesExecutor.ExecutorInstance.Stop();

 

客户端:

>> 配置:

         配置路径:Config/ TCPClientHostConfig.xml

         配置例子:

<?xml version="1.0" encoding="utf-8" ?>
<root>
 <Host Key="local">
    <HostAddress>127.0.0.1</HostAddress>
    <HostPort>10000</HostPort>
 </Host>
 <
Host Key="animax-pc">
    <HostAddress>192.168.1.20</HostAddress>
    <HostPort>10001</HostPort>
 </Host>
</root>

 

>>  Client逻辑类:

         最基础的TCPClient类是TCPClientBase,而TCPClientBase只是提供以bytes方式的处理。TCPStringClientBase继承于TCPClientBase,他提供了以string方式处理请求的方法。
      Clien类继承了TCPClientBaseTCPStringClientBase后可以通过HostKey属性来指定这个client所访问的服务器路径,这个属性与配置文件中HostKey属性匹配。   

        

>> 调用:

    TCPClient client = TCPClientFactory.GetClient<TCPClient>();
    // 即时提交请求
    client.Request(/* 需要提交到服务器 string 或 byte 信息 */);
    // 异步提交请求
    client.RequestAsync(/* 需要提交到服务器 string 或 byte 信息 */);
 
    client.DataPostBackEvent += new GeneralEventHandler<byte[]>((o, e) =>
    {
        // 服务器返回的信息(bytes)
    });
    client.StringDataPostBackEvent += new GeneralEventHandler<string>((o, e) =>
    {
        // 服务器返回的信息(string)
    });

 

 项目连接:http://anybase.codeplex.com/