|
@@ -21,11 +21,19 @@ class ImapClient { |
|
@@ -21,11 +21,19 @@ class ImapClient { |
21
|
*/
|
21
|
*/
|
22
|
public int $tagNum = 0;
|
22
|
public int $tagNum = 0;
|
23
|
|
23
|
|
|
|
24
|
+ /**
|
|
|
25
|
+ * 是否是非阻塞模式
|
|
|
26
|
+ * @var bool
|
|
|
27
|
+ */
|
|
|
28
|
+ public $isBlocking = false;
|
|
|
29
|
+
|
24
|
public function __construct(string $host){
|
30
|
public function __construct(string $host){
|
25
|
$this->host = $host;
|
31
|
$this->host = $host;
|
26
|
$this->socket = false;
|
32
|
$this->socket = false;
|
27
|
}
|
33
|
}
|
28
|
|
34
|
|
|
|
35
|
+
|
|
|
36
|
+
|
29
|
public function open(){
|
37
|
public function open(){
|
30
|
$content = stream_context_create([
|
38
|
$content = stream_context_create([
|
31
|
'ssl' => [
|
39
|
'ssl' => [
|
|
@@ -45,6 +53,9 @@ class ImapClient { |
|
@@ -45,6 +53,9 @@ class ImapClient { |
45
|
$content
|
53
|
$content
|
46
|
);
|
54
|
);
|
47
|
|
55
|
|
|
|
56
|
+ // 设置为非阻塞模式
|
|
|
57
|
+ $this->isBlocking = stream_set_blocking($this->socket, false);
|
|
|
58
|
+
|
48
|
if($error){
|
59
|
if($error){
|
49
|
throw new \Exception($error);
|
60
|
throw new \Exception($error);
|
50
|
}
|
61
|
}
|
|
@@ -90,7 +101,30 @@ class ImapClient { |
|
@@ -90,7 +101,30 @@ class ImapClient { |
90
|
* @time 2024/9/13 15:49
|
101
|
* @time 2024/9/13 15:49
|
91
|
*/
|
102
|
*/
|
92
|
protected function readLine(){
|
103
|
protected function readLine(){
|
93
|
- $str = fgets($this->socket,2048);
|
104
|
+ $i = 50; // 非阻塞模式下,只等待5秒,没返回数据就返回
|
|
|
105
|
+ $str = false;
|
|
|
106
|
+ while ($i>0){
|
|
|
107
|
+ $str = fgets($this->socket,2048);
|
|
|
108
|
+ // 非阻塞模式
|
|
|
109
|
+ if($this->isBlocking && $str === false){
|
|
|
110
|
+ $i--;
|
|
|
111
|
+ // 兼容下swoole
|
|
|
112
|
+ if(function_exists('go') && \co::getCid()>0){
|
|
|
113
|
+ \co::sleep(0.1);
|
|
|
114
|
+ }else{
|
|
|
115
|
+ // 等待 100 毫秒
|
|
|
116
|
+ usleep(100000);
|
|
|
117
|
+ }
|
|
|
118
|
+
|
|
|
119
|
+ }else{
|
|
|
120
|
+ break;
|
|
|
121
|
+ }
|
|
|
122
|
+ }
|
|
|
123
|
+ // 超时了,异常
|
|
|
124
|
+ if(!$i && $str===false){
|
|
|
125
|
+ throw new \Exception('read time out');
|
|
|
126
|
+ }
|
|
|
127
|
+
|
94
|
if($this->debug){
|
128
|
if($this->debug){
|
95
|
echo 'read: '.$str;
|
129
|
echo 'read: '.$str;
|
96
|
}
|
130
|
}
|