Skip to content

Commit

Permalink
Make sure HL7 sender honors timeout value
Browse files Browse the repository at this point in the history
  • Loading branch information
senaranya authored and Aranya Sen committed Mar 31, 2019
1 parent 25313c2 commit 3e35395
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/HL7/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
class Connection
{
protected $socket;
protected $timeout;
protected $MESSAGE_PREFIX;
protected $MESSAGE_SUFFIX;

Expand All @@ -51,6 +52,7 @@ public function __construct(string $host, int $port, int $timeout = 10)
$this->setSocket($host, $port, $timeout);
$this->MESSAGE_PREFIX = "\013";
$this->MESSAGE_SUFFIX = "\034\015";
$this->timeout = $timeout;
}

/**
Expand Down Expand Up @@ -123,11 +125,15 @@ public function send(Message $req, string $responseCharEncoding = 'UTF-8'): Mess

$data = null;

$startTime = time();
while (($buf = socket_read($this->socket, 1024)) !== false) { // Read ACK / NACK from server
$data .= $buf;
if (preg_match('/' . $this->MESSAGE_SUFFIX . '$/', $data)) {
break;
}
if ((time() - $startTime) > $this->timeout) {
throw new HL7ConnectionException("Timed out listening for response from server");
}
}

// Remove message prefix and suffix
Expand Down

0 comments on commit 3e35395

Please sign in to comment.