Skip to content

Commit

Permalink
Docs, refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
kakserpom committed Mar 20, 2013
1 parent 64088c6 commit a7dab17
Show file tree
Hide file tree
Showing 9 changed files with 471 additions and 90 deletions.
15 changes: 12 additions & 3 deletions lib/BoundSocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ abstract class BoundSocket {
* Context
* @var mixed
*/
public $ctx; // @TODO: make it protected
protected $ctx;

/**
* URI
Expand Down Expand Up @@ -189,7 +189,7 @@ protected function initSSLContext() {
$this->errorneous = true;
return false;
}
$this->ctx = new EventSslContext(EventSslContext::SSLv3_SERVER_METHOD, $a = [
$this->ctx = new EventSslContext(EventSslContext::SSLv3_SERVER_METHOD, [
EventSslContext::OPT_LOCAL_CERT => $this->certfile,
EventSslContext::OPT_LOCAL_PK => $this->pkfile,
EventSslContext::OPT_PASSPHRASE => $this->passphrase,
Expand Down Expand Up @@ -240,6 +240,7 @@ public function enable() {
-1,
$this->fd
);
//EventUtil::getSocketName($this->ev, $this->locHost, $this->locPort);
} else {
$this->ev->enable();
}
Expand All @@ -262,6 +263,9 @@ public function onListenerAcceptEv($listener, $fd, $addrPort, $ctx) {
$conn = new $class(null, $this->pool);
$conn->setParentSocket($this);
$conn->setPeername($addrPort[0], $addrPort[1]);
if ($this->ctx) {
$conn->setContext($this->ctx, EventBufferEvent::SSL_ACCEPTING);
}
$conn->setFd($fd);
}

Expand Down Expand Up @@ -353,7 +357,12 @@ public function accept() {
}
socket_set_nonblock($fd);
$class = $this->pool->connectionClass;
return new $class($fd, $this->pool);
$conn = new $class(null, $this->pool);
if ($this->ctx) {
$conn->setContext($this->ctx, EventBufferEvent::SSL_ACCEPTING);
}
$conn->setFd($fd);
return $conn;
}

/**
Expand Down
133 changes: 113 additions & 20 deletions lib/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,45 +100,66 @@ class Connection extends IOStream {
protected $bevConnectEnabled = true;

/**
* bevConnect used?
* SSL?
* @var boolean
*/
protected $bevConnect = false;
protected $ssl = false;

/**
* URL
* @var string
*/
protected $url;

/**
* URI information
* @var hash
*/
protected $uri;

/**
* Scheme
* @var string
*/
protected $scheme;


/**
* Connected?
* @return boolean
* Private key file
* @var string
*/
public function isConnected() {
return $this->connected;
}
protected $pkfile;

/**
* Certificate file
* @var string
*/
protected $certfile;

/**
* Parses URL
* @return hash URL info.
* Passphrase
* @var string
*/
public function parseUrl($url) {
$u = Daemon_Config::parseCfgUri($url);
if (!$u) {
return false;
}
if (!isset($u['port']) && isset($this->pool->config->port->value)) {
$u['port'] = $this->pool->config->port->value;
}
return $u;
protected $passphrase;

/**
* Verify peer?
* @var boolean
*/
protected $verifypeer = false;

/**
* Allow self-signed?
* @var boolean
*/
protected $allowselfsigned = true;

/**
* Connected?
* @return boolean
*/
public function isConnected() {
return $this->connected;
}

/**
Expand Down Expand Up @@ -293,8 +314,68 @@ public function onConnected($cb) {
}
$this->onConnected->push($cb);
}
}

protected function importParams() {

foreach ($this->uri['params'] as $key => $val) {
if (isset($this->{$key}) && is_bool($this->{$key})) {
$this->{$key} = (bool) $val;
continue;
}
if (!property_exists($this, $key)) {
Daemon::log(get_class($this).': unrecognized setting \'' . $key . '\'');
continue;
}
$this->{$key} = $val;
}
if (!$this->ctxname) {
return;
}
if (!isset(Daemon::$config->{'TransportContext:' . $this->ctxname})) {
Daemon::log(get_class($this).': undefined transport context \'' . $this->ctxname . '\'');
return;
}
$ctx = Daemon::$config->{'TransportContext:' . $this->ctxname};
foreach ($ctx as $key => $entry) {
$value = ($entry instanceof Daemon_ConfigEntry) ? $entry->value : $entry;
if (isset($this->{$key}) && is_bool($this->{$key})) {
$this->{$key} = (bool) $value;
continue;
}
if (!property_exists($this, $key)) {
Daemon::log(get_class($this).': unrecognized setting in transport context \'' . $this->ctxname . '\': \'' . $key . '\'');
continue;
}
$this->{$key} = $value;
}

}

/**
* Initialize SSL context
* @return object|false Context
*/
protected function initSSLContext() {
if (!EventUtil::sslRandPoll()) {
Daemon::$process->log(get_class($this->pool) . ': EventUtil::sslRandPoll failed');
return false;
}
$params = [
EventSslContext::OPT_VERIFY_PEER => $this->verifypeer,
EventSslContext::OPT_ALLOW_SELF_SIGNED => $this->allowselfsigned,
];
if ($this->certfile !== null) {
$params[EventSslContext::OPT_LOCAL_CERT] = $this->certfile;
}
if ($this->pkfile !== null) {
$params[EventSslContext::OPT_LOCAL_PK] = $this->pkfile;
}
if ($this->passphrase !== null) {
$params[EventSslContext::OPT_PASSPHRASE] = $this->passphrase;
}
return new EventSslContext(EventSslContext::SSLv3_SERVER_METHOD, $params);
}


/**
* Connects to URL
Expand All @@ -303,10 +384,22 @@ public function onConnected($cb) {
* @return void
*/
public function connect($url, $cb = null) {
$u = $this->parseUrl($url);
$this->uri = Daemon_Config::parseCfgUri($url);
$u =& $this->uri;
if (!$u) {
return false;
}
if (!isset($u['port']) && isset($this->pool->config->port->value)) {
$u['port'] = $this->pool->config->port->value;
}
if (isset($u['user'])) {
$this->user = $u['user'];
}
$this->importParams();

if ($this->ssl) {
$this->setContext($this->initSSLContext(), EventBufferEvent::SSL_CONNECTING);
}

$this->url = $url;
$this->scheme = strtolower($u['scheme']);
Expand Down
2 changes: 1 addition & 1 deletion lib/Daemon_Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public static function init($configFile = null) {
}

if (isset(Daemon::$config->locale->value) && Daemon::$config->locale->value !== '') {
setlocale(LC_ALL,explode(',', Daemon::$config->locale->value));
setlocale(LC_ALL,array_map('trim', explode(',', Daemon::$config->locale->value)));
}

if (
Expand Down
Loading

0 comments on commit a7dab17

Please sign in to comment.