Skip to content

Commit

Permalink
upload 5th src
Browse files Browse the repository at this point in the history
  • Loading branch information
LinkedDestiny committed Nov 5, 2014
1 parent feb1ea1 commit 746d68a
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/05/client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

class Client
{
private $client;


public function __construct() {
$this->client = new swoole_client(SWOOLE_SOCK_TCP);
}

public function connect() {
if( !$this->client->connect("127.0.0.1", 9501 , 1) ) {
echo "Error: {$fp->errMsg}[{$fp->errCode}]\n";
}

$msg_normal = "This is a Msg";
$msg_eof = "This is a Msg\r\n";
$msg_length = pack("N" , strlen($msg_normal) ). $msg_normal;

$i = 0;
while( $i < 100 ) {
$this->client->send( $msg_length );
$i ++;
}
}
}

$client = new Client();
$client->connect();

51 changes: 51 additions & 0 deletions src/05/swoole_eof_server.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

class Server
{
private $serv;

public function __construct() {
$this->serv = new swoole_server("0.0.0.0", 9501);
$this->serv->set(array(
'worker_num' => 8,
'daemonize' => false,
'max_request' => 10000,
'dispatch_mode' => 2,
'package_max_length' => 8192,
'open_eof_check'=> true,
'package_eof' => "\r\n"
));

$this->serv->on('Start', array($this, 'onStart'));
$this->serv->on('Connect', array($this, 'onConnect'));
$this->serv->on('Receive', array($this, 'onReceive'));
$this->serv->on('Close', array($this, 'onClose'));

$this->serv->start();
}

public function onStart( $serv ) {
echo "Start\n";
}

public function onConnect( $serv, $fd, $from_id ) {
echo "Client {$fd} connect\n";

}

public function onReceive( swoole_server $serv, $fd, $from_id, $data ) {
$data_list = explode("\r\n", $data);
foreach ($data_list as $msg) {
if( !empty($msg) ) {
echo "Get Message From Client {$fd}:{$msg}\n";
}

}
}

public function onClose( $serv, $fd, $from_id ) {
echo "Client {$fd} close connection\n";
}
}

new Server();
33 changes: 33 additions & 0 deletions src/05/swoole_http_server.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

class Server
{
private $http;

public function __construct() {
$this->http = new swoole_http_server("127.0.0.1", 9501);

$this->http->set(
array(
'worker_num' => 16,
'daemonize' => false,
'max_request' => 10000,
'dispatch_mode' => 1
)
);

$this->http->on('Start', array($this, 'onStart'));
$this->http->on('request' , array( $this , 'onRequest'));
$this->http->start();
}

public function onStart( $serv ) {
echo "Start\n";
}

public function onRequest($request, $response) {
$response->end("<h1>Hello Swoole.</h1>");
}
}

new Server();
50 changes: 50 additions & 0 deletions src/05/swoole_length_check_server.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

class Server
{
private $serv;

public function __construct() {
$this->serv = new swoole_server("0.0.0.0", 9501);
$this->serv->set(array(
'worker_num' => 8,
'daemonize' => false,
'max_request' => 10000,
'dispatch_mode' => 2,
'package_max_length' => 8192,
'open_length_check'=> true,
'package_length_offset' => 0,
'package_body_offset' => 4,
'package_length_type' => 'N'
));

$this->serv->on('Start', array($this, 'onStart'));
$this->serv->on('Connect', array($this, 'onConnect'));
$this->serv->on('Receive', array($this, 'onReceive'));
$this->serv->on('Close', array($this, 'onClose'));

$this->serv->start();
}

public function onStart( $serv ) {
echo "Start\n";
}

public function onConnect( $serv, $fd, $from_id ) {
echo "Client {$fd} connect\n";

}

public function onReceive( swoole_server $serv, $fd, $from_id, $data ) {
$length = unpack("N" , $data)[1];
echo "Length = {$length}\n";
$msg = substr($data,-$length);
echo "Get Message From Client {$fd}:{$msg}\n";
}

public function onClose( $serv, $fd, $from_id ) {
echo "Client {$fd} close connection\n";
}
}

new Server();
42 changes: 42 additions & 0 deletions src/05/swoole_normal_server.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

class Server
{
private $serv;

public function __construct() {
$this->serv = new swoole_server("0.0.0.0", 9501);
$this->serv->set(array(
'worker_num' => 8,
'daemonize' => false,
'max_request' => 10000,
'dispatch_mode' => 2
));

$this->serv->on('Start', array($this, 'onStart'));
$this->serv->on('Connect', array($this, 'onConnect'));
$this->serv->on('Receive', array($this, 'onReceive'));
$this->serv->on('Close', array($this, 'onClose'));

$this->serv->start();
}

public function onStart( $serv ) {
echo "Start\n";
}

public function onConnect( $serv, $fd, $from_id ) {
echo "Client {$fd} connect\n";

}

public function onReceive( swoole_server $serv, $fd, $from_id, $data ) {
echo "Get Message From Client {$fd}:{$data}\n";
}

public function onClose( $serv, $fd, $from_id ) {
echo "Client {$fd} close connection\n";
}
}

new Server();

0 comments on commit 746d68a

Please sign in to comment.