-
Notifications
You must be signed in to change notification settings - Fork 2
/
ngx_channel.php
156 lines (120 loc) · 3.4 KB
/
ngx_channel.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/**
* Created by PhpStorm.
* User: zenus
* Date: 15-12-18
* Time: 下午10:30
*/
class ngx_channel_t {
/** ngx_uint_t **/ private $command;
/** ngx_pid_t **/ private $pid;
/** ngx_int_t **/ private $slot;
/** ngx_fd_t ***/ private $fd;
public function __set($name,$value){
$this->$name = $value;
}
public function __get($name){
return $this->$name;
}
}
function ngx_close_channel( $fd, ngx_log $log)
{
if (fclose($fd[0]) == false) {
ngx_log_error(NGX_LOG_ALERT, $log, socket_last_error(), "close() channel failed");
}
if (fclose($fd[1]) == false) {
ngx_log_error(NGX_LOG_ALERT, $log, socket_last_error(), "close() channel failed");
}
}
function ngx_write_channel( $s, ngx_channel_t $ch, ngx_log $log)
{
// ssize_t n;
// ngx_err_t err;
// struct iovec iov[1];
// struct msghdr msg;
$n = socket_sendmsg($s, get_object_vars($ch), 0);
if ($n == -1) {
$err = socket_last_error();
if ($err == NGX_EAGAIN) {
return NGX_AGAIN;
}
ngx_log_error(NGX_LOG_ALERT, $log, $err, "sendmsg() failed");
return NGX_ERROR;
}
return NGX_OK;
}
function ngx_add_channel_event(ngx_cycle_t $cycle, $fd, $event, callable $handler)
{
// ngx_event_t *ev, *rev, *wev;
// ngx_connection_t *c;
$c = ngx_get_connection($fd, $cycle->log);
if ($c == NULL) {
return NGX_ERROR;
}
//c->pool = cycle->pool;
$rev = $c->read;
$wev = $c->write;
$rev->log = $cycle->log;
$wev->log = $cycle->log;
$rev->channel = 1;
$wev->channel = 1;
$ev = ($event == NGX_READ_EVENT) ? $rev : $wev;
$ev->handler = $handler;
if (is_callable('ngx_add_conn') && (ngx_event_flags() & NGX_USE_EPOLL_EVENT) == 0) {
if (ngx_add_conn($c) == NGX_ERROR) {
ngx_free_connection($c);
return NGX_ERROR;
}
} else {
if (ngx_add_event($ev, $event, 0) == NGX_ERROR) {
ngx_free_connection($c);
return NGX_ERROR;
}
}
return NGX_OK;
}
function ngx_read_channel($s, ngx_channel_t $ch, ngx_log $log)
{
// ssize_t n;
// ngx_err_t err;
// struct iovec iov[1];
// struct msghdr msg;
// iov[0].iov_base = (char *) ch;
// iov[0].iov_len = size;
//
// msg.msg_name = NULL;
// msg.msg_namelen = 0;
// msg.msg_iov = iov;
// msg.msg_iovlen = 1;
//
//
// msg.msg_accrights = (caddr_t) &fd;
// msg.msg_accrightslen = sizeof(int);
$n = socket_recvmsg($s, $msg, 0);
if ($n == -1) {
$err = socket_last_error();
if ($err == NGX_EAGAIN) {
return NGX_AGAIN;
}
ngx_log_error(NGX_LOG_ALERT, $log, $err, "recvmsg() failed");
return NGX_ERROR;
}
if ($n == 0) {
ngx_log_debug0(NGX_LOG_DEBUG_CORE, $log, 0, "recvmsg() returned zero");
return NGX_ERROR;
}
// if ((size_t) n < sizeof(ngx_channel_t)) {
// ngx_log_error(NGX_LOG_ALERT, $log, 0,
// "recvmsg() returned not enough data: %z", n);
// return NGX_ERROR;
// }
if ($ch->command == NGX_CMD_OPEN_CHANNEL) {
// if (msg.msg_accrightslen != sizeof(int)) {
// ngx_log_error(NGX_LOG_ALERT, log, 0,
// "recvmsg() returned no ancillary data");
// return NGX_ERROR;
// }
$ch->fd = $msg;
}
return $n;
}