Skip to content

Commit a8404b7

Browse files
committed
Merge pull request #13 from thekid/libevent-server
Libevent server
2 parents b626540 + ed6212f commit a8404b7

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php namespace peer\server;
2+
3+
/**
4+
* Basic TCP/IP Server using libevent
5+
*
6+
* @ext event
7+
* @see http://pecl.php.net/package/event
8+
*/
9+
class EventServer extends Server {
10+
11+
/**
12+
* Service
13+
*/
14+
public function service() {
15+
if (!$this->socket->isConnected()) return false;
16+
17+
$base= new \EventBase();
18+
$listener= new \EventListener(
19+
$base,
20+
function($listener, $fd, $address, $base) {
21+
$event= new \EventBufferEvent($base, $fd, \EventBufferEvent::OPT_CLOSE_ON_FREE);
22+
$event->setCallbacks(
23+
function($event) {
24+
$this->protocol->handleData(new EventSocket($event));
25+
},
26+
function($event) {
27+
if (0 === $event->output->length) {
28+
$event->free();
29+
}
30+
},
31+
function($event, $events) {
32+
if ($events & (\EventBufferEvent::EOF | \EventBufferEvent::ERROR)) {
33+
$event->free();
34+
}
35+
},
36+
null
37+
);
38+
$event->enable(\Event::READ);
39+
},
40+
$base,
41+
\EventListener::OPT_CLOSE_ON_FREE | \EventListener::OPT_REUSEABLE,
42+
-1,
43+
$this->socket->getHandle()
44+
);
45+
46+
$base->dispatch();
47+
}
48+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php namespace peer\server;
2+
3+
use peer\Socket;
4+
5+
/**
6+
* Wrapper socket aroud EventBufferEvent
7+
*
8+
* @ext event
9+
* @see http://pecl.php.net/package/event
10+
*/
11+
class EventSocket extends \peer\Socket {
12+
protected $event= null;
13+
14+
/**
15+
* Constructor
16+
*
17+
* @param php.EventBufferEvent event
18+
*/
19+
public function __construct(\EventBufferEvent $event) {
20+
$this->event= $event;
21+
}
22+
23+
/**
24+
* Checks if EOF was reached
25+
*
26+
* @return bool
27+
*/
28+
public function eof() {
29+
return 0 === $this->event->input->length;
30+
}
31+
32+
/**
33+
* Read data from a socket (binary-safe)
34+
*
35+
* @param int maxLen maximum bytes to read
36+
* @return string data
37+
* @throws peer.SocketException
38+
*/
39+
public function readBinary($maxLen= 4096) {
40+
return $this->event->input->read($maxLen);
41+
}
42+
43+
/**
44+
* Read line from a socket
45+
*
46+
* @param int maxLen maximum bytes to read
47+
* @return string data
48+
* @throws peer.SocketException
49+
*/
50+
public function readLine($maxLen= 4096) {
51+
return $this->event->input->readLine(\EventBuffer::EOL_ANY);
52+
}
53+
54+
/**
55+
* Read data from a socket
56+
*
57+
* @param int maxLen maximum bytes to read
58+
* @return string data
59+
* @throws peer.SocketException
60+
*/
61+
public function read($maxLen= 4096) {
62+
return $this->event->input->read($maxLen);
63+
}
64+
65+
/**
66+
* Write a string to the socket
67+
*
68+
* @param string str
69+
* @return int bytes written
70+
* @throws peer.SocketException in case of an error
71+
*/
72+
public function write($data) {
73+
$this->event->output->add($data);
74+
return strlen($data);
75+
}
76+
77+
/**
78+
* Close socket
79+
*
80+
* @return bool success
81+
*/
82+
public function close() {
83+
return false;
84+
}
85+
}

0 commit comments

Comments
 (0)