Apple 推送通知服务 c++ /升压未连接

发布于 2024-11-27 13:42:22 字数 2654 浏览 1 评论 0原文

我目前正在开发带有 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();
}

I'm currently working on a server-side push notification service with Apple push notification.
I'm working with boost asio.
Here's my code :

    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));
  }

The tools::Ip::getServiceIp gives me the ip resolved from the dns :

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();
    }
  };
}

The thing is, the async_connect is called, and then nothing... handle_connection is never called not even with an error.

I'm currently wondering if the problem is not coming from resolving the dns into an ip to connect.
If there another way to ask for a connection with an endpoint pointing towards gateway.push.apple.com directly (without resolving IP) ?

EDIT 1 :

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

誰ツ都不明白 2024-12-04 13:42:23

我目前想知道问题是否不是来自于解决
将 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 无法正常工作或根本不运行造成的。

I'm currently wondering if the problem is not coming from resolving
the dns into an ip to connect.

I don't think this is the cause. Here is a sample program showing synchronous resolve and connect working just fine.

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$ 

In my experience, problems like this are typically caused the the io_service running out of work or not running at all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文