function connect_dest() { global $read_timeout, $write_timeout; echo "connect dest...\n"; global $base; $buffer = new EventBufferEvent($base, NULL, EventBufferEvent::OPT_CLOSE_ON_FREE | EventBufferEvent::OPT_DEFER_CALLBACKS, 'readcb', null, 'eventcb', $base); $buffer->enable(Event::READ); $buffer->setTimeouts($read_timeout, $write_timeout); $buffer->connect('127.0.0.1:8000'); $buffer->write('client data, ' . time()); }
function callback_accept($listener, $fd, $address, $base) { echo "accept running...\n"; $buffer = new EventBufferEvent($base, $fd, EventBufferEvent::OPT_CLOSE_ON_FREE); $buffer->setCallbacks('callback_read', 'callback_write', 'callback_event', $base); $buffer->setTimeouts(3, 3); $buffer->enable(Event::READ); $data = 'server.hello'; $length = strlen($data); $buffer->write($data); echo "write output buffer, data: server.hello({$length})\n"; }
/** * Send data and appending \n to connection. Note that it just writes to buffer flushed at every baseloop * @param string Data to send * @return boolean Success */ public function writeln($data) { if (!$this->alive) { Daemon::log('Attempt to write to dead IOStream (' . get_class($this) . ')'); return false; } if (!isset($this->bevWrite)) { return false; } if (!strlen($data) && !strlen($this->EOL)) { return true; } $this->writing = true; $this->bevWrite->write($data); $this->bevWrite->write($this->EOL); return true; }
function send_data() { global $count; global $url, $port; global $base; $pid = posix_getpid(); $rand = random(); $data = time() . "-{$rand}-{$pid}-{$count}"; $bev = new EventBufferEvent($base, null, EventBufferEvent::OPT_CLOSE_ON_FREE | EventBufferEvent::OPT_DEFER_CALLBACKS); $bev->setTimeouts(3, 3); $bev->setCallbacks('readcb', null, 'eventcb', $pid); $bev->enable(Event::READ | Event::WRITE); $bev->connect("{$url}:{$port}"); $bev->write($data . "\r\n"); $base->dispatch(); lg("[{$pid}] {$data}"); //lg("pid:{$pid},count:{$count}"); exit(0); }