Apple Push通知服务c ++ / boost无法连接

时间:2011-08-03 12:03:16

标签: c++ asynchronous boost-asio apple-push-notifications

我目前正在使用Apple推送通知处理服务器端推送通知服务。 我正在使用boost asio。 这是我的代码:

    void IPhonePush::connection() {
    std::cout << "Push iPhone connection" << std::endl;
    std::cout << getCertif() << std::endl;
    m_context.set_options(boost::asio::ssl::context::default_workarounds
                          | boost::asio::ssl::context::no_sslv2
                          | boost::asio::ssl::context::single_dh_use);

    m_context.set_password_callback(boost::bind(&IPhonePush::get_password, this));
    m_context.use_certificate_chain_file(getCertif().c_str());
    m_context.use_private_key_file(getKey().c_str(), boost::asio::ssl::context::pem);
    boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address::from_string(tools::Ip::getServiceIp("gateway.push.apple.com")), 2195);
    m_socket.lowest_layer().async_connect(ep,
                                    boost::bind(&IPhonePush::handle_connection,
                                                this,
                                                boost::asio::placeholders::error));
  }

  void IPhonePush::handle_connection(const boost::system::error_code& error) {
    if (!error) {
      std::cout << "/////////// Connected to PUSH SERVER !!!" << std::endl;
      start_handshake();
    } else {
      std::cout << "/////////// Error Connecting to push: " << error.message() << std::endl;
    }
  }

  void IPhonePush::start_handshake() {
    m_socket.async_handshake(boost::asio::ssl::stream_base::client,
                             boost::bind(&IPhonePush::handle_handshake,
                                         this,
                                         boost::asio::placeholders::error));
  }

tools :: Ip :: getServiceIp给我从dns解析的ip:

namespace tools {
  class Ip {
  public:
    static std::string getServiceIp(std::string url) {
      boost::asio::io_service io;
      tcp::resolver resolver(io);
      tcp::resolver::query query(url, "");
      tcp::resolver::iterator iter = resolver.resolve(query);

      tcp::endpoint ep;
      while (iter != tcp::resolver::iterator()) {
        ep = *iter++;
        break;
      }
      return ep.address().to_string();
    }
  };
}

问题是,async_connect被调用,然后什么都没有...即使出错也不会调用handle_connection。

我目前想知道问题是不是来自将dns解析为ip连接。 如果有另一种方式要求与直接指向gateway.push.apple.com的端点建立连接(不解析IP)?

编辑1:

int main() {
     boost::asio::io_service io_push;
     push::IPhonePush ifpush(io_push);
     ifpush.connection();
     io_push.run();
}

1 个答案:

答案 0 :(得分:2)

  

我目前想知道问题是不是来自解决问题   dns进入ip连接。

我不认为这是原因。这是一个示例程序,显示同步解析和连接工作正常。

mac:stackoverflow samm$ cat push.cc
#include <boost/asio.hpp>

#include <iostream>

int
main( unsigned argc, const char** argv )
{
    using namespace boost::asio;

    io_service io_service;

    ip::tcp::resolver resolver( io_service );
    ip::tcp::resolver::query query(
            "gateway.push.apple.com",
            "2195"
            );
    ip::tcp::resolver::iterator i = resolver.resolve( query );

    ip::tcp::socket socket( io_service );
    socket.connect( *i );
    std::cout << "connected to: " << ip::tcp::endpoint(*i) << std::endl;
}
mac:stackoverflow samm$ g++ -I /opt/local/include -L/opt/local/lib -lboost_system -Wl,-rpath,/opt/local/lib push.cc
mac:stackoverflow samm$ ./a.out
connected to: 17.172.239.6:2195
mac:stackoverflow samm$ 

根据我的经验,这样的问题通常会导致io_service停止工作或根本不运行。