├── .gitignore ├── .hhconfig ├── Definitions.hhi ├── Example.hh ├── LICENSE ├── README.md └── Server.hh /.gitignore: -------------------------------------------------------------------------------- 1 | .idea -------------------------------------------------------------------------------- /.hhconfig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/steelbrain/HHVM-Async-Server/72cccf49b034c2c6dfa0376958968f257336b740/.hhconfig -------------------------------------------------------------------------------- /Definitions.hhi: -------------------------------------------------------------------------------- 1 | ; 6 | -------------------------------------------------------------------------------- /Example.hh: -------------------------------------------------------------------------------- 1 | Server = $Server = new Server('localhost', 9001); 7 | } 8 | public async function OnConnect(resource $Client):Awaitable{ 9 | await Server::OnResponse($Client, async function(string $Data, resource $Client){ 10 | echo "Client Sent Data: $Data\n"; 11 | }); 12 | } 13 | public async function Init():Awaitable{ 14 | await $this->Server->ListenInCoop(inst_meth($this, 'OnConnect'), async function(Server $Instance):Awaitable{ 15 | $ShouldClose = rand(0, 1); 16 | if($ShouldClose === 1){ 17 | $Instance->Close(); 18 | } 19 | }); 20 | } 21 | } 22 | $App = new MyServerSidedApp(); 23 | $App->Init()->getWaitHandle()->join(); 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Steel Brain 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | HHVM-Async-Server 2 | ================= 3 | Async Socket Server Experiment in [HHVM][HHVM]'s [HackLang][HackLang]. it makes use of HackLang's built-in [async functions][async functions]. 4 | 5 | #### Usage 6 | 7 | ```hack 8 | $Server = new Server('localhost', 9098); 9 | $Server->Listen(async function(resource $Client){ 10 | await Server::OnResponse($Client, async function(string $Data, resource $Client){ 11 | echo "Client Sent Data: $Data\n"; 12 | }); 13 | }); 14 | ``` 15 | 16 | #### Usage.ServerOnly 17 | 18 | When You only want the server, You can do `$Server->Listen(...)->getWaitHandle()->join()`. This is a blocking operation, which means that It won't let the program get to the next line. It'll immediately start waiting for clients to connect to it. You can also store the wait handle in a variable and join it at the end of your program, like 19 | ```hack 20 | $WaitHandle = $Server->Listen(...)->getWaitHandle(); 21 | ... Do Stuff Here 22 | $WaitHandle->join(); 23 | ``` 24 | 25 | #### Usage.ServerSidedApp 26 | 27 | If you have a server sided app, then it of course does a lot of other stuff than just listening on an http port. In that case, You can execute two async functions and join them both, for example. 28 | ```hack 29 | class MyServerSidedApp{ 30 | public Server $Server; 31 | public function __construct(){ 32 | $this->Server = $Server = new Server('localhost', 9098); 33 | } 34 | public async function Init():Awaitable{ 35 | // Do the regular app stuff here.. 36 | } 37 | } 38 | $App = new MyServerSidedApp(); 39 | GenArrayWaitHandle::create([ 40 | $App->Server->Listen(async function(resource $Client){ 41 | await Server::OnResponse($Client, async function(string $Data, resource $Client){ 42 | echo "Client Sent Data: $Data\n"; 43 | }); 44 | })->getWaitHandle(), $App->Init()] 45 | )->join(); 46 | // This way your program and the HTTP Server and App will run co-currently 47 | ``` 48 | 49 | As you can see above, the code above looks un-clean, to fix that, Server class contains a method `ListenInCoop` just for this purpose. Here's how you can use it. 50 | 51 | ```hack 52 | class MyServerSidedApp{ 53 | public Server $Server; 54 | public function __construct(){ 55 | $this->Server = $Server = new Server('localhost', 9098); 56 | } 57 | public async function OnConnect(resource $Client):Awaitable{ 58 | await Server::OnResponse($Client, async function(string $Data, resource $Client){ 59 | echo "Client Sent Data: $Data\n"; 60 | }); 61 | } 62 | public async function Init():Awaitable{ 63 | await $this->Server->ListenInCoop(inst_meth($this, 'OnConnect'), async function(Server $Instance):Awaitable{ 64 | ... Do Stuff 65 | if(ShouldCloseServer()){ 66 | $Instance->Close(); 67 | } 68 | }); 69 | } 70 | } 71 | $App = new MyServerSidedApp(); 72 | $App->Init()->getWaitHandle()->join(); 73 | ``` 74 | 75 | #### License 76 | 77 | This Project is licensed under the terms of MIT License. 78 | 79 | [HHVM]:https://github.com/facebook/hhvm 80 | [HackLang]:http://hacklang.org 81 | [async functions]:http://docs.hhvm.com/manual/en/hack.async.php 82 | -------------------------------------------------------------------------------- /Server.hh: -------------------------------------------------------------------------------- 1 | Socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); 6 | register_shutdown_function(function() use ($Socket){ 7 | if(is_resource($Socket)) fclose($Socket); 8 | }); 9 | } 10 | public function Close():void{ 11 | fclose($this->Socket); 12 | } 13 | public async function Listen((function(resource):Awaitable) $Callback):Awaitable{ 14 | if(!is_resource($this->Socket)){ 15 | // It's closed... 16 | return ; 17 | } 18 | socket_bind($this->Socket, $this->Address, $this->Port); 19 | socket_listen($this->Socket, 100); 20 | $Handles = Vector{}; 21 | while(is_resource($this->Socket)){ 22 | try { 23 | $Handles->add($Callback(socket_accept($this->Socket))->getWaitHandle()); 24 | if($Handles->count() === 2){ 25 | await GenVectorWaitHandle::create($Handles); 26 | $Handles->clear(); 27 | } 28 | } catch(Exception $e){} // Let the server run! 29 | } 30 | } 31 | public static async function OnResponse(resource $Socket, (function(string, resource):Awaitable) $Callback):Awaitable{ 32 | while (is_resource($Socket) && !feof($Socket)){ 33 | $select = await stream_await($Socket, STREAM_AWAIT_READ, 5); 34 | switch ($select) { 35 | case STREAM_AWAIT_READY: 36 | $Data = stream_get_contents($Socket); 37 | if(!is_bool($Data)){ 38 | await $Callback($Data, $Socket); 39 | } 40 | break; 41 | case STREAM_AWAIT_CLOSED: 42 | return ; 43 | // Do nothing for timeouts 44 | default: 45 | } 46 | } 47 | } 48 | public async function ListenInCoop((function(resource):Awaitable) $First, (function(Server):Awaitable) $Second):Awaitable{ 49 | await GenArrayWaitHandle::create([$Second($this), $this->Listen($First)]); 50 | } 51 | } 52 | --------------------------------------------------------------------------------