1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FastAPNS; |
4
|
|
|
|
5
|
|
|
class ClientStreamSocket { |
6
|
|
|
const FASTAPNS_DEFAULT_GATEWAY_HOST = 'gateway.push.apple.com'; |
7
|
|
|
const FASTAPNS_DEFAULT_GATEWAY_PORT = 2195; |
8
|
|
|
const FASTAPNS_STATUS_TIMEOUT = 3; |
9
|
|
|
|
10
|
|
|
const FASTAPNS_WRITE_SUCCESS = 1; |
11
|
|
|
const FASTAPNS_STATUS_WRITABLE = 2; |
12
|
|
|
const FASTAPNS_STATUS_READABLE = 3; |
13
|
|
|
const FASTAPNS_STATUS_NONE = 4; |
14
|
|
|
|
15
|
|
|
private $local_cert; |
16
|
|
|
private $passphrase; |
17
|
|
|
private $host; |
18
|
|
|
private $port; |
19
|
|
|
|
20
|
|
|
private $stream_socket_client; |
21
|
|
|
|
22
|
|
|
public function __construct($local_cert, $passphrase = '', $host = ClientStreamSocket::FASTAPNS_DEFAULT_GATEWAY_HOST, $port = ClientStreamSocket::FASTAPNS_DEFAULT_GATEWAY_PORT) { |
23
|
|
|
$this->local_cert = $local_cert; |
24
|
|
|
$this->passphrase = $passphrase; |
25
|
|
|
$this->host = $host; |
26
|
|
|
$this->port = $port; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function connect() { |
30
|
|
|
$streamContext = stream_context_create(); |
31
|
|
|
|
32
|
|
|
stream_context_set_option($streamContext, 'ssl', 'local_cert', $this->local_cert); |
33
|
|
|
stream_context_set_option($streamContext, 'ssl', 'passphrase', $this->passphrase); |
34
|
|
|
|
35
|
|
|
$this->stream_socket_client = stream_socket_client('ssl://' . $this->host . ':' . $this->port, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext); |
36
|
|
|
|
37
|
|
|
if (!empty($error)) { |
38
|
|
|
throw new \Exception('Error creating stream socket client: ' . $errorString); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function disconnect() { |
43
|
|
|
fclose($this->stream_socket_client); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function reconnect() { |
47
|
|
|
$this->disconnect(); |
48
|
|
|
$this->connect(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function isConnected() { |
52
|
|
|
return !empty($this->stream_socket_client); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param $notification_bytes |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
|
|
public function write($notification_bytes) { |
60
|
|
|
try { |
61
|
|
|
fwrite($this->stream_socket_client, $notification_bytes); |
62
|
|
|
|
63
|
|
|
return ClientStreamSocket::FASTAPNS_WRITE_SUCCESS; |
64
|
|
|
} catch (\Exception $e) { |
65
|
|
|
return $this->status(); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function read() { |
70
|
|
|
$bytes = fread($this->stream_socket_client, 6); |
71
|
|
|
|
72
|
|
|
return unpack("C1command/C1status/N1identifier", $bytes); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function readFeedback() { |
76
|
|
|
$feedback = array(); |
77
|
|
|
|
78
|
|
|
while(!feof($this->stream_socket_client)) { |
79
|
|
|
$data = fread($this->stream_socket_client, 38); |
80
|
|
|
|
81
|
|
|
if(strlen($data)) { |
82
|
|
|
$feedback[] = unpack("N1timestamp/n1length/H*devtoken", $data); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $feedback; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function status($readOnly = FALSE) { |
90
|
|
|
$read = array($this->stream_socket_client); |
91
|
|
|
$write = $readOnly ? NULL : array($this->stream_socket_client); |
92
|
|
|
$except = NULL; |
93
|
|
|
|
94
|
|
|
stream_select($read, $write, $except, ClientStreamSocket::FASTAPNS_STATUS_TIMEOUT); |
95
|
|
|
|
96
|
|
|
if (!empty($write)) { |
97
|
|
|
return ClientStreamSocket::FASTAPNS_STATUS_WRITABLE; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if (!empty($read)) { |
101
|
|
|
return ClientStreamSocket::FASTAPNS_STATUS_READABLE; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return ClientStreamSocket::FASTAPNS_STATUS_NONE; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|