前面简单写了点静态结构,这一次将主要关注动态模型以及调用方式。
这个系列的名字叫“为C++实现一个IDL”,实际上应该叫“为C++实现一个Remoting”可能更好一些,说是IDL,主要是想通过宏,使用简单的类型定义达到自动生成调用代码的目的。
一、首先来看看调用习惯。
从调用习惯入手,主要是因为看到目前有很多库/工具包在调用上都有很多不便之处。假如能在一开始就从这点出发,就能把调用接口设计得更好一些。
先来看看服务端如何开放一个服务。
int
main ()
{
// 发布为SOAP服务,先生成一个服务容器。
// 服务将发布在localhost的7911上,localhost用来绑定loopback网卡。
SOAPProxy soap_service ( 7911 , “localhost”);
TestService test_service;
soap_service.addService (“test_service”, & test_service);
TestService service1;
soap_service.addService (“HelloService”, & service1);
try {
soap_service.run ();
} catch (SocketException & e)
{
} catch (SignalException & e)
{
}
return 0 ;
}
{
// 发布为SOAP服务,先生成一个服务容器。
// 服务将发布在localhost的7911上,localhost用来绑定loopback网卡。
SOAPProxy soap_service ( 7911 , “localhost”);
TestService test_service;
soap_service.addService (“test_service”, & test_service);
TestService service1;
soap_service.addService (“HelloService”, & service1);
try {
soap_service.run ();
} catch (SocketException & e)
{
} catch (SignalException & e)
{
}
return 0 ;
}
我希望就是这么简单,客户端调用有多种方式:
1、 使用服务的IDL定义,直接调用:
int
main ()
{
SOAPProxy soap_service ( 7911 , “localhost”);
try {
TestService test_service (“test_service”, & soap_service);
test_service.method1 ( /*
*/
);
} catch (SocketException & e)
{
}
return 0 ;
}
{
SOAPProxy soap_service ( 7911 , “localhost”);
try {
TestService test_service (“test_service”, & soap_service);
test_service.method1 ( /*

} catch (SocketException & e)
{
}
return 0 ;
}
这种方式比较简单,调用时会检查是否已经连接,然后发送调用请求,并处理调用结果。
2、 服务验证方式: