Skip to content

Commit 0f98b08

Browse files
committed
add eof testing
1 parent 8ce6ed5 commit 0f98b08

File tree

4 files changed

+28
-21
lines changed

4 files changed

+28
-21
lines changed

examples/client/async.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111

1212
class Cli extends \FastD\Swoole\Client
1313
{
14-
public function connect(swoole_client $client)
14+
public function onConnect(swoole_client $client)
1515
{
1616
$client->send('hello');
1717
}
1818

19-
public function receive(swoole_client $client, $data)
19+
public function onReceive(swoole_client $client, $data)
2020
{
2121
echo $data;
2222
$client->close();

examples/client/tcp.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
include __DIR__ . '/../../vendor/autoload.php';
44

55
$client = new \FastD\Swoole\Client('tcp://127.0.0.1:9527');
6-
6+
$client->configure([
7+
'open_eof_check' => true,
8+
'package_eof' => "\r\n",
9+
'package_max_length' => 1024 * 1024 * 2,
10+
]);
711
echo $client->send('hello');
812

913

examples/tcp/server.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
*/
1717
class DemoServer extends TCP
1818
{
19+
public function doConnect(swoole_server $server, $fd, $from_id)
20+
{
21+
parent::doConnect($server, $fd, $from_id);
22+
$server->send($fd, 'connect');
23+
}
24+
1925
public function doWork(swoole_server $server, $fd, $data, $from_id)
2026
{
2127
echo $fd;

src/Client.php

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212

1313
use FastD\Http\Cookie;
14+
use LogicException;
15+
use RuntimeException;
1416
use swoole_client;
1517
use swoole_http_client;
1618

@@ -109,7 +111,7 @@ public function createRequest($url, $async = false, $keep = false)
109111
$socketType = SWOOLE_SOCK_UDP;
110112
break;
111113
default:
112-
throw new \LogicException("Don't support schema ".$info['scheme']);
114+
throw new LogicException("Don't support schema ".$info['scheme']);
113115
}
114116

115117
$this->path = isset($info['path']) ? $info['path'] : '/';
@@ -274,43 +276,38 @@ public function configure($configure)
274276
* @param swoole_client $client
275277
* @return mixed
276278
*/
277-
public function connect(swoole_client $client)
278-
{
279-
}
279+
public function onConnect(swoole_client $client) {}
280280

281281
/**
282282
* @param swoole_client $client
283283
* @param string $data
284284
* @return mixed
285285
*/
286-
public function receive(swoole_client $client, $data)
287-
{
288-
}
286+
public function onReceive(swoole_client $client, $data) {}
289287

290288
/**
291289
* @param swoole_client $client
292290
* @return mixed
293291
*/
294-
public function error(swoole_client $client)
295-
{
296-
}
292+
public function onError(swoole_client $client) {}
297293

298294
/**
299295
* @param swoole_client $client
300296
* @return mixed
301297
*/
302-
public function close(swoole_client $client)
303-
{
304-
}
298+
public function onClose(swoole_client $client) {}
305299

306300
/**
307301
* @param string|array $data
308302
* @return mixed
309303
*/
310304
public function send($data = '')
311305
{
306+
if (null === $this->client) {
307+
throw new LogicException('Please call the createRequest method first');
308+
}
312309
if ( ! $this->client->connect($this->host, $this->port, $this->timeout)) {
313-
throw new \RuntimeException(socket_strerror($this->client->errCode));
310+
throw new RuntimeException(socket_strerror($this->client->errCode));
314311
}
315312

316313
$this->client->send($this->wrapBody($data));
@@ -326,16 +323,16 @@ public function send($data = '')
326323
public function start()
327324
{
328325
$this->client->on("connect", function ($client) {
329-
call_user_func_array([$this, 'connect'], [$client]);
326+
call_user_func_array([$this, 'onConnect'], [$client]);
330327
});
331328
$this->client->on("receive", function ($client, $data) {
332-
call_user_func_array([$this, 'receive'], [$client, $data]);
329+
call_user_func_array([$this, 'onReceive'], [$client, $data]);
333330
});
334331
$this->client->on("error", function ($client) {
335-
call_user_func_array([$this, 'error'], [$client]);
332+
call_user_func_array([$this, 'onError'], [$client]);
336333
});
337334
$this->client->on("close", function ($client) {
338-
call_user_func_array([$this, 'close'], [$client]);
335+
call_user_func_array([$this, 'onClose'], [$client]);
339336
});
340337
$this->client->connect($this->host, $this->port, $this->timeout);
341338
}

0 commit comments

Comments
 (0)