Apple 推送通知服务 c++ /升压未连接
我目前正在开发带有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不认为这是原因。这是一个示例程序,显示同步解析和连接工作正常。
根据我的经验,此类问题通常是由于
io_service
无法正常工作或根本不运行造成的。I don't think this is the cause. Here is a sample program showing synchronous resolve and connect working just fine.
In my experience, problems like this are typically caused the the
io_service
running out of work or not running at all.