Skip to content

Instantly share code, notes, and snippets.

@xeoncross
Forked from wez/sslclient.php
Created April 16, 2014 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xeoncross/10921198 to your computer and use it in GitHub Desktop.
Save xeoncross/10921198 to your computer and use it in GitHub Desktop.
class SSLSocket {
public $s = null;
public $on_err = null;
public $on_read = null;
public $on_readComplete = null;
public $on_timeout = null;
public $on_write = null;
public $endpoint = null;
public $wbuffer = array();
public $timer = null;
static $next_id = 0;
public $id = null;
static $by_id = array();
public $window = null;
function __construct($window, $endpoint) {
$this->window = $window;
$this->id = self::$next_id++;
$this->endpoint = $endpoint;
self::$by_id[$this->id] = $this;
}
function close() {
$this->trigger('readComplete');
$this->s = null;
if ($this->timer !== null) {
$this->window->clearTimeout($this->timer);
$this->timer = null;
}
}
function trigger() {
$args = func_get_args();
$name = 'on_' . array_shift($args);
$m = $this->$name;
$this->window->Titanium->API->runOnMainThread($m, $args, null);
}
function connect() {
if ($this->s !== null) {
throw new Exception("already connected");
}
$errno = null;
$errstr = null;
$this->s = stream_socket_client(
$this->endpoint,
$errno, $errstr,
60,
STREAM_CLIENT_CONNECT|STREAM_CLIENT_ASYNC_CONNECT
);
if ($this->s) {
stream_set_blocking($this->s, true);
$this->maintain();
return true;
}
$this->trigger('err', "$errno: $errstr");
return false;
}
static function maintain_by_id($id) {
$s = self::$by_id[$id];
if ($s instanceof SSLSocket) {
$s->maintain();
}
}
private function maintain() {
$this->timer = null;
$r = array($this->s);
$w = array($this->s);
stream_select($r, $w, $e = array(), 0);
if (count($r)) {
$data = fread($this->s, 8192);
if (strlen($data)) {
$this->trigger('read', $data);
}
if ($data === false) {
$this->trigger('err', 'read failed');
$this->close();
return;
}
}
if (count($w) + count($this->wbuffer)) {
$data = array_shift($this->wbuffer);
$x = fwrite($this->s, $data, min(strlen($data), 8192));
if ($x === false) {
$this->trigger('err', 'write failed');
$this->close();
return;
}
$nb = substr($data, 0, $x);
if (strlen($nb)) {
array_unshift($this->wbuffer, $nb);
}
}
$this->timer = $this->window->setTimeout(
"maintain_ssl_socket($this->id);", 100);
}
function isClosed() {
return $s === null;
}
function onError($handler) {
$this->on_err = $handler;
}
function onRead($handler) {
$this->on_read = $handler;
}
function onReadComplete($handler) {
$this->on_readComplete = $handler;
}
function onTimeout($handler) {
$this->on_timeout = $handler;
}
function onWrite($handler) {
$this->on_write = $handler;
}
function write($data) {
$this->wbuffer[] = $data;
}
}
function create_ssl_socket($window, $endpoint)
{
echo "creating_ssl_socket\n";
return new SSLSocket($window, $endpoint);
}
function maintain_ssl_socket($id) {
SSLSocket::maintain_by_id($id);
}
echo "sslclient.php was loaded ok\n";
<head>
<script type="text/javascript" src="jquery-1.3.2.js"></script>
<script type="text/php" src="sslclient.php"></script>
<script type="text/javascript">
$(document).ready(function() {
try {
var s = create_ssl_socket(window, 'ssl://imap.example.com:993');
s.onRead(function (data) {
alert("read: " + data);
});
s.onError(function (errmsg) {
alert("error: " + errmsg);
});
s.connect();
} catch(err) {
alert("error while creating socket" + err);
}
});
</script>
</head>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment