Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow defining socket options #46

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
},
"require": {
"php": ">=5.3",
"ext-sockets": "*",
skydiablo marked this conversation as resolved.
Show resolved Hide resolved
"evenement/evenement": "^3.0 || ^2.0 || ^1.0",
"react/event-loop": "^1.2",
"react/dns": "^1.7",
Expand Down
50 changes: 46 additions & 4 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,34 +48,76 @@ public function __construct(LoopInterface $loop = null, ResolverInterface $resol
$this->resolver = $resolver;
}

public function createClient($address)
public function createClient($address, array $socketOptions = array())
{
$loop = $this->loop;
$that = $this;

return $this->resolveAddress($address)->then(function ($address) use ($loop) {
return $this->resolveAddress($address)->then(function ($address) use ($loop, $socketOptions, $that) {
$socket = @\stream_socket_client($address, $errno, $errstr);
if (!$socket) {
throw new Exception('Unable to create client socket: ' . $errstr, $errno);
}
$that->assignSocketOptionsToStream($socket, $socketOptions);

return new Socket($loop, $socket);
});
}

public function createServer($address)
public function createServer($address, array $socketOptions = array())
{
$loop = $this->loop;
$that = $this;

return $this->resolveAddress($address)->then(function ($address) use ($loop) {
return $this->resolveAddress($address)->then(function ($address) use ($loop, $socketOptions, $that) {
$socket = @\stream_socket_server($address, $errno, $errstr, \STREAM_SERVER_BIND);
if (!$socket) {
throw new Exception('Unable to create server socket: ' . $errstr, $errno);
}
$that->assignSocketOptionsToStream($socket, $socketOptions);

return new Socket($loop, $socket);
});
}

/**
* Allows to set custom socket options on a stream resource. This is useful for
* setting options like `SO_REUSEADDR` or `SO_BROADCAST`. This method will
* throw an exception if the stream resource is not a socket resource.
* Example:
* ```php
* $factory->assignSocketOptionsToStream($socket, array(
* SOL_SOCKET => array( // SOL_SOCKET is the level
* SO_REUSEADDR => 1,
* SO_BROADCAST' => 1,
* SO_BINDTODEVICE => 'eth0',
* ),
* ));
* ```
* @param $stream
* @param $socketOptions
* @return void
* @throws Exception
* @internal
*/
public function assignSocketOptionsToStream($stream, $socketOptions) {
if(
$stream &&
$socketOptions &&
function_exists('socket_import_stream') &&
function_exists('socket_set_option'))
{
$socket = @socket_import_stream($stream);
foreach($socketOptions as $level => $options) {
foreach($options as $option => $value) {
if(!socket_set_option($socket, $level, $option, $value)) {
throw new Exception('Unable to set socket option: ' . socket_strerror(socket_last_error($socket)));
}
}
}
}
}

protected function resolveAddress($address)
{
if (\strpos($address, '://') === false) {
Expand Down