├── .gitignore ├── .scrutinizer.yml ├── .travis.yml ├── Examples └── ExampleIMAPClient.php ├── LICENSE ├── PHPDaemon ├── Applications │ ├── CGI.php │ ├── CGIRequest.php │ ├── FileReader.php │ ├── FileReaderRequest.php │ ├── GibsonREST │ │ ├── App.php │ │ └── Request.php │ ├── ServerStatus.php │ └── ServerStatusRequest.php ├── BoundSocket │ ├── Generic.php │ ├── TCP.php │ ├── UDP.php │ └── UNIX.php ├── Cache │ ├── CappedStorage.php │ ├── CappedStorageHits.php │ └── Item.php ├── Clients │ ├── AMQP │ │ ├── Channel.php │ │ ├── Connection.php │ │ ├── Driver │ │ │ ├── ConnectionOptions.php │ │ │ ├── Exception │ │ │ │ ├── AMQPChannelException.php │ │ │ │ ├── AMQPConnectionException.php │ │ │ │ ├── AMQPExchangeException.php │ │ │ │ ├── AMQPMessageException.php │ │ │ │ └── AMQPQueueException.php │ │ │ ├── Features.php │ │ │ ├── PackageInfo.php │ │ │ └── Protocol │ │ │ │ ├── ChannelInterface.php │ │ │ │ ├── CommandInterface.php │ │ │ │ ├── ConnectionInterface.php │ │ │ │ ├── DeliveryModeInterface.php │ │ │ │ ├── Exception │ │ │ │ ├── AMQPChannelExceptionInterface.php │ │ │ │ ├── AMQPConnectionExceptionInterface.php │ │ │ │ ├── AMQPException.php │ │ │ │ ├── AMQPExceptionInterface.php │ │ │ │ ├── AMQPExchangeExceptionInterface.php │ │ │ │ ├── AMQPMessageExceptionInterface.php │ │ │ │ ├── AMQPProtocolException.php │ │ │ │ ├── AMQPProtocolExceptionInterface.php │ │ │ │ └── AMQPQueueExceptionInterface.php │ │ │ │ ├── ExchangeInterface.php │ │ │ │ ├── ExchangeOptionsInterface.php │ │ │ │ ├── FeaturesInterface.php │ │ │ │ ├── MessageInterface.php │ │ │ │ ├── MessagePropertiesInterface.php │ │ │ │ ├── QueueInterface.php │ │ │ │ ├── QueueOptionsInterface.php │ │ │ │ └── v091 │ │ │ │ ├── DeliveryMode.php │ │ │ │ ├── ExchangeOptions.php │ │ │ │ ├── ExchangeType.php │ │ │ │ ├── Parser │ │ │ │ ├── Frame.php │ │ │ │ ├── FrameInterface.php │ │ │ │ ├── HeaderFrameParserTrait.php │ │ │ │ ├── MethodFrameParserTrait.php │ │ │ │ ├── ScalarParserTrait.php │ │ │ │ ├── Table.php │ │ │ │ └── TableInterface.php │ │ │ │ ├── Protocol │ │ │ │ ├── Access │ │ │ │ │ ├── AccessRequestFrame.php │ │ │ │ │ └── AccessRequestOkFrame.php │ │ │ │ ├── Basic │ │ │ │ │ ├── BasicAckFrame.php │ │ │ │ │ ├── BasicCancelFrame.php │ │ │ │ │ ├── BasicCancelOkFrame.php │ │ │ │ │ ├── BasicConsumeFrame.php │ │ │ │ │ ├── BasicConsumeOkFrame.php │ │ │ │ │ ├── BasicDeliverFrame.php │ │ │ │ │ ├── BasicGetEmptyFrame.php │ │ │ │ │ ├── BasicGetFrame.php │ │ │ │ │ ├── BasicGetOkFrame.php │ │ │ │ │ ├── BasicHeaderFrame.php │ │ │ │ │ ├── BasicNackFrame.php │ │ │ │ │ ├── BasicPublishFrame.php │ │ │ │ │ ├── BasicQosFrame.php │ │ │ │ │ ├── BasicQosOkFrame.php │ │ │ │ │ ├── BasicRecoverFrame.php │ │ │ │ │ ├── BasicRecoverOkFrame.php │ │ │ │ │ ├── BasicRejectFrame.php │ │ │ │ │ └── BasicReturnFrame.php │ │ │ │ ├── BodyFrame.php │ │ │ │ ├── Channel │ │ │ │ │ ├── ChannelCloseFrame.php │ │ │ │ │ ├── ChannelCloseOkFrame.php │ │ │ │ │ ├── ChannelFlowFrame.php │ │ │ │ │ ├── ChannelFlowOkFrame.php │ │ │ │ │ ├── ChannelOpenFrame.php │ │ │ │ │ └── ChannelOpenOkFrame.php │ │ │ │ ├── Confirm │ │ │ │ │ ├── ConfirmSelectFrame.php │ │ │ │ │ └── ConfirmSelectOkFrame.php │ │ │ │ ├── Connection │ │ │ │ │ ├── ConnectionBlockedFrame.php │ │ │ │ │ ├── ConnectionCloseFrame.php │ │ │ │ │ ├── ConnectionCloseOkFrame.php │ │ │ │ │ ├── ConnectionOpenFrame.php │ │ │ │ │ ├── ConnectionOpenOkFrame.php │ │ │ │ │ ├── ConnectionSecureFrame.php │ │ │ │ │ ├── ConnectionSecureOkFrame.php │ │ │ │ │ ├── ConnectionStartFrame.php │ │ │ │ │ ├── ConnectionStartOkFrame.php │ │ │ │ │ ├── ConnectionTuneFrame.php │ │ │ │ │ ├── ConnectionTuneOkFrame.php │ │ │ │ │ └── ConnectionUnblockedFrame.php │ │ │ │ ├── Constants.php │ │ │ │ ├── ContentFrameInterface.php │ │ │ │ ├── ContentPrecursorFrame.php │ │ │ │ ├── Endianness.php │ │ │ │ ├── Exchange │ │ │ │ │ ├── ExchangeBindFrame.php │ │ │ │ │ ├── ExchangeBindOkFrame.php │ │ │ │ │ ├── ExchangeDeclareFrame.php │ │ │ │ │ ├── ExchangeDeclareOkFrame.php │ │ │ │ │ ├── ExchangeDeleteFrame.php │ │ │ │ │ ├── ExchangeDeleteOkFrame.php │ │ │ │ │ ├── ExchangeUnbindFrame.php │ │ │ │ │ └── ExchangeUnbindOkFrame.php │ │ │ │ ├── FrameInterface.php │ │ │ │ ├── HeaderFrameInterface.php │ │ │ │ ├── HeartbeatFrame.php │ │ │ │ ├── IncomingFrame.php │ │ │ │ ├── MethodFrame.php │ │ │ │ ├── OutgoingFrame.php │ │ │ │ ├── Queue │ │ │ │ │ ├── QueueBindFrame.php │ │ │ │ │ ├── QueueBindOkFrame.php │ │ │ │ │ ├── QueueDeclareFrame.php │ │ │ │ │ ├── QueueDeclareOkFrame.php │ │ │ │ │ ├── QueueDeleteFrame.php │ │ │ │ │ ├── QueueDeleteOkFrame.php │ │ │ │ │ ├── QueuePurgeFrame.php │ │ │ │ │ ├── QueuePurgeOkFrame.php │ │ │ │ │ ├── QueueUnbindFrame.php │ │ │ │ │ └── QueueUnbindOkFrame.php │ │ │ │ └── Tx │ │ │ │ │ ├── TxCommitFrame.php │ │ │ │ │ ├── TxCommitOkFrame.php │ │ │ │ │ ├── TxRollbackFrame.php │ │ │ │ │ ├── TxRollbackOkFrame.php │ │ │ │ │ ├── TxSelectFrame.php │ │ │ │ │ └── TxSelectOkFrame.php │ │ │ │ ├── QueueOptions.php │ │ │ │ └── Serializer │ │ │ │ ├── Frame.php │ │ │ │ ├── FrameInterface.php │ │ │ │ ├── FrameSerializerTrait.php │ │ │ │ ├── ScalarSerializerTrait.php │ │ │ │ ├── Table.php │ │ │ │ └── TableInterface.php │ │ ├── Examples │ │ │ └── AMQPExample.php │ │ ├── Message.php │ │ ├── Pool.php │ │ ├── README_EN.md │ │ └── README_RU.md │ ├── Asterisk │ │ ├── Connection.php │ │ ├── ConnectionFinished.php │ │ └── Pool.php │ ├── DNS │ │ ├── Connection.php │ │ └── Pool.php │ ├── Gearman │ │ ├── Connection.php │ │ └── Pool.php │ ├── Gibson │ │ ├── Connection.php │ │ └── Pool.php │ ├── HTTP │ │ ├── Connection.php │ │ ├── Examples │ │ │ ├── Simple.php │ │ │ └── SimpleRequest.php │ │ └── Pool.php │ ├── ICMP │ │ ├── Connection.php │ │ └── Pool.php │ ├── IMAP │ │ ├── Connection.php │ │ └── Pool.php │ ├── IRC │ │ ├── Channel.php │ │ ├── ChannelParticipant.php │ │ ├── Connection.php │ │ └── Pool.php │ ├── Memcache │ │ ├── Connection.php │ │ └── Pool.php │ ├── Mongo │ │ ├── Collection.php │ │ ├── Connection.php │ │ ├── ConnectionFinished.php │ │ ├── Cursor.php │ │ ├── MongoId.php │ │ └── Pool.php │ ├── MySQL │ │ ├── Connection.php │ │ ├── ConnectionFinished.php │ │ └── Pool.php │ ├── PostgreSQL │ │ ├── Connection.php │ │ ├── ConnectionFinished.php │ │ └── Pool.php │ ├── Redis │ │ ├── AutoScan.php │ │ ├── Connection.php │ │ ├── Examples │ │ │ ├── Iterator.php │ │ │ ├── Multi.php │ │ │ ├── Scan.php │ │ │ ├── Simple.php │ │ │ └── SimpleRequest.php │ │ ├── Lock.php │ │ ├── MultiEval.php │ │ └── Pool.php │ ├── Valve │ │ ├── Connection.php │ │ └── Pool.php │ ├── WebSocket │ │ ├── Connection.php │ │ ├── Example.php │ │ └── Pool.php │ └── XMPP │ │ ├── Connection.php │ │ ├── Pool.php │ │ └── XMPPRoster.php ├── Config │ ├── Entry │ │ ├── ArraySet.php │ │ ├── Boolean.php │ │ ├── ConfigFile.php │ │ ├── Double.php │ │ ├── ExtFunc.php │ │ ├── Generic.php │ │ ├── Number.php │ │ ├── Size.php │ │ └── Time.php │ ├── Parser.php │ ├── Section.php │ └── _Object.php ├── Core │ ├── AppInstance.php │ ├── AppResolver.php │ ├── Bootstrap.php │ ├── CallbackWrapper.php │ ├── ClassFinder.php │ ├── ComplexJob.php │ ├── Daemon.php │ ├── Debug.php │ ├── DeferredEvent.php │ ├── EventLoop.php │ ├── Pool.php │ ├── ShellCommand.php │ ├── SyncWrapper.php │ ├── Timer.php │ └── TransportContext.php ├── DNode │ └── DNode.php ├── Examples │ ├── Example.php │ ├── ExampleAsteriskClient.php │ ├── ExampleAsyncProcess.php │ ├── ExampleAsyncProcessRequest.php │ ├── ExampleBroadcastCall.php │ ├── ExampleComplexJob.php │ ├── ExampleDNSClient.php │ ├── ExampleDNSClientRequest.php │ ├── ExampleDNode.php │ ├── ExampleFs.php │ ├── ExampleFsRequest.php │ ├── ExampleGibson.php │ ├── ExampleICMP.php │ ├── ExampleICMPRequest.php │ ├── ExampleIRCBot.php │ ├── ExampleJabberbot.php │ ├── ExamplePubSub.php │ ├── ExamplePubSubTestPageRequest.php │ ├── ExamplePubSubWebSocketRoute.php │ ├── ExampleRequest.php │ ├── ExampleSandbox.php │ ├── ExampleWebSocket.php │ ├── ExampleWithMemcache.php │ ├── ExampleWithMongo.php │ ├── ExampleWithMySQL.php │ ├── ExampleWithPostgreSQL.php │ ├── GameMonitor.php │ ├── MongoNode.php │ ├── TelnetHoneypot.php │ └── UDPEchoServer.php ├── Exceptions │ ├── ClearStack.php │ ├── ConnectionFinished.php │ ├── InfiniteRecursion.php │ ├── ProtocolError.php │ ├── UndefinedEventCalled.php │ ├── UndefinedMethodCalled.php │ ├── UndefinedPropertySetting.php │ └── UnsettingProperty.php ├── FS │ ├── File.php │ ├── FileSystem.php │ └── FileWatcher.php ├── HTTPRequest │ ├── Generic.php │ └── Input.php ├── IPCManager │ ├── IPCManager.php │ ├── MasterPool.php │ ├── MasterPoolConnection.php │ └── WorkerConnection.php ├── Network │ ├── Client.php │ ├── ClientConnection.php │ ├── Connection.php │ ├── IOStream.php │ ├── Pool.php │ └── Server.php ├── PubSub │ ├── PubSub.php │ └── PubSubEvent.php ├── Request │ ├── Generic.php │ ├── IRequestUpstream.php │ ├── RequestHeadersAlreadySent.php │ ├── RequestSleep.php │ └── RequestTerminated.php ├── Servers │ ├── DebugConsole │ │ ├── Connection.php │ │ └── Pool.php │ ├── FastCGI │ │ ├── Connection.php │ │ └── Pool.php │ ├── FlashPolicy │ │ ├── Connection.php │ │ └── Pool.php │ ├── HTTP │ │ ├── Connection.php │ │ └── Pool.php │ ├── IRCBouncer │ │ ├── Connection.php │ │ └── Pool.php │ ├── Ident │ │ ├── Connection.php │ │ └── Pool.php │ ├── Socks │ │ ├── Connection.php │ │ ├── Pool.php │ │ └── SlaveConnection.php │ └── WebSocket │ │ ├── Connection.php │ │ ├── Pool.php │ │ └── Protocols │ │ ├── V0.php │ │ ├── V13.php │ │ └── VE.php ├── SockJS │ ├── Application.php │ ├── Examples │ │ ├── Simple.php │ │ ├── SimpleRequest.php │ │ └── SimpleRoute.php │ ├── Methods │ │ ├── Eventsource.php │ │ ├── Generic.php │ │ ├── Htmlfile.php │ │ ├── Iframe.php │ │ ├── Info.php │ │ ├── Jsonp.php │ │ ├── JsonpSend.php │ │ ├── NotFound.php │ │ ├── Websocket.php │ │ ├── Welcome.php │ │ ├── Xhr.php │ │ ├── XhrSend.php │ │ └── XhrStreaming.php │ ├── Session.php │ ├── TestRelay │ │ ├── Application.php │ │ ├── Close.php │ │ └── EchoFeed.php │ ├── WebSocketConnectionProxy.php │ └── WebSocketRouteProxy.php ├── Structures │ ├── ObjectStorage.php │ ├── PriorityQueueCallbacks.php │ └── StackCallbacks.php ├── Thread │ ├── Collection.php │ ├── Generic.php │ ├── IPC.php │ ├── Master.php │ └── Worker.php ├── Traits │ ├── ClassWatchdog.php │ ├── DeferredEventHandlers.php │ ├── EventHandlers.php │ ├── EventLoopContainer.php │ ├── Sessions.php │ ├── StaticObjectWatchdog.php │ └── StrictStaticObjectWatchdog.php ├── Utils │ ├── Binary.php │ ├── Crypt.php │ ├── DateTime.php │ ├── Encoding.php │ ├── IRC.php │ ├── MIME.php │ ├── PPPDeflate.php │ ├── PlainJSON.php │ ├── ShmEntity.php │ ├── Terminal.php │ └── func.php ├── WebSocket │ ├── Route.php │ └── RouteInterface.php └── XMLStream │ ├── XMLStream.php │ └── XMLStreamObject.php ├── README.md ├── VERSION ├── bin ├── php-chroot ├── phpd └── sampleapp ├── composer.json ├── conf ├── AppResolver.php ├── conf.d │ ├── ExampleJabberBot.conf │ ├── FastCGI.conf │ ├── FlashpolicyServer.conf │ ├── HTTPServer.conf │ ├── IdentServer.conf │ ├── SSL-sample.conf │ └── WebSocketServer.conf ├── crossdomain.xml ├── logrotate ├── phpd-ircbnc.conf ├── phpd-jw.conf ├── phpd-sampleapp.conf └── phpd.conf.example ├── init-scripts ├── phpd ├── phpd.gentoo └── phpd.ubuntu ├── phpunit.xml.dist └── tests ├── AbstractTestCase.php ├── Clients └── Redis │ └── ConnectionTest.php ├── Utils ├── CryptTest.php └── DateTimeTest.php └── ci └── install.sh /.gitignore: -------------------------------------------------------------------------------- 1 | conf/phpd.conf 2 | doc/output 3 | /.project 4 | *.swp 5 | .idea 6 | vendor 7 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.6 5 | - 7.0 6 | - 7.1 7 | 8 | sudo: false 9 | 10 | cache: 11 | directories: 12 | - vendor 13 | - $HOME/.composer/cache 14 | 15 | install: 16 | - ./tests/ci/install.sh 17 | - composer self-update --quiet 18 | - COMPOSER_DISCARD_CHANGES=true composer --prefer-source update --no-interaction --no-progress 19 | 20 | script: 21 | - ./vendor/bin/phpcs --standard=PSR2 --report=emacs --extensions=php --warning-severity=0 --error-severity=6 PHPDaemon/ 22 | - ./vendor/bin/phpunit -v --debug --coverage-clover=coverage.clover 23 | -------------------------------------------------------------------------------- /Examples/ExampleIMAPClient.php: -------------------------------------------------------------------------------- 1 | '', 16 | 'login' => '', 17 | 'password' => '' 18 | ]; 19 | } 20 | 21 | /** 22 | * Called when the worker is ready to go. 23 | * @return void 24 | */ 25 | public function onReady() 26 | { 27 | \PHPDaemon\Clients\IMAP\Pool::getInstance()->open( 28 | $this->config->host->value, 29 | $this->config->login->value, 30 | $this->config->password->value, 31 | function ($conn) { 32 | if (!$conn) { 33 | $this->log('Fail to open IMAP connection'); 34 | return; 35 | } 36 | $this->log('open IMAP connection success'); 37 | $conn->getRawMessage( 38 | function ($conn, $isSuccess, $raw) { 39 | $this->log(print_r($raw, true)); 40 | $conn->logout(); 41 | }, 42 | 1, 43 | false 44 | ); 45 | }, 46 | true 47 | ); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /PHPDaemon/Applications/CGI.php: -------------------------------------------------------------------------------- 1 | '/usr/local/php/bin/php-cgi', 20 | 'php6' => '/usr/local/php6/bin/php-cgi', 21 | 'perl' => '/usr/bin/perl', 22 | 'python' => '/usr/local/bin/python', 23 | 'ruby' => '/usr/local/bin/ruby', 24 | ]; 25 | 26 | /** 27 | * @var string 28 | */ 29 | public $chroot = '/'; // default chroot 30 | 31 | /** 32 | * Called when the worker is ready to go. 33 | * @return void 34 | */ 35 | public function onReady() 36 | { 37 | } 38 | 39 | /** 40 | * Creates Request. 41 | * @param object $req Request. 42 | * @param object $upstream Upstream application instance. 43 | * @return CGIRequest Request. 44 | */ 45 | public function beginRequest($req, $upstream) 46 | { 47 | return new CGIRequest($this, $upstream, $req); 48 | } 49 | 50 | /** 51 | * Setting default config options 52 | * Overriden from AppInstance::getConfigDefaults 53 | * @return array|bool 54 | */ 55 | protected function getConfigDefaults() 56 | { 57 | return [ 58 | // @todo add description strings 59 | 'allow-override-binpath' => true, 60 | 'allow-override-cwd' => true, 61 | 'allow-override-chroot' => true, 62 | 'allow-override-user' => true, 63 | 'allow-override-group' => true, 64 | 'cwd' => null, 65 | 'output-errors' => true, 66 | 'errlog-file' => __DIR__ . '/cgi-error.log', 67 | ]; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /PHPDaemon/Applications/FileReader.php: -------------------------------------------------------------------------------- 1 | 9 | */ 10 | /** 11 | * Class FileReader 12 | * @package PHPDaemon\Applications 13 | */ 14 | class FileReader extends \PHPDaemon\Core\AppInstance 15 | { 16 | 17 | public $indexFiles; 18 | 19 | /** 20 | * Constructor. 21 | * @return void 22 | */ 23 | public function init() 24 | { 25 | $this->onConfigUpdated(); 26 | } 27 | 28 | /** 29 | * Update indexFiles field when config is updated 30 | */ 31 | public function onConfigUpdated() 32 | { 33 | $this->indexFiles = explode('/', $this->config->indexfiles->value); 34 | } 35 | 36 | /** 37 | * Creates Request. 38 | * @param object Request. 39 | * @param object Upstream application instance. 40 | * @return FileReaderRequest Request. 41 | */ 42 | public function beginRequest($req, $upstream) 43 | { 44 | return new \PHPDaemon\Applications\FileReaderRequest($this, $upstream, $req); 45 | } 46 | 47 | /** 48 | * Setting default config options 49 | * Overriden from AppInstance::getConfigDefaults 50 | * @return array|false 51 | */ 52 | protected function getConfigDefaults() 53 | { 54 | return [ 55 | // index file names 56 | 'indexfiles' => 'index.html/index.htm' 57 | ]; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /PHPDaemon/Applications/GibsonREST/App.php: -------------------------------------------------------------------------------- 1 | 9 | */ 10 | class App extends \PHPDaemon\Core\AppInstance 11 | { 12 | 13 | public $gibson; 14 | 15 | /** 16 | * Constructor. 17 | * @return void 18 | */ 19 | public function init() 20 | { 21 | $this->gibson = \PHPDaemon\Clients\Gibson\Pool::getInstance($this->config->gibsonname->value); 22 | } 23 | 24 | /** 25 | * Creates Request. 26 | * @param object Request. 27 | * @param object Upstream application instance. 28 | * @return Request Request. 29 | */ 30 | public function beginRequest($req, $upstream) 31 | { 32 | return new Request($this, $upstream, $req); 33 | } 34 | 35 | /** 36 | * Setting default config options 37 | * Overriden from AppInstance::getConfigDefaults 38 | * Uncomment and return array with your default options 39 | * @return array|false 40 | */ 41 | protected function getConfigDefaults() 42 | { 43 | return [ 44 | 'gibson-name' => '', 45 | 'auth' => 0, 46 | 'username' => 'admin', 47 | 'password' => 'gibson', 48 | 'credver' => 1, 49 | ]; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /PHPDaemon/Applications/ServerStatus.php: -------------------------------------------------------------------------------- 1 | 13 | */ 14 | class ServerStatusRequest extends Generic 15 | { 16 | 17 | /** 18 | * Called when request iterated. 19 | * @return integer Status. 20 | */ 21 | public function run() 22 | { 23 | $stime = microtime(true); 24 | $this->header('Content-Type: text/html; charset=utf-8'); 25 | ?> 26 | 28 | 29 | 30 | 31 | Server status. 32 | 33 | 34 |
Uptime: 36 |

State of workers: 38 |
Idle: 40 |
Busy: 42 |
Total alive: 44 |
Shutdown: 46 |
Pre-init: 48 |
Wait-init: 50 |
Init: 52 |
53 |
Request took: 55 | 56 | 57 | 8 | */ 9 | class CappedStorageHits extends CappedStorage 10 | { 11 | 12 | /** 13 | * Constructor 14 | * @param integer $max Maximum number of cached elements 15 | */ 16 | public function __construct($max = null) 17 | { 18 | if ($max !== null) { 19 | $this->maxCacheSize = $max; 20 | } 21 | $this->sorter = function ($a, $b) { 22 | if ($a->hits === $b->hits) { 23 | return 0; 24 | } 25 | return ($a->hits < $b->hits) ? 1 : -1; 26 | }; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /PHPDaemon/Cache/Item.php: -------------------------------------------------------------------------------- 1 | 10 | */ 11 | class Item 12 | { 13 | use \PHPDaemon\Traits\ClassWatchdog; 14 | use \PHPDaemon\Traits\StaticObjectWatchdog; 15 | 16 | /** 17 | * @var integer Hits counter 18 | */ 19 | public $hits = 1; 20 | /** 21 | * @var integer Expire time 22 | */ 23 | public $expire; 24 | /** 25 | * @var mixed Value 26 | */ 27 | protected $value; 28 | /** 29 | * @var StackCallbacks Listeners 30 | */ 31 | protected $listeners; 32 | 33 | /** 34 | * Constructor 35 | */ 36 | public function __construct($value) 37 | { 38 | $this->listeners = new StackCallbacks; 39 | $this->value = $value; 40 | } 41 | 42 | /** 43 | * Get hits number 44 | * @return integer 45 | */ 46 | public function getHits() 47 | { 48 | return $this->hits; 49 | } 50 | 51 | /** 52 | * Get value 53 | * @return mixed 54 | */ 55 | public function getValue() 56 | { 57 | ++$this->hits; 58 | return $this->value; 59 | } 60 | 61 | /** 62 | * Sets the value 63 | * @param mixed $value 64 | */ 65 | public function setValue($value) 66 | { 67 | $this->value = $value; 68 | $this->listeners->executeAll($this->value); 69 | } 70 | 71 | /** 72 | * Adds listener callback 73 | * @param callable $cb 74 | */ 75 | public function addListener($cb) 76 | { 77 | $this->listeners->push($cb); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Exception/AMQPChannelException.php: -------------------------------------------------------------------------------- 1 | realm = $realm; 33 | } 34 | if (null !== $exclusive) { 35 | $frame->exclusive = $exclusive; 36 | } 37 | if (null !== $passive) { 38 | $frame->passive = $passive; 39 | } 40 | if (null !== $active) { 41 | $frame->active = $active; 42 | } 43 | if (null !== $write) { 44 | $frame->write = $write; 45 | } 46 | if (null !== $read) { 47 | $frame->read = $read; 48 | } 49 | 50 | return $frame; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/Access/AccessRequestOkFrame.php: -------------------------------------------------------------------------------- 1 | deliveryTag = $deliveryTag; 30 | } 31 | if (null !== $multiple) { 32 | $frame->multiple = $multiple; 33 | } 34 | 35 | return $frame; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/Basic/BasicCancelFrame.php: -------------------------------------------------------------------------------- 1 | consumerTag = $consumerTag; 29 | } 30 | if (null !== $nowait) { 31 | $frame->nowait = $nowait; 32 | } 33 | 34 | return $frame; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/Basic/BasicCancelOkFrame.php: -------------------------------------------------------------------------------- 1 | consumerTag = $consumerTag; 28 | } 29 | 30 | return $frame; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/Basic/BasicConsumeFrame.php: -------------------------------------------------------------------------------- 1 | queue = $queue; 35 | } 36 | if (null !== $consumerTag) { 37 | $frame->consumerTag = $consumerTag; 38 | } 39 | if (null !== $noLocal) { 40 | $frame->noLocal = $noLocal; 41 | } 42 | if (null !== $noAck) { 43 | $frame->noAck = $noAck; 44 | } 45 | if (null !== $exclusive) { 46 | $frame->exclusive = $exclusive; 47 | } 48 | if (null !== $nowait) { 49 | $frame->nowait = $nowait; 50 | } 51 | if (null !== $arguments) { 52 | $frame->arguments = $arguments; 53 | } 54 | 55 | return $frame; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/Basic/BasicConsumeOkFrame.php: -------------------------------------------------------------------------------- 1 | consumerTag = $consumerTag; 28 | } 29 | 30 | return $frame; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/Basic/BasicDeliverFrame.php: -------------------------------------------------------------------------------- 1 | consumerTag = $consumerTag; 33 | } 34 | if (null !== $deliveryTag) { 35 | $frame->deliveryTag = $deliveryTag; 36 | } 37 | if (null !== $redelivered) { 38 | $frame->redelivered = $redelivered; 39 | } 40 | if (null !== $exchange) { 41 | $frame->exchange = $exchange; 42 | } 43 | if (null !== $routingKey) { 44 | $frame->routingKey = $routingKey; 45 | } 46 | 47 | return $frame; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/Basic/BasicGetEmptyFrame.php: -------------------------------------------------------------------------------- 1 | clusterId = $clusterId; 28 | } 29 | 30 | return $frame; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/Basic/BasicGetFrame.php: -------------------------------------------------------------------------------- 1 | queue = $queue; 30 | } 31 | if (null !== $noAck) { 32 | $frame->noAck = $noAck; 33 | } 34 | 35 | return $frame; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/Basic/BasicGetOkFrame.php: -------------------------------------------------------------------------------- 1 | deliveryTag = $deliveryTag; 33 | } 34 | if (null !== $redelivered) { 35 | $frame->redelivered = $redelivered; 36 | } 37 | if (null !== $exchange) { 38 | $frame->exchange = $exchange; 39 | } 40 | if (null !== $routingKey) { 41 | $frame->routingKey = $routingKey; 42 | } 43 | if (null !== $messageCount) { 44 | $frame->messageCount = $messageCount; 45 | } 46 | 47 | return $frame; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/Basic/BasicHeaderFrame.php: -------------------------------------------------------------------------------- 1 | deliveryTag = $deliveryTag; 31 | } 32 | if (null !== $multiple) { 33 | $frame->multiple = $multiple; 34 | } 35 | if (null !== $requeue) { 36 | $frame->requeue = $requeue; 37 | } 38 | 39 | return $frame; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/Basic/BasicPublishFrame.php: -------------------------------------------------------------------------------- 1 | exchange = $exchange; 33 | } 34 | if (null !== $routingKey) { 35 | $frame->routingKey = $routingKey; 36 | } 37 | if (null !== $mandatory) { 38 | $frame->mandatory = $mandatory; 39 | } 40 | if (null !== $immediate) { 41 | $frame->immediate = $immediate; 42 | } 43 | 44 | return $frame; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/Basic/BasicQosFrame.php: -------------------------------------------------------------------------------- 1 | prefetchSize = $prefetchSize; 30 | } 31 | if (null !== $prefetchCount) { 32 | $frame->prefetchCount = $prefetchCount; 33 | } 34 | if (null !== $global) { 35 | $frame->global = $global; 36 | } 37 | 38 | return $frame; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/Basic/BasicQosOkFrame.php: -------------------------------------------------------------------------------- 1 | requeue = $requeue; 28 | } 29 | 30 | return $frame; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/Basic/BasicRecoverOkFrame.php: -------------------------------------------------------------------------------- 1 | deliveryTag = $deliveryTag; 29 | } 30 | if (null !== $requeue) { 31 | $frame->requeue = $requeue; 32 | } 33 | 34 | return $frame; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/Basic/BasicReturnFrame.php: -------------------------------------------------------------------------------- 1 | replyCode = $replyCode; 31 | } 32 | if (null !== $replyText) { 33 | $frame->replyText = $replyText; 34 | } 35 | if (null !== $exchange) { 36 | $frame->exchange = $exchange; 37 | } 38 | if (null !== $routingKey) { 39 | $frame->routingKey = $routingKey; 40 | } 41 | 42 | return $frame; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/BodyFrame.php: -------------------------------------------------------------------------------- 1 | replyCode = $replyCode; 32 | } 33 | if (null !== $replyText) { 34 | $frame->replyText = $replyText; 35 | } 36 | if (null !== $classId) { 37 | $frame->classId = $classId; 38 | } 39 | if (null !== $methodId) { 40 | $frame->methodId = $methodId; 41 | } 42 | 43 | return $frame; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/Channel/ChannelCloseOkFrame.php: -------------------------------------------------------------------------------- 1 | active = $active; 29 | } 30 | 31 | return $frame; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/Channel/ChannelFlowOkFrame.php: -------------------------------------------------------------------------------- 1 | active = $active; 29 | } 30 | 31 | return $frame; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/Channel/ChannelOpenFrame.php: -------------------------------------------------------------------------------- 1 | outOfBand = $outOfBand; 28 | } 29 | 30 | return $frame; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/Channel/ChannelOpenOkFrame.php: -------------------------------------------------------------------------------- 1 | channelId = $channelId; 28 | } 29 | 30 | return $frame; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/Confirm/ConfirmSelectFrame.php: -------------------------------------------------------------------------------- 1 | nowait = $nowait; 28 | } 29 | 30 | return $frame; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/Confirm/ConfirmSelectOkFrame.php: -------------------------------------------------------------------------------- 1 | reason = $reason; 28 | } 29 | 30 | return $frame; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/Connection/ConnectionCloseFrame.php: -------------------------------------------------------------------------------- 1 | replyCode = $replyCode; 32 | } 33 | if (null !== $replyText) { 34 | $frame->replyText = $replyText; 35 | } 36 | if (null !== $classId) { 37 | $frame->classId = $classId; 38 | } 39 | if (null !== $methodId) { 40 | $frame->methodId = $methodId; 41 | } 42 | 43 | return $frame; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/Connection/ConnectionCloseOkFrame.php: -------------------------------------------------------------------------------- 1 | virtualHost = $virtualHost; 30 | } 31 | if (null !== $capabilities) { 32 | $frame->capabilities = $capabilities; 33 | } 34 | if (null !== $insist) { 35 | $frame->insist = $insist; 36 | } 37 | 38 | return $frame; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/Connection/ConnectionOpenOkFrame.php: -------------------------------------------------------------------------------- 1 | knownHosts = $knownHosts; 28 | } 29 | 30 | return $frame; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/Connection/ConnectionSecureFrame.php: -------------------------------------------------------------------------------- 1 | challenge = $challenge; 28 | } 29 | 30 | return $frame; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/Connection/ConnectionSecureOkFrame.php: -------------------------------------------------------------------------------- 1 | response = $response; 28 | } 29 | 30 | return $frame; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/Connection/ConnectionStartFrame.php: -------------------------------------------------------------------------------- 1 | versionMajor = $versionMajor; 32 | } 33 | if (null !== $versionMinor) { 34 | $frame->versionMinor = $versionMinor; 35 | } 36 | if (null !== $serverProperties) { 37 | $frame->serverProperties = $serverProperties; 38 | } 39 | if (null !== $mechanisms) { 40 | $frame->mechanisms = $mechanisms; 41 | } 42 | if (null !== $locales) { 43 | $frame->locales = $locales; 44 | } 45 | 46 | return $frame; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/Connection/ConnectionStartOkFrame.php: -------------------------------------------------------------------------------- 1 | clientProperties = $clientProperties; 31 | } 32 | if (null !== $mechanism) { 33 | $frame->mechanism = $mechanism; 34 | } 35 | if (null !== $response) { 36 | $frame->response = $response; 37 | } 38 | if (null !== $locale) { 39 | $frame->locale = $locale; 40 | } 41 | 42 | return $frame; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/Connection/ConnectionTuneFrame.php: -------------------------------------------------------------------------------- 1 | channelMax = $channelMax; 30 | } 31 | if (null !== $frameMax) { 32 | $frame->frameMax = $frameMax; 33 | } 34 | if (null !== $heartbeat) { 35 | $frame->heartbeat = $heartbeat; 36 | } 37 | 38 | return $frame; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/Connection/ConnectionTuneOkFrame.php: -------------------------------------------------------------------------------- 1 | channelMax = $channelMax; 30 | } 31 | if (null !== $frameMax) { 32 | $frame->frameMax = $frameMax; 33 | } 34 | if (null !== $heartbeat) { 35 | $frame->heartbeat = $heartbeat; 36 | } 37 | 38 | return $frame; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/Connection/ConnectionUnblockedFrame.php: -------------------------------------------------------------------------------- 1 | destination = $destination; 33 | } 34 | if (null !== $source) { 35 | $frame->source = $source; 36 | } 37 | if (null !== $routingKey) { 38 | $frame->routingKey = $routingKey; 39 | } 40 | if (null !== $nowait) { 41 | $frame->nowait = $nowait; 42 | } 43 | if (null !== $arguments) { 44 | $frame->arguments = $arguments; 45 | } 46 | 47 | return $frame; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/Exchange/ExchangeBindOkFrame.php: -------------------------------------------------------------------------------- 1 | exchange = $exchange; 31 | } 32 | if (null !== $ifUnused) { 33 | $frame->ifUnused = $ifUnused; 34 | } 35 | if (null !== $nowait) { 36 | $frame->nowait = $nowait; 37 | } 38 | 39 | return $frame; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/Exchange/ExchangeDeleteOkFrame.php: -------------------------------------------------------------------------------- 1 | destination = $destination; 33 | } 34 | if (null !== $source) { 35 | $frame->source = $source; 36 | } 37 | if (null !== $routingKey) { 38 | $frame->routingKey = $routingKey; 39 | } 40 | if (null !== $nowait) { 41 | $frame->nowait = $nowait; 42 | } 43 | if (null !== $arguments) { 44 | $frame->arguments = $arguments; 45 | } 46 | 47 | return $frame; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/Exchange/ExchangeUnbindOkFrame.php: -------------------------------------------------------------------------------- 1 | queue = $queue; 33 | } 34 | if (null !== $exchange) { 35 | $frame->exchange = $exchange; 36 | } 37 | if (null !== $routingKey) { 38 | $frame->routingKey = $routingKey; 39 | } 40 | if (null !== $nowait) { 41 | $frame->nowait = $nowait; 42 | } 43 | if (null !== $arguments) { 44 | $frame->arguments = $arguments; 45 | } 46 | 47 | return $frame; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/Queue/QueueBindOkFrame.php: -------------------------------------------------------------------------------- 1 | queue = $queue; 35 | } 36 | if (null !== $passive) { 37 | $frame->passive = $passive; 38 | } 39 | if (null !== $durable) { 40 | $frame->durable = $durable; 41 | } 42 | if (null !== $exclusive) { 43 | $frame->exclusive = $exclusive; 44 | } 45 | if (null !== $autoDelete) { 46 | $frame->autoDelete = $autoDelete; 47 | } 48 | if (null !== $nowait) { 49 | $frame->nowait = $nowait; 50 | } 51 | if (null !== $arguments) { 52 | $frame->arguments = $arguments; 53 | } 54 | 55 | return $frame; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/Queue/QueueDeclareOkFrame.php: -------------------------------------------------------------------------------- 1 | queue = $queue; 30 | } 31 | if (null !== $messageCount) { 32 | $frame->messageCount = $messageCount; 33 | } 34 | if (null !== $consumerCount) { 35 | $frame->consumerCount = $consumerCount; 36 | } 37 | 38 | return $frame; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/Queue/QueueDeleteFrame.php: -------------------------------------------------------------------------------- 1 | queue = $queue; 32 | } 33 | if (null !== $ifUnused) { 34 | $frame->ifUnused = $ifUnused; 35 | } 36 | if (null !== $ifEmpty) { 37 | $frame->ifEmpty = $ifEmpty; 38 | } 39 | if (null !== $nowait) { 40 | $frame->nowait = $nowait; 41 | } 42 | 43 | return $frame; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/Queue/QueueDeleteOkFrame.php: -------------------------------------------------------------------------------- 1 | messageCount = $messageCount; 28 | } 29 | 30 | return $frame; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/Queue/QueuePurgeFrame.php: -------------------------------------------------------------------------------- 1 | queue = $queue; 30 | } 31 | if (null !== $nowait) { 32 | $frame->nowait = $nowait; 33 | } 34 | 35 | return $frame; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/Queue/QueuePurgeOkFrame.php: -------------------------------------------------------------------------------- 1 | messageCount = $messageCount; 28 | } 29 | 30 | return $frame; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/Queue/QueueUnbindFrame.php: -------------------------------------------------------------------------------- 1 | queue = $queue; 32 | } 33 | if (null !== $exchange) { 34 | $frame->exchange = $exchange; 35 | } 36 | if (null !== $routingKey) { 37 | $frame->routingKey = $routingKey; 38 | } 39 | if (null !== $arguments) { 40 | $frame->arguments = $arguments; 41 | } 42 | 43 | return $frame; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Protocol/Queue/QueueUnbindOkFrame.php: -------------------------------------------------------------------------------- 1 | passive = (bool)$options['passive']; 57 | } 58 | if (array_key_exists('durable', $options)) { 59 | $this->durable = (bool)$options['durable']; 60 | } 61 | if (array_key_exists('exclusive', $options)) { 62 | $this->exclusive = (bool)$options['exclusive']; 63 | } 64 | if (array_key_exists('autoDelete', $options)) { 65 | $this->autoDelete = (bool)$options['autoDelete']; 66 | } 67 | if (array_key_exists('noWait', $options)) { 68 | $this->noWait = (bool)$options['noWait']; 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Serializer/Frame.php: -------------------------------------------------------------------------------- 1 | tableSerializer = $tableSerializer; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/AMQP/Driver/Protocol/v091/Serializer/FrameInterface.php: -------------------------------------------------------------------------------- 1 | setConnectionClass(Connection::class); 20 | parent::init(); 21 | } 22 | 23 | /** 24 | * Setting default config options 25 | * @return array 26 | */ 27 | protected function getConfigDefaults() 28 | { 29 | return [ 30 | 'host' => '127.0.0.1', 31 | 'port' => 5672, 32 | 'username' => 'guest', 33 | 'password' => 'guest', 34 | 'vhost' => '/', 35 | ]; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/Asterisk/ConnectionFinished.php: -------------------------------------------------------------------------------- 1 | (original code) 12 | * @author TyShkan (2.0) 13 | * @author Vasily Zorin (2.0) 14 | */ 15 | class ConnectionFinished extends \PHPDaemon\Exceptions\ConnectionFinished 16 | { 17 | } 18 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/Gearman/Pool.php: -------------------------------------------------------------------------------- 1 | 'tcp://127.0.0.1/', 23 | 'port' => 4730, 24 | 'maxconnperserv' => 32, 25 | ]; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/HTTP/Examples/Simple.php: -------------------------------------------------------------------------------- 1 | 12 | */ 13 | class Simple extends \PHPDaemon\Core\AppInstance 14 | { 15 | /** 16 | * @var Pool 17 | */ 18 | public $httpclient; 19 | 20 | /** 21 | * Constructor. 22 | * @return void 23 | */ 24 | public function init() 25 | { 26 | $this->httpclient = \PHPDaemon\Clients\HTTP\Pool::getInstance(); 27 | } 28 | 29 | /** 30 | * Creates Request. 31 | * @param object Request. 32 | * @param object Upstream application instance. 33 | * @return SimpleRequest Request. 34 | */ 35 | public function beginRequest($req, $upstream) 36 | { 37 | return new SimpleRequest($this, $upstream, $req); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/HTTP/Examples/SimpleRequest.php: -------------------------------------------------------------------------------- 1 | header('Content-Type: text/html'); 16 | } catch (\Exception $e) { 17 | } 18 | 19 | $this->appInstance->httpclient->get( 20 | 'http://www.google.com/robots.txt', 21 | function ($conn, $success) { 22 | echo $conn->body; 23 | $this->finish(); 24 | } 25 | ); 26 | 27 | // setting timeout 28 | $this->sleep(5, true); 29 | } 30 | 31 | /** 32 | * Called when request iterated 33 | * @return integer Status 34 | */ 35 | public function run() 36 | { 37 | echo 'Something went wrong.'; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/ICMP/Pool.php: -------------------------------------------------------------------------------- 1 | 10 | */ 11 | class Pool extends Client 12 | { 13 | 14 | /** 15 | * Establishes connection 16 | * @param string $host Address 17 | * @param callable $cb Callback 18 | * @callback $cb ( ) 19 | */ 20 | public function sendPing($host, $cb) 21 | { 22 | $this->connect('raw://' . $host, function ($conn) use ($cb) { 23 | $conn->sendEcho($cb); 24 | }); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/IMAP/Pool.php: -------------------------------------------------------------------------------- 1 | 143, 14 | 'sslport' => 993, 15 | ]; 16 | } 17 | 18 | public function open($host, $user, $pass, $cb, $ssl = true) 19 | { 20 | $params = compact('host', 'user', 'pass', 'cb', 'ssl'); 21 | $params['port'] = $params['ssl'] ? $this->config->sslport->value : $this->config->port->value; 22 | $dest = 'tcp://' . $params['host'] . ':' . $params['port'] . ($params['ssl'] ? '#ssl' : ''); 23 | $this->getConnection($dest, function ($conn) use ($params) { 24 | if (!$conn || !$conn->isConnected()) { 25 | call_user_func($params['cb'], false); 26 | return; 27 | } 28 | $conn->auth( 29 | function ($conn) use ($params) { 30 | if (!$conn) { 31 | call_user_func($params['cb'], false); 32 | return; 33 | } 34 | $conn->selectBox($params['cb']); 35 | }, 36 | $params['user'], 37 | $params['pass'] 38 | ); 39 | 40 | //TODO: startTls 41 | }); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/IRC/Pool.php: -------------------------------------------------------------------------------- 1 | 8 | */ 9 | class Pool extends \PHPDaemon\Network\Client 10 | { 11 | 12 | /** 13 | * @var Pool 14 | */ 15 | public $identd; 16 | 17 | /** 18 | * @var bool 19 | */ 20 | public $protologging = false; 21 | 22 | /** 23 | * @TODO DESCR 24 | */ 25 | public function onReady() 26 | { 27 | $this->identd = \PHPDaemon\Servers\Ident\Pool::getInstance(); 28 | parent::onReady(); 29 | } 30 | 31 | /** 32 | * Setting default config options 33 | * Overriden from NetworkClient::getConfigDefaults 34 | * @return array|bool 35 | */ 36 | protected function getConfigDefaults() 37 | { 38 | return [ 39 | /* [integer] Port */ 40 | 'port' => 6667, 41 | ]; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/Mongo/ConnectionFinished.php: -------------------------------------------------------------------------------- 1 | 8 | */ 9 | class ConnectionFinished extends \PHPDaemon\Exceptions\ConnectionFinished 10 | { 11 | } 12 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/MySQL/ConnectionFinished.php: -------------------------------------------------------------------------------- 1 | 8 | */ 9 | class ConnectionFinished extends \PHPDaemon\Exceptions\ConnectionFinished 10 | { 11 | } 12 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/PostgreSQL/Pool.php: -------------------------------------------------------------------------------- 1 | 'tcp://root@127.0.0.1', 17 | 18 | /* [integer] default port */ 19 | 'port' => 5432, 20 | 21 | /* [integer] @todo */ 22 | 'protologging' => 0, 23 | 24 | /* [integer] disabled by default */ 25 | 'enable' => 0 26 | ]; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/Redis/Examples/Multi.php: -------------------------------------------------------------------------------- 1 | 12 | */ 13 | class Multi extends \PHPDaemon\Core\AppInstance 14 | { 15 | /** 16 | * @var Pool 17 | */ 18 | public $redis; 19 | 20 | /** 21 | * Called when the worker is ready to go 22 | * @return void 23 | */ 24 | public function onReady() 25 | { 26 | $this->redis = \PHPDaemon\Clients\Redis\Pool::getInstance(); 27 | 28 | $this->redis->multi(function ($multi) { 29 | // "OK" 30 | D('start multi: ' . $multi->result); 31 | 32 | $multi->set('test1', 'value1', function ($redis) use ($multi) { 33 | // "QUEUED" 34 | D('in multi 1: ' . $redis->result); 35 | 36 | $this->redis->set('test1', 'value1-new', function ($redis) { 37 | // "OK", not "QUEUED" 38 | D('out multi 1: ' . $redis->result); 39 | }); 40 | 41 | setTimeout(function ($timer) use ($multi) { 42 | // "QUEUED" 43 | $multi->set('test2', 'value2', function ($redis) use ($multi) { 44 | D('in multi 2: ' . $redis->result); 45 | 46 | $multi->exec(function ($redis) { 47 | D('exec'); 48 | D($redis->result); 49 | }); 50 | }); 51 | $timer->free(); 52 | }, 2e5); 53 | }); 54 | }); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/Redis/Examples/Scan.php: -------------------------------------------------------------------------------- 1 | 12 | */ 13 | class Scan extends \PHPDaemon\Core\AppInstance 14 | { 15 | /** 16 | * @var Pool 17 | */ 18 | public $redis; 19 | 20 | /** 21 | * Called when the worker is ready to go 22 | * @return void 23 | */ 24 | public function onReady() 25 | { 26 | $this->redis = \PHPDaemon\Clients\Redis\Pool::getInstance(); 27 | 28 | $params = []; 29 | foreach (range(0, 100) as $i) { 30 | $params[] = 'myset' . $i; 31 | $params[] = 'value' . $i; 32 | } 33 | $params[] = function ($redis) { 34 | $params = [ 35 | function ($redis) { 36 | D('Count: ' . count($redis->result[1]) . '; Next: ' . $redis->result[0]); 37 | } 38 | ]; 39 | 40 | $cbEnd = function ($redis, $scan) { 41 | D('Full scan end!'); 42 | }; 43 | 44 | // test 1 45 | // $this->redis->scan(...$params); 46 | 47 | // test 2 48 | $this->redis->autoscan('scan', $params, $cbEnd, 50); 49 | }; 50 | $this->redis->mset(...$params); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/Redis/Examples/Simple.php: -------------------------------------------------------------------------------- 1 | 12 | */ 13 | class Simple extends \PHPDaemon\Core\AppInstance 14 | { 15 | /** 16 | * @var Pool 17 | */ 18 | public $redis; 19 | 20 | /** 21 | * Called when the worker is ready to go. 22 | * @return void 23 | */ 24 | public function onReady() 25 | { 26 | $this->redis = \PHPDaemon\Clients\Redis\Pool::getInstance(); 27 | 28 | /*$this->redis->eval("return {'a','b','c', {'d','e','f', {'g','h','i'}} }",0, function($redis) { 29 | Daemon::log(Debug::dump($redis->result)); 30 | });*/ 31 | 32 | $this->redis->subscribe('te3st', function ($redis) { 33 | Daemon::log(Debug::dump($redis->result)); 34 | }); 35 | $this->redis->psubscribe('test*', function ($redis) { 36 | Daemon::log(Debug::dump($redis->result)); 37 | 38 | }); 39 | } 40 | 41 | /** 42 | * Creates Request. 43 | * @param $req object Request. 44 | * @param $upstream IRequestUpstream Upstream application instance. 45 | * @return ExampleWithRedisRequest Request. 46 | */ 47 | public function beginRequest($req, $upstream) 48 | { 49 | return new SimpleRequest($this, $upstream, $req); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/WebSocket/Pool.php: -------------------------------------------------------------------------------- 1 | 11 | */ 12 | class Pool extends Network\Client 13 | { 14 | 15 | /** 16 | * Types of WebSocket frame 17 | */ 18 | const TYPE_TEXT = 'text'; 19 | const TYPE_BINARY = 'binary'; 20 | const TYPE_CLOSE = 'close'; 21 | const TYPE_PING = 'ping'; 22 | const TYPE_PONG = 'pong'; 23 | 24 | public function getConfigDefaults() 25 | { 26 | return [ 27 | /* [Size] Maximum allowed size of packet */ 28 | 'max-allowed-packet' => new Entry\Size('1M'), 29 | ]; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /PHPDaemon/Clients/XMPP/Pool.php: -------------------------------------------------------------------------------- 1 | 10 | */ 11 | class Pool extends Client 12 | { 13 | 14 | /** 15 | * Setting default config options 16 | * Overriden from NetworkClient::getConfigDefaults 17 | * @return array|bool 18 | */ 19 | protected function getConfigDefaults() 20 | { 21 | return [ 22 | /* [integer] Default port */ 23 | 'port' => 5222, 24 | ]; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /PHPDaemon/Config/Entry/ArraySet.php: -------------------------------------------------------------------------------- 1 | 12 | */ 13 | class ArraySet extends Generic 14 | { 15 | /** 16 | * Converts human-readable value to plain 17 | * @param array|string $value 18 | * @return array 19 | */ 20 | public static function humanToPlain($value) 21 | { 22 | if (is_array($value)) { 23 | return $value; 24 | } 25 | $value = preg_replace_callback('~(".*?")|(\'.*?\')|(\s*,\s*)~s', function ($m) { 26 | if (!empty($m[3])) { 27 | return "\x00"; 28 | } 29 | if (!empty($m[2])) { 30 | return substr($m[2], 1, -1); 31 | } 32 | if (!empty($m[1])) { 33 | return substr($m[1], 1, -1); 34 | } 35 | return null; 36 | }, $value); 37 | return explode("\x00", $value); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /PHPDaemon/Config/Entry/Boolean.php: -------------------------------------------------------------------------------- 1 | 11 | */ 12 | class Boolean extends Generic 13 | { 14 | /** 15 | * Converts human-readable value to plain 16 | * @param $value 17 | * @return bool 18 | */ 19 | public static function humanToPlain($value) 20 | { 21 | return (boolean)$value; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /PHPDaemon/Config/Entry/ConfigFile.php: -------------------------------------------------------------------------------- 1 | 14 | */ 15 | class ConfigFile extends Generic 16 | { 17 | 18 | /** 19 | * Called when entry is updated 20 | * @param $old 21 | * @return void 22 | */ 23 | public function onUpdate($old) 24 | { 25 | if (!Daemon::$process instanceof Master || (Daemon::$config->autoreload->value === 0) || !$old) { 26 | return; 27 | } 28 | 29 | $e = explode(';', $old); 30 | foreach ($e as $path) { 31 | Daemon::$process->fileWatcher->rmWatch($path, [Daemon::$process, 'sighup']); 32 | } 33 | 34 | $e = explode(';', $this->value); 35 | foreach ($e as $path) { 36 | Daemon::$process->fileWatcher->addWatch($path, [Daemon::$process, 'sighup']); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /PHPDaemon/Config/Entry/Double.php: -------------------------------------------------------------------------------- 1 | 11 | */ 12 | class Double extends Generic 13 | { 14 | /** 15 | * Converts human-readable value to plain 16 | * @param $value 17 | * @return double 18 | */ 19 | public static function humanToPlain($value) 20 | { 21 | return (double)$value; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /PHPDaemon/Config/Entry/ExtFunc.php: -------------------------------------------------------------------------------- 1 | 11 | */ 12 | class ExtFunc extends Generic 13 | { 14 | /** 15 | * Converts human-readable value to plain 16 | * @param $value 17 | * @return callable|null 18 | */ 19 | public static function humanToPlain($value) 20 | { 21 | $cb = include($value); 22 | return is_callable($cb) ? $cb : null; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /PHPDaemon/Config/Entry/Number.php: -------------------------------------------------------------------------------- 1 | 11 | */ 12 | class Number extends Generic 13 | { 14 | 15 | /** 16 | * Converts human-readable value to plain 17 | * @param $value 18 | * @return int|null 19 | */ 20 | public static function humanToPlain($value) 21 | { 22 | if ($value === null) { 23 | return null; 24 | } 25 | $l = substr($value, -1); 26 | 27 | if (($l === 'k') || ($l === 'K')) { 28 | return ((int)substr($value, 0, -1) * 1000); 29 | } 30 | 31 | if (($l === 'm') || ($l === 'M')) { 32 | return ((int)substr($value, 0, -1) * 1000 * 1000); 33 | } 34 | 35 | if (($l === 'g') || ($l === 'G')) { 36 | return ((int)substr($value, 0, -1) * 1000 * 1000 * 1000); 37 | } 38 | return (int)$value; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /PHPDaemon/Config/Entry/Size.php: -------------------------------------------------------------------------------- 1 | 11 | */ 12 | class Size extends Generic 13 | { 14 | 15 | /** 16 | * Converts human-readable value to plain 17 | * @param $value 18 | * @return int 19 | */ 20 | public static function humanToPlain($value) 21 | { 22 | $l = substr($value, -1); 23 | 24 | if ($l === 'b' || $l === 'B') { 25 | return ((int)substr($value, 0, -1)); 26 | } 27 | 28 | if ($l === 'k') { 29 | return ((int)substr($value, 0, -1) * 1000); 30 | } 31 | 32 | if ($l === 'K') { 33 | return ((int)substr($value, 0, -1) * 1024); 34 | } 35 | 36 | if ($l === 'm') { 37 | return ((int)substr($value, 0, -1) * 1000 * 1000); 38 | } 39 | 40 | if ($l === 'M') { 41 | return ((int)substr($value, 0, -1) * 1024 * 1024); 42 | } 43 | 44 | if ($l === 'g') { 45 | return ((int)substr($value, 0, -1) * 1000 * 1000 * 1000); 46 | } 47 | 48 | if ($l === 'G') { 49 | return ((int)substr($value, 0, -1) * 1024 * 1024 * 1024); 50 | } 51 | 52 | return (int)$value; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /PHPDaemon/Config/Entry/Time.php: -------------------------------------------------------------------------------- 1 | 11 | */ 12 | class Time extends Generic 13 | { 14 | 15 | /** 16 | * Converts human-readable value to plain 17 | * @param $value 18 | * @return int 19 | */ 20 | public static function humanToPlain($value) 21 | { 22 | $time = 0; 23 | 24 | preg_replace_callback('~(\d+(\.\d+)?)\s*([smhd])\s*|(.+)~i', function ($m) use (&$time) { 25 | if (isset($m[4]) && ($m[4] !== '')) { 26 | $time = false; 27 | } 28 | 29 | if ($time === false) { 30 | return; 31 | } 32 | 33 | if (!empty($m[2])) { 34 | $n = (float)$m[1]; 35 | } else { 36 | $n = (int)$m[1]; 37 | } 38 | 39 | $l = strtolower($m[3]); 40 | 41 | if ($l === 's') { 42 | $time += $n; 43 | } elseif ($l === 'm') { 44 | $time += $n * 60; 45 | } elseif ($l === 'h') { 46 | $time += $n * 60 * 60; 47 | } elseif ($l === 'd') { 48 | $time += $n * 60 * 60 * 24; 49 | } 50 | }, (string) $value); 51 | 52 | return $time; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /PHPDaemon/Core/SyncWrapper.php: -------------------------------------------------------------------------------- 1 | obj = $cb($this); 15 | } 16 | 17 | 18 | /** 19 | * Abstract call 20 | * @param $method 21 | * @param $args 22 | */ 23 | public function __call($method, $args) { 24 | $args[] = function($arg) use (&$ret) { 25 | $ret = $arg->result; 26 | EventLoop::$instance->stop(); 27 | }; 28 | $this->obj->$method(...$args); 29 | EventLoop::$instance->run(); 30 | return $ret; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /PHPDaemon/Core/TransportContext.php: -------------------------------------------------------------------------------- 1 | 8 | */ 9 | class TransportContext extends AppInstance 10 | { 11 | /** 12 | * Init 13 | * @return void 14 | */ 15 | public function init() 16 | { 17 | if ($this->isEnabled()) { 18 | } 19 | } 20 | 21 | /** 22 | * Setting default config options 23 | * Overriden from AppInstance::getConfigDefaults 24 | * Uncomment and return array with your default options 25 | * @return boolean 26 | */ 27 | protected function getConfigDefaults() 28 | { 29 | return false; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /PHPDaemon/Examples/Example.php: -------------------------------------------------------------------------------- 1 | 9 | */ 10 | class Example extends \PHPDaemon\Core\AppInstance 11 | { 12 | 13 | public $counter = 0; 14 | 15 | /** 16 | * Constructor. 17 | * @return void 18 | */ 19 | public function init() 20 | { 21 | } 22 | 23 | /** 24 | * Called when the worker is ready to go. 25 | * @return void 26 | */ 27 | public function onReady() 28 | { 29 | // Initialization. 30 | } 31 | 32 | /** 33 | * Called when application instance is going to shutdown. 34 | * @return boolean Ready to shutdown? 35 | */ 36 | public function onShutdown($graceful = false) 37 | { 38 | // Finalization. 39 | return true; 40 | } 41 | 42 | /** 43 | * Creates Request. 44 | * @param object Request. 45 | * @param object Upstream application instance. 46 | * @return ExampleRequest Request. 47 | */ 48 | public function beginRequest($req, $upstream) 49 | { 50 | return new ExampleRequest($this, $upstream, $req); 51 | } 52 | 53 | /** 54 | * Setting default config options 55 | * Overriden from AppInstance::getConfigDefaults 56 | * Uncomment and return array with your default options 57 | * @return boolean 58 | */ 59 | protected function getConfigDefaults() 60 | { 61 | return false; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /PHPDaemon/Examples/ExampleAsyncProcess.php: -------------------------------------------------------------------------------- 1 | 9 | */ 10 | class ExampleAsyncProcess extends \PHPDaemon\Core\AppInstance 11 | { 12 | 13 | /** 14 | * Creates Request. 15 | * @param object Request. 16 | * @param object Upstream application instance. 17 | * @return ExampleAsyncProcessRequest Request. 18 | */ 19 | public function beginRequest($req, $upstream) 20 | { 21 | return new ExampleAsyncProcessRequest($this, $upstream, $req); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /PHPDaemon/Examples/ExampleAsyncProcessRequest.php: -------------------------------------------------------------------------------- 1 | header('Content-Type: text/plain'); 18 | 19 | $this->proc = new \PHPDaemon\Core\ShellCommand(); 20 | $this->proc->onReadData(function ($stream, $data) { 21 | echo $data; 22 | }); 23 | $this->proc->onEOF(function ($stream) { 24 | $this->wakeup(); 25 | }); 26 | $this->proc->nice(256); 27 | $this->proc->execute('/bin/ls -l /tmp'); 28 | } 29 | 30 | public function onAbort() 31 | { 32 | if ($this->proc) { 33 | $this->proc->close(); 34 | } 35 | } 36 | 37 | public function onFinish() 38 | { 39 | $this->proc = null; 40 | } 41 | 42 | /** 43 | * Called when request iterated. 44 | * @return integer Status. 45 | */ 46 | public function run() 47 | { 48 | if (!$this->proc->eof()) { 49 | $this->sleep(1); 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /PHPDaemon/Examples/ExampleBroadcastCall.php: -------------------------------------------------------------------------------- 1 | 9 | */ 10 | class ExampleBroadcastCall extends \PHPDaemon\Core\AppInstance 11 | { 12 | 13 | public $enableRPC = true; 14 | 15 | public function hello($pid) 16 | { 17 | \PHPDaemon\Core\Daemon::$process->log('I got hello from ' . $pid . '!'); 18 | } 19 | 20 | public function onReady() 21 | { 22 | $appInstance = $this; 23 | 24 | setTimeout(function ($event) use ($appInstance) { 25 | 26 | $appInstance->broadcastCall('hello', [\PHPDaemon\Core\Daemon::$process->getPid()]); 27 | 28 | $event->finish(); 29 | 30 | }, 2e6); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /PHPDaemon/Examples/ExampleDNSClient.php: -------------------------------------------------------------------------------- 1 | 9 | */ 10 | class ExampleDNSClient extends \PHPDaemon\Core\AppInstance 11 | { 12 | /** 13 | * Creates Request. 14 | * @param object Request. 15 | * @param object Upstream application instance. 16 | * @return ExampleDNSClientRequest Request. 17 | */ 18 | public function beginRequest($req, $upstream) 19 | { 20 | return new \PHPDaemon\Examples\ExampleDNSClientRequest($this, $upstream, $req); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /PHPDaemon/Examples/ExampleDNSClientRequest.php: -------------------------------------------------------------------------------- 1 | job = new \PHPDaemon\Core\ComplexJob(function () use ($req) { // called when job is done 20 | $req->wakeup(); // wake up the request immediately 21 | 22 | }); 23 | 24 | $job('query', function ($name, $job) { // registering job named 'showvar' 25 | \PHPDaemon\Clients\DNS\Pool::getInstance()->get('phpdaemon.net', function ($response) use ($name, $job) { 26 | $job->setResult($name, $response); 27 | }); 28 | }); 29 | 30 | $job('resolve', function ($name, $job) { // registering job named 'showvar' 31 | \PHPDaemon\Clients\DNS\Pool::getInstance()->resolve('phpdaemon.net', function ($ip) use ($name, $job) { 32 | $job->setResult($name, ['phpdaemon.net resolved to' => $ip]); 33 | }); 34 | }); 35 | 36 | $job(); // let the fun begin 37 | 38 | $this->sleep(5, true); // setting timeout 39 | } 40 | 41 | /** 42 | * Called when request iterated. 43 | * @return integer Status. 44 | */ 45 | public function run() 46 | { 47 | try { 48 | $this->header('Content-Type: text/plain'); 49 | } catch (\Exception $e) { 50 | } 51 | var_dump($this->job->getResult('query')); 52 | var_dump($this->job->getResult('resolve')); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /PHPDaemon/Examples/ExampleFs.php: -------------------------------------------------------------------------------- 1 | 9 | */ 10 | class ExampleFs extends \PHPDaemon\Core\AppInstance 11 | { 12 | /** 13 | * Creates Request. 14 | * @param object Request. 15 | * @param object Upstream application instance. 16 | * @return ExampleFsRequest Request. 17 | */ 18 | public function beginRequest($req, $upstream) 19 | { 20 | return new \PHPDaemon\Examples\ExampleFsRequest($this, $upstream, $req); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /PHPDaemon/Examples/ExampleFsRequest.php: -------------------------------------------------------------------------------- 1 | sleep(1, true); 13 | \PHPDaemon\FS\FileSystem::readfile('/etc/filesystems', function ($file, $data) use ($req) { 14 | $req->fileData = $data; 15 | $req->wakeup(); 16 | }); 17 | } 18 | 19 | /** 20 | * Called when request iterated. 21 | * @return integer Status. 22 | */ 23 | public function run() 24 | { 25 | $this->header('Content-Type: text/plain'); 26 | echo "Contents of /etc/filesystems:\n" . $this->fileData; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /PHPDaemon/Examples/ExampleICMP.php: -------------------------------------------------------------------------------- 1 | 9 | */ 10 | class ExampleICMP extends \PHPDaemon\Core\AppInstance 11 | { 12 | /** 13 | * Creates Request. 14 | * @param object Request. 15 | * @param object Upstream application instance. 16 | * @return ExampleICMPRequest Request. 17 | */ 18 | public function beginRequest($req, $upstream) 19 | { 20 | return new \PHPDaemon\Examples\ExampleICMPRequest($this, $upstream, $req); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /PHPDaemon/Examples/ExampleICMPRequest.php: -------------------------------------------------------------------------------- 1 | job = new \PHPDaemon\Core\ComplexJob(function () use ($req) { // called when job is done 17 | 18 | $req->wakeup(); // wake up the request immediately 19 | 20 | }); 21 | 22 | $job('pingjob', function ($name, $job) use ($req) { // registering job named 'pingjob' 23 | 24 | \PHPDaemon\Clients\ICMP\Pool::getInstance()->sendPing('8.8.8.8', function ($latency) use ($name, $job) { 25 | $job->setResult($name, $latency); 26 | }); 27 | }); 28 | 29 | $job(); // let the fun begin 30 | 31 | $this->sleep(5, true); // setting timeout 32 | } 33 | 34 | /** 35 | * Called when request iterated. 36 | * @return integer Status. 37 | */ 38 | public function run() 39 | { 40 | $this->header('Content-Type: text/html'); 41 | ?> 43 | 44 | 45 | 46 | Ping 8.8.8.8 47 | 48 | 49 |

Latency to 8.8.8.8:

50 | job->getResult('pingjob'), 4) * 1000; 51 | ?> ms. 52 | 53 | header('Content-Type: text/html'); 16 | ?> 18 | 19 | 20 | 21 | WebSocket test page 22 | 23 | 24 | 42 | 43 | 44 | 45 |
46 | 47 | 48 | appInstance->pubsub->sub($req['event'], $this, function ($data) use ($ws, $eventName) { 21 | $ws->sendObject([ 22 | 'type' => 'event', 23 | 'event' => $eventName, 24 | 'data' => $data, 25 | ]); 26 | }); 27 | } 28 | } 29 | 30 | public function sendObject($obj) 31 | { 32 | $this->client->sendFrame(json_encode($obj), 'STRING'); 33 | } 34 | 35 | public function onFinish() 36 | { 37 | $this->appInstance->pubsub->unsubFromAll($this); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /PHPDaemon/Examples/UDPEchoServer.php: -------------------------------------------------------------------------------- 1 | 9 | */ 10 | class UDPEchoServer extends \PHPDaemon\Network\Server 11 | { 12 | 13 | public function onConfigUpdated() 14 | { 15 | parent::onConfigUpdated(); 16 | } 17 | 18 | /** 19 | * Setting default config options 20 | * Overriden from ConnectionPool::getConfigDefaults 21 | * @return array|false 22 | */ 23 | protected function getConfigDefaults() 24 | { 25 | return [ 26 | 'listen' => 'udp://0.0.0.0', 27 | 'port' => 1111, 28 | ]; 29 | } 30 | } 31 | 32 | class UDPEchoServerConnection extends \PHPDaemon\Network\Connection 33 | { 34 | /** 35 | * Called when UDP packet received. 36 | * @param string New data. 37 | * @return void 38 | */ 39 | public function onUdpPacket($pct) 40 | { 41 | $this->write('got: ' . $pct); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /PHPDaemon/Exceptions/ClearStack.php: -------------------------------------------------------------------------------- 1 | thread = $thread; 22 | } 23 | 24 | /** 25 | * Gets associated Thread object 26 | * @return object Thread 27 | */ 28 | public function getThread() 29 | { 30 | return $this->thread; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /PHPDaemon/Exceptions/ConnectionFinished.php: -------------------------------------------------------------------------------- 1 | 10 | */ 11 | class ProtocolError extends \Exception 12 | { 13 | } 14 | -------------------------------------------------------------------------------- /PHPDaemon/Exceptions/UndefinedEventCalled.php: -------------------------------------------------------------------------------- 1 | 9 | */ 10 | class MasterPool extends \PHPDaemon\Network\Server 11 | { 12 | /** @var array */ 13 | public $workers = []; 14 | /** @var string */ 15 | public $connectionClass = '\PHPDaemon\IPCManager\MasterPoolConnection'; 16 | } 17 | -------------------------------------------------------------------------------- /PHPDaemon/Request/IRequestUpstream.php: -------------------------------------------------------------------------------- 1 | 10 | */ 11 | class RequestHeadersAlreadySent extends \Exception 12 | { 13 | } 14 | -------------------------------------------------------------------------------- /PHPDaemon/Request/RequestSleep.php: -------------------------------------------------------------------------------- 1 | 'tcp://127.0.0.1', 20 | 21 | /* [integer] Listen port */ 22 | 'port' => 8818, 23 | 24 | /* [string] Password for auth */ 25 | 'passphrase' => 'secret', 26 | ]; 27 | } 28 | 29 | /** 30 | * Constructor 31 | * @return void 32 | */ 33 | protected function init() 34 | { 35 | Daemon::log('CAUTION: Danger! DebugConsole is up. Potential security breach.'); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /PHPDaemon/Servers/FlashPolicy/Connection.php: -------------------------------------------------------------------------------- 1 | 8 | */ 9 | class Connection extends \PHPDaemon\Network\Connection 10 | { 11 | /** 12 | * @var integer Length of "\x00" 13 | */ 14 | protected $lowMark = 23; 15 | 16 | /** 17 | * @var integer Length of "\x00" 18 | */ 19 | protected $highMark = 23; 20 | 21 | /** 22 | * Called when new data received 23 | * @return void 24 | */ 25 | public function onRead() 26 | { 27 | if (false === ($pct = $this->readExact($this->lowMark))) { 28 | return; // not readed yet 29 | } 30 | if ($pct === "\x00") { 31 | if ($this->pool->policyData) { 32 | $this->write($p = $this->pool->policyData . "\x00"); 33 | } else { 34 | $this->write("\x00"); 35 | } 36 | } 37 | $this->finish(); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /PHPDaemon/Servers/FlashPolicy/Pool.php: -------------------------------------------------------------------------------- 1 | '0.0.0.0', 26 | 27 | /* [integer] Listen port */ 28 | 'port' => 843, 29 | 30 | /* [string] Path to crossdomain.xml file */ 31 | 'file' => getcwd() . '/conf/crossdomain.xml', 32 | ]; 33 | } 34 | 35 | /** 36 | * Called when worker is ready 37 | * @return void 38 | */ 39 | public function onReady() 40 | { 41 | $this->onConfigUpdated(); 42 | } 43 | 44 | /** 45 | * Called when worker is going to update configuration 46 | * @return void 47 | */ 48 | public function onConfigUpdated() 49 | { 50 | parent::onConfigUpdated(); 51 | if (Daemon::$process instanceof \PHPDaemon\Thread\Worker) { 52 | $pool = $this; 53 | FileSystem::readfile($this->config->file->value, function ($file, $data) use ($pool) { 54 | $pool->policyData = $data; 55 | $pool->enable(); 56 | }); 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /PHPDaemon/Servers/Ident/Connection.php: -------------------------------------------------------------------------------- 1 | 8 | */ 9 | class Connection extends \PHPDaemon\Network\Connection 10 | { 11 | 12 | /** 13 | * @var string EOL 14 | */ 15 | protected $EOL = "\r\n"; 16 | 17 | /** 18 | * @var integer Default high mark. Maximum number of bytes in buffer. 19 | */ 20 | protected $highMark = 32; 21 | 22 | /** 23 | * Called when new data received. 24 | * @return void 25 | */ 26 | protected function onRead() 27 | { 28 | while (($line = $this->readline()) !== null) { 29 | $e = explode(' , ', $line); 30 | if ((sizeof($e) !== 2) || !ctype_digit($e[0]) || !ctype_digit($e[1])) { 31 | $this->writeln($line . ' : ERROR : INVALID-PORT'); 32 | $this->finish(); 33 | return; 34 | } 35 | $local = (int)$e[0]; 36 | $foreign = (int)$e[1]; 37 | if ($user = $this->pool->findPair($local, $foreign)) { 38 | $this->writeln($line . ' : USERID : ' . $user); 39 | } else { 40 | $this->writeln($line . ' : ERROR : NO-USER'); 41 | } 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /PHPDaemon/Servers/Socks/Pool.php: -------------------------------------------------------------------------------- 1 | 'tcp://0.0.0.0', 18 | 19 | /* [integer] Listen port */ 20 | 'port' => 1080, 21 | 22 | /* [boolean] Authentication required */ 23 | 'auth' => 0, 24 | 25 | /* [string] User name */ 26 | 'username' => 'User', 27 | 28 | /* [string] Password */ 29 | 'password' => 'Password', 30 | 31 | /* [string] Allowed clients ip list */ 32 | 'allowedclients' => null, 33 | ]; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /PHPDaemon/Servers/Socks/SlaveConnection.php: -------------------------------------------------------------------------------- 1 | client = $client; 19 | } 20 | 21 | /** 22 | * Called when new data received. 23 | * @return void 24 | */ 25 | public function onRead() 26 | { 27 | if (!$this->client) { 28 | return; 29 | } 30 | do { 31 | $this->client->writeFromBuffer($this->bev->input, $this->bev->input->length); 32 | } while ($this->bev->input->length > 0); 33 | } 34 | 35 | /** 36 | * Event of Connection 37 | * @return void 38 | */ 39 | public function onFinish() 40 | { 41 | if (isset($this->client)) { 42 | $this->client->finish(); 43 | } 44 | unset($this->client); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /PHPDaemon/SockJS/Examples/Simple.php: -------------------------------------------------------------------------------- 1 | attrs->server['DOCUMENT_URI']; 23 | 24 | if(mb_orig_strpos($uri, '/sockjspage/') === 0) { 25 | return 'SockJS\\Examples\\Simple'; 26 | } 27 | if(mb_orig_strpos($uri, '/sockjs/') === 0) { 28 | return 'SockJS\\Application'; 29 | } 30 | */ 31 | 32 | class Simple extends \PHPDaemon\Core\AppInstance 33 | { 34 | /** 35 | * Called when the worker is ready to go. 36 | * @return void 37 | */ 38 | public function onReady() 39 | { 40 | \PHPDaemon\Servers\WebSocket\Pool::getInstance()->addRoute( 41 | '/sockjs', 42 | function ($client) { 43 | return new SimpleRoute($client, $this); 44 | } 45 | ); 46 | } 47 | 48 | /** 49 | * Creates Request. 50 | * @param object Request. 51 | * @param object Upstream application instance. 52 | * @return SimpleRequest Request. 53 | */ 54 | public function beginRequest($req, $upstream) 55 | { 56 | return new SimpleRequest($this, $upstream, $req); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /PHPDaemon/SockJS/Examples/SimpleRequest.php: -------------------------------------------------------------------------------- 1 | header('Content-Type: text/html'); 15 | ?> 16 | 17 | 18 | 19 | SockJS test page 20 | 21 | 22 | 23 | 49 | 50 | 51 | 52 | 53 |
54 | 55 | 56 | client->sendFrame('pong'); 17 | } 18 | } 19 | 20 | /** 21 | * Uncaught exception handler 22 | * @param $e 23 | * @return boolean|null Handled? 24 | */ 25 | public function handleException($e) 26 | { 27 | $this->client->sendFrame('exception ...'); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /PHPDaemon/SockJS/Methods/Eventsource.php: -------------------------------------------------------------------------------- 1 | 8 | */ 9 | class Eventsource extends Generic 10 | { 11 | protected $contentType = 'text/event-stream'; 12 | protected $poll = true; 13 | protected $pollMode = ['stream']; 14 | protected $gcEnabled = true; 15 | 16 | /** 17 | * Send frame 18 | * @param string $frame 19 | * @return void 20 | */ 21 | public function sendFrame($frame) 22 | { 23 | $this->outputFrame('data: ' . $frame . "\r\n\r\n"); 24 | parent::sendFrame($frame); 25 | } 26 | 27 | /** 28 | * Constructor 29 | * @return void 30 | */ 31 | public function init() 32 | { 33 | parent::init(); 34 | if ($this->isFinished()) { 35 | return; 36 | } 37 | $this->out("\r\n", false); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /PHPDaemon/SockJS/Methods/Htmlfile.php: -------------------------------------------------------------------------------- 1 | 8 | */ 9 | class Htmlfile extends Generic 10 | { 11 | protected $gcEnabled = true; 12 | protected $contentType = 'text/html'; 13 | protected $callbackParamEnabled = true; 14 | protected $poll = true; 15 | protected $pollMode = ['stream']; 16 | 17 | /** 18 | * Send frame 19 | * @param string $frame 20 | * @return void 21 | */ 22 | public function sendFrame($frame) 23 | { 24 | $this->outputFrame("\r\n"); 26 | parent::sendFrame($frame); 27 | } 28 | 29 | /** 30 | * Constructor 31 | * @return void 32 | */ 33 | public function init() 34 | { 35 | parent::init(); 36 | if ($this->isFinished()) { 37 | return; 38 | } 39 | echo str_repeat(' ', 1024); 40 | echo "\n\n"; 41 | ?> 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 |

Don't panic!

50 | 63 | 64 | 65 | 10 | */ 11 | class Info extends Generic 12 | { 13 | protected $contentType = 'application/json'; 14 | 15 | /** 16 | * Constructor 17 | * @return void 18 | */ 19 | public function init() 20 | { 21 | parent::init(); 22 | Crypt::randomInts32(1, function ($ints) { 23 | $this->opts['entropy'] = $ints[0]; 24 | echo json_encode($this->opts); 25 | $this->finish(); 26 | }, 9); 27 | $this->sleep(5, true); 28 | } 29 | 30 | /** 31 | * Called when request iterated 32 | * @return void 33 | */ 34 | public function run() 35 | { 36 | $this->header('500 Server Too Busy'); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /PHPDaemon/SockJS/Methods/Jsonp.php: -------------------------------------------------------------------------------- 1 | 8 | */ 9 | class Jsonp extends Generic 10 | { 11 | protected $delayedStopEnabled = true; 12 | protected $contentType = 'application/javascript'; 13 | protected $callbackParamEnabled = true; 14 | protected $poll = true; 15 | protected $pollMode = ['one-by-one']; 16 | 17 | /** 18 | * Send frame 19 | * @param string $frame 20 | * @return void 21 | */ 22 | protected function sendFrame($frame) 23 | { 24 | $c = &$this->attrs->get['c']; 25 | if (!is_string($c)) { 26 | $this->header('400 Bad Request'); 27 | $this->finish(); 28 | return; 29 | } 30 | $this->outputFrame($c . '(' . json_encode($frame, JSON_UNESCAPED_SLASHES) . ");\r\n"); 31 | parent::sendFrame($frame); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /PHPDaemon/SockJS/Methods/NotFound.php: -------------------------------------------------------------------------------- 1 | 8 | */ 9 | class NotFound extends Generic 10 | { 11 | protected $contentType = 'text/plain'; 12 | 13 | /** 14 | * Constructor 15 | * @return void 16 | */ 17 | public function init() 18 | { 19 | $this->header('404 Not Found'); 20 | echo 'Not found'; 21 | $this->finish(); 22 | } 23 | 24 | /** 25 | * Called when request iterated 26 | * @return void 27 | */ 28 | public function run() 29 | { 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /PHPDaemon/SockJS/Methods/Websocket.php: -------------------------------------------------------------------------------- 1 | 8 | */ 9 | class Websocket extends Generic 10 | { 11 | protected $contentType = 'text/plain'; 12 | 13 | /** 14 | * Constructor 15 | * @return void 16 | */ 17 | public function init() 18 | { 19 | $this->header('426 Upgrade Required'); 20 | $this->finish(); 21 | } 22 | 23 | /** 24 | * Called when request iterated 25 | * @return void 26 | */ 27 | public function run() 28 | { 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /PHPDaemon/SockJS/Methods/Welcome.php: -------------------------------------------------------------------------------- 1 | 8 | */ 9 | class Welcome extends Generic 10 | { 11 | protected $contentType = 'text/plain'; 12 | protected $cacheable = true; 13 | 14 | /** 15 | * Constructor 16 | * @return void 17 | */ 18 | public function init() 19 | { 20 | parent::init(); 21 | $this->header('Cache-Control: max-age=31536000, public, pre-check=0, post-check=0'); 22 | $this->header('Expires: ' . date('r', strtotime('+1 year'))); 23 | echo "Welcome to SockJS!\n"; 24 | $this->finish(); 25 | } 26 | 27 | /** 28 | * Called when request iterated 29 | * @return void 30 | */ 31 | public function run() 32 | { 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /PHPDaemon/SockJS/Methods/Xhr.php: -------------------------------------------------------------------------------- 1 | 8 | */ 9 | class Xhr extends Generic 10 | { 11 | protected $delayedStopEnabled = true; 12 | protected $contentType = 'application/javascript'; 13 | protected $poll = true; 14 | protected $pollMode = ['one-by-one']; 15 | protected $allowedMethods = 'POST'; 16 | 17 | /** 18 | * Send frame 19 | * @param string $frame 20 | * @return void 21 | */ 22 | protected function sendFrame($frame) 23 | { 24 | $this->outputFrame($frame . "\n"); 25 | parent::sendFrame($frame); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /PHPDaemon/SockJS/Methods/XhrSend.php: -------------------------------------------------------------------------------- 1 | 8 | */ 9 | class XhrSend extends Generic 10 | { 11 | protected $contentType = 'text/plain'; 12 | protected $allowedMethods = 'POST'; 13 | 14 | /** 15 | * Called when request iterated 16 | * @return void 17 | */ 18 | public function run() 19 | { 20 | if ($this->stage++ > 0) { 21 | $this->header('500 Too Busy'); 22 | return; 23 | } 24 | if ($this->attrs->raw === '') { 25 | $this->header('500 Internal Server Error'); 26 | echo 'Payload expected.'; 27 | return; 28 | } 29 | if (!json_decode($this->attrs->raw, true)) { 30 | $this->header('500 Internal Server Error'); 31 | echo 'Broken JSON encoding.'; 32 | return; 33 | } 34 | $this->appInstance->publish('c2s:' . $this->sessId, $this->attrs->raw, function ($redis) { 35 | if (!$this->headers_sent) { 36 | if ($redis->result === 0) { 37 | $this->header('404 Not Found'); 38 | } else { 39 | $this->header('204 No Content'); 40 | } 41 | } 42 | $this->finish(); 43 | }); 44 | $this->sleep(10); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /PHPDaemon/SockJS/Methods/XhrStreaming.php: -------------------------------------------------------------------------------- 1 | 8 | */ 9 | class XhrStreaming extends Generic 10 | { 11 | protected $gcEnabled = true; 12 | protected $contentType = 'application/javascript'; 13 | protected $fillerEnabled = true; 14 | protected $poll = true; 15 | protected $pollMode = ['stream']; 16 | protected $allowedMethods = 'POST'; 17 | 18 | /** 19 | * afterHeaders 20 | * @return void 21 | */ 22 | public function afterHeaders() 23 | { 24 | $this->sendFrame(str_repeat('h', 2048)); 25 | $this->bytesSent = 0; 26 | } 27 | 28 | /** 29 | * Send frame 30 | * @param string $frame 31 | * @return void 32 | */ 33 | protected function sendFrame($frame) 34 | { 35 | $this->outputFrame($frame . "\n"); 36 | parent::sendFrame($frame); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /PHPDaemon/SockJS/TestRelay/Application.php: -------------------------------------------------------------------------------- 1 | 10 | */ 11 | class Application extends \PHPDaemon\Core\AppInstance 12 | { 13 | /** 14 | * Called when the worker is ready to go. 15 | * @return void 16 | */ 17 | public function onReady() 18 | { 19 | $ws = WebSocketPool::getInstance($this->config->wssname->value); 20 | $ws->addRoute('close', function ($client) { 21 | return new Close($client, $this); 22 | }); 23 | 24 | $ws->addRoute('echo', function ($client) { 25 | return new EchoFeed($client, $this); 26 | }); 27 | 28 | $ws->addRoute('disabled_websocket_echo', function ($client) { 29 | return new EchoFeed($client, $this); 30 | }); 31 | $ws->setRouteOptions('disabled_websocket_echo', ['websocket' => false]); 32 | 33 | $ws->addRoute('cookie_needed_echo', function ($client) { 34 | return new EchoFeed($client, $this); 35 | }); 36 | $ws->setRouteOptions('cookie_needed_echo', ['cookie_needed' => true]); 37 | } 38 | 39 | /** 40 | * Setting default config options 41 | * @return array|bool 42 | */ 43 | protected function getConfigDefaults() 44 | { 45 | return [ 46 | /* [string] WSS name */ 47 | 'wss-name' => '', 48 | ]; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /PHPDaemon/SockJS/TestRelay/Close.php: -------------------------------------------------------------------------------- 1 | 8 | */ 9 | class Close extends \PHPDaemon\WebSocket\Route 10 | { 11 | /** 12 | * Called when the connection is handshaked. 13 | * @return void 14 | */ 15 | public function onHandshake() 16 | { 17 | $this->client->finish(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /PHPDaemon/SockJS/TestRelay/EchoFeed.php: -------------------------------------------------------------------------------- 1 | 8 | */ 9 | class EchoFeed extends \PHPDaemon\WebSocket\Route 10 | { 11 | /** 12 | * Called when new frame received 13 | * @param string $data Frame's contents 14 | * @param integer $type Frame's type 15 | * @return void 16 | */ 17 | public function onFrame($data, $type) 18 | { 19 | $this->client->sendFrame($data); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /PHPDaemon/Structures/ObjectStorage.php: -------------------------------------------------------------------------------- 1 | 8 | */ 9 | class ObjectStorage extends \SplObjectStorage 10 | { 11 | use \PHPDaemon\Traits\ClassWatchdog; 12 | use \PHPDaemon\Traits\StaticObjectWatchdog; 13 | 14 | /** 15 | * Call given method of all objects in storage 16 | * @param string $method Method name 17 | * @param mixed ...$args Arguments 18 | * @return integer Number of called objects 19 | */ 20 | public function each($method, ...$args) 21 | { 22 | if ($this->count() === 0) { 23 | return 0; 24 | } 25 | $n = 0; 26 | foreach ($this as $obj) { 27 | $obj->$method(...$args); 28 | ++$n; 29 | } 30 | return $n; 31 | } 32 | 33 | /** 34 | * Remove all objects from this storage, which contained in another storage 35 | * @param \SplObjectStorage $obj 36 | * @return void 37 | */ 38 | public function removeAll($obj = null): int 39 | { 40 | if ($obj === null) { 41 | $this->removeAllExcept(new \SplObjectStorage); 42 | } 43 | parent::removeAll($obj); 44 | } 45 | 46 | /** 47 | * Detaches first object and returns it 48 | * @return object 49 | */ 50 | public function detachFirst() 51 | { 52 | $this->rewind(); 53 | $o = $this->current(); 54 | if (!$o) { 55 | return false; 56 | } 57 | $this->detach($o); 58 | return $o; 59 | } 60 | 61 | /** 62 | * Returns first object 63 | * @return object 64 | */ 65 | public function getFirst() 66 | { 67 | $this->rewind(); 68 | return $this->current(); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /PHPDaemon/Traits/ClassWatchdog.php: -------------------------------------------------------------------------------- 1 | 10 | */ 11 | trait ClassWatchdog 12 | { 13 | /** 14 | * @param string $method Method name 15 | * @param array $args Arguments 16 | * @throws UndefinedMethodCalled if call to undefined static method 17 | * @return mixed 18 | */ 19 | public static function __callStatic($method, $args) 20 | { 21 | throw new UndefinedMethodCalled('Call to undefined static method ' . static::class . '::' . $method); 22 | } 23 | 24 | /** 25 | * @param string $method Method name 26 | * @param array $args Arguments 27 | * @throws UndefinedMethodCalled if call to undefined method 28 | * @return mixed 29 | */ 30 | public function __call($method, $args) 31 | { 32 | throw new UndefinedMethodCalled('Call to undefined method ' . get_class($this) . '->' . $method); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /PHPDaemon/Traits/EventLoopContainer.php: -------------------------------------------------------------------------------- 1 | eventLoop = $eventLoop; 19 | return $this; 20 | } 21 | } -------------------------------------------------------------------------------- /PHPDaemon/Traits/StaticObjectWatchdog.php: -------------------------------------------------------------------------------- 1 | 11 | */ 12 | trait StaticObjectWatchdog 13 | { 14 | /** 15 | * @param string $prop 16 | * @param mixed $value 17 | * @return void 18 | */ 19 | public function __set($prop, $value) 20 | { 21 | Daemon::log('[CODE WARN] Setting undefined property ' . json_encode($prop) . ' in object of class ' . get_class($this) . PHP_EOL . Debug::backtrace()); 22 | $this->{$prop} = $value; 23 | } 24 | 25 | /** 26 | * @param string $prop 27 | * @return void 28 | */ 29 | public function __unset($prop) 30 | { 31 | Daemon::log('[CODE WARN] Unsetting property ' . json_encode($prop) . ' in object of class ' . get_class($this) . PHP_EOL . Debug::backtrace()); 32 | unset($this->{$prop}); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /PHPDaemon/Traits/StrictStaticObjectWatchdog.php: -------------------------------------------------------------------------------- 1 | 8 | */ 9 | trait StrictStaticObjectWatchdog 10 | { 11 | /** 12 | * @param string $prop 13 | * @param mixed $value 14 | * @throws UndefinedPropertySetting if trying to set undefined property 15 | * @return void 16 | */ 17 | public function __set($prop, $value) 18 | { 19 | throw new UndefinedPropertySetting('Trying to set undefined property ' . json_encode($prop) . ' in object of class ' . get_class($this)); 20 | } 21 | 22 | /** 23 | * @param string $prop 24 | * @throws UnsettingProperty if trying to unset property 25 | * @return void 26 | */ 27 | public function __unset($prop) 28 | { 29 | throw new UnsettingProperty('Trying to unset property ' . json_encode($prop) . ' in object of class ' . get_class($this)); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /PHPDaemon/Utils/PPPDeflate.php: -------------------------------------------------------------------------------- 1 | 10 | * @author Efimenko Dmitriy 11 | * 12 | * Use: 13 | * $obj1 = new PlainJSON('{"name":"John"}'); 14 | * $obj2 = new PlainJSON('{"name":"Smit"}'); 15 | * echo PlainJSON::apply(json_encode([ ['name' => 'Mary' ], $obj1, $obj2])); 16 | * // [{"name":"Mary"},{"name":"John"},{"name":"Smit"}] 17 | */ 18 | class PlainJSON implements \JsonSerializable 19 | { 20 | use \PHPDaemon\Traits\ClassWatchdog; 21 | use \PHPDaemon\Traits\StaticObjectWatchdog; 22 | 23 | protected static $tr = []; 24 | protected $id; 25 | 26 | /** 27 | * Save 28 | * @param string $str JSON string 29 | */ 30 | public function __construct($str) 31 | { 32 | $this->id = Daemon::uniqid(); 33 | static::$tr['"' . $this->id . '"'] = $str; 34 | } 35 | 36 | /** 37 | * Apply 38 | * @param string $data JSON string 39 | * @return string Result JSON 40 | */ 41 | public static function apply($data) 42 | { 43 | return strtr($data, static::$tr); 44 | } 45 | 46 | /** 47 | * Clean cache 48 | */ 49 | public function __destruct() 50 | { 51 | unset(static::$tr[$this->id]); 52 | } 53 | 54 | /** 55 | * jsonSerialize 56 | * @return string 57 | */ 58 | public function jsonSerialize() 59 | { 60 | return $this->id; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /PHPDaemon/Utils/func.php: -------------------------------------------------------------------------------- 1 | 10 | */ 11 | interface RouteInterface 12 | { 13 | // @TODO: fill 14 | 15 | /** 16 | * Called when new frame is received 17 | * @param string $data Frame's contents 18 | * @param integer $type Frame's type 19 | * @return void 20 | */ 21 | public function onFrame($data, $type); 22 | } 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | phpDaemon 2 | ========= 3 | [![Build Status](https://travis-ci.org/kakserpom/phpdaemon.svg?branch=master)](https://travis-ci.org/kakserpom/phpdaemon) 4 | 5 | https://github.com/kakserpom/phpdaemon 6 | 7 | Asynchronous framework in PHP. It has a huge number of features. Designed for highload. 8 | Each worker is capable of handling thousands of simultaneous connections. 9 | Main features and possibilities: 10 | 11 | * Powerful servers: HTTP, FastCGI, FlashPolicy, Ident, Socks4/5. 12 | * Many bundled clients like DNS, MySQL, Postgresql, Memcache, MongoDB, Redis, HTTP, IRC, Jabber, ICMP, Valve games client, etc. 13 | * Asynchronous Filesystem I/O (using eio). 14 | * Many useful built-in applications like CGI. 15 | * Interactive debug console. 16 | * Dynamic spawning workers. 17 | * Chroot & Chdir for workers. 18 | * Automatic graceful reloading user's scripts when it's updated. 19 | * Graceful worker shutdown (and re-spawn if necessary) by the following limits: memory, query counter, idle time. 20 | 21 | Master process understands signals: 22 | 23 | SIGINT, SIGTERM, SIGQUIT - termination. 24 | SIGHUP - update config from file. 25 | SIGUSR1 - reopen log-file. 26 | SIGUSR2 - graceful restart all workers. 27 | 28 | Mail listing: phpdaemon@googlegroups.com 29 | Maintainer: maintainer@daemon.io 30 | Web-site: http://daemon.io/ 31 | 32 | -------------------------------------------------------------------------------- /VERSION: -------------------------------------------------------------------------------- 1 | 1.0-beta3 -------------------------------------------------------------------------------- /bin/php-chroot: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env php 2 | 14 | 15 | Environment variables: 16 | 17 | PHPCHROOT_USER - run as username (optional, default: file owner user) 18 | PHPCHROOT_GROUP - run as groupname (optional, default: file owner group) 19 | PHPCHROOT - chroot to directory (optional) 20 | 21 | EOF; 22 | 23 | exit; 24 | } 25 | 26 | $_SERVER["SCRIPT_NAME"] = $_SERVER["SCRIPT_FILENAME"] = $argv[1]; 27 | 28 | $filename = (isset($_SERVER["PHPCHROOT"]) ? $_SERVER["PHPCHROOT"] . "/" : null) . $argv[1]; 29 | 30 | if ( 31 | !file_exists($filename) 32 | || !is_file($filename) 33 | ) { 34 | exit('php-chroot: PHP file not found'); 35 | } 36 | 37 | $user = isset($_SERVER['PHPCHROOT_USER']) 38 | ? posix_getpwnam($_SERVER['PHPCHROOT_USER']) 39 | : array('uid' => fileowner($filename)); 40 | 41 | if (!$user) { 42 | exit('php-chroot: Invalid PHPCHROOT_USER'); 43 | } 44 | 45 | $group = isset($_SERVER['PHPCHROOT_GROUP']) 46 | ? posix_getgrnam($_SERVER['PHPCHROOT_GROUP']) 47 | : array('gid' => filegroup($filename)); 48 | 49 | if (!$group) { 50 | exit('PHP chroot: Invalid PHPCHROOT_GROUP'); 51 | } 52 | 53 | if ( 54 | !$user['uid'] 55 | || !$group['gid'] 56 | ) { 57 | exit('php-chroot: Root uid/gid are not allowed'); 58 | } 59 | 60 | if (isset($_SERVER['PHPCHROOT'])) { 61 | chroot($_SERVER['PHPCHROOT']); 62 | unset($_SERVER['PHPCHROOT']); 63 | } 64 | 65 | posix_setuid($user['uid']); 66 | posix_setgid($group['gid']); 67 | 68 | if (isset($_SERVER['PHPCHROOT_USER'])) { 69 | unset($_SERVER['PHPCHROOT_USER']); 70 | } 71 | 72 | if (isset($_SERVER['PHPCHROOT_GROUP'])) { 73 | unset($_SERVER['PHPCHROOT_GROUP']); 74 | } 75 | 76 | unset($user, $group); 77 | 78 | include $_SERVER['argv'][1]; 79 | 80 | exit; 81 | -------------------------------------------------------------------------------- /bin/sampleapp: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env php 2 | =5.6", 27 | "ext-event": ">=1.6.0", 28 | "ext-pcntl": "*", 29 | "ext-shmop": "*", 30 | "ext-sockets": "*" 31 | }, 32 | "require-dev": { 33 | "squizlabs/php_codesniffer": "~2.6.0", 34 | "phpunit/phpunit": "~5.3.2" 35 | }, 36 | "bin": [ 37 | "bin/phpd" 38 | ], 39 | "autoload": { 40 | "psr-4": { 41 | "PHPDaemon\\": "PHPDaemon/" 42 | }, 43 | "files": [ 44 | "PHPDaemon/Utils/func.php" 45 | ] 46 | }, 47 | "autoload-dev": { 48 | "psr-4": { 49 | "Tests\\": "tests/" 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /conf/AppResolver.php: -------------------------------------------------------------------------------- 1 | 8 | */ 9 | class MyAppResolver extends \PHPDaemon\Core\AppResolver 10 | { 11 | 12 | /** 13 | * Routes incoming request to related application. Method is for overloading. 14 | * @param object Request. 15 | * @param object AppInstance of Upstream. 16 | * @return string Application's name. 17 | */ 18 | public function getRequestRoute($req, $upstream) 19 | { 20 | 21 | /* 22 | This method should return application name to handle incoming request ($req). 23 | */ 24 | 25 | if (preg_match('~^/(WebSocketOverCOMET|Example.*)/?~', $req->attrs->server['DOCUMENT_URI'], $m)) { 26 | return $m[1]; 27 | } 28 | 29 | /* Example 30 | $host = basename($req->attrs->server['HTTP_HOST']); 31 | 32 | if (is_dir('/home/web/domains/' . basename($host))) { 33 | preg_match('~^/(.*)$~', $req->attrs->server['DOCUMENT_URI'], $m); 34 | $req->attrs->server['FR_PATH'] = '/home/web/domains/'.$host.'/'.$m[1]; 35 | $req->attrs->server['FR_AUTOINDEX'] = TRUE; 36 | return 'FileReader'; 37 | } */ 38 | } 39 | 40 | } 41 | 42 | return new MyAppResolver; -------------------------------------------------------------------------------- /conf/conf.d/ExampleJabberBot.conf: -------------------------------------------------------------------------------- 1 | #ExampleJabberbot { 2 | # url "xmpp://user@password@jabber:5222/phpdaemon"; 3 | #} -------------------------------------------------------------------------------- /conf/conf.d/FastCGI.conf: -------------------------------------------------------------------------------- 1 | #Pool:FastCGIServer { 2 | #privileged; 3 | # you can redefine default settings here 4 | #listen '0.0.0.0'; 5 | #port 9000; 6 | #} 7 | -------------------------------------------------------------------------------- /conf/conf.d/FlashpolicyServer.conf: -------------------------------------------------------------------------------- 1 | #Pool:FlashPolicyServer { 2 | #privileged; 3 | # you can redefine default settings here 4 | #listen '0.0.0.0'; 5 | #port 843; 6 | #} 7 | -------------------------------------------------------------------------------- /conf/conf.d/HTTPServer.conf: -------------------------------------------------------------------------------- 1 | #Pool:HTTPServer { 2 | #privileged; 3 | # you can redefine default settings here 4 | #} 5 | -------------------------------------------------------------------------------- /conf/conf.d/IdentServer.conf: -------------------------------------------------------------------------------- 1 | #Pool:IdentServer { 2 | #privileged; 3 | # you can redefine default settings here 4 | #listen 'tcp://0.0.0.0'; 5 | #port 113; 6 | #} 7 | -------------------------------------------------------------------------------- /conf/conf.d/SSL-sample.conf: -------------------------------------------------------------------------------- 1 | #TransportContext:myContext { 2 | # ssl; 3 | # certFile "/path/to/cert.pem"; 4 | # pkFile "/path/to/privkey.pem"; 5 | # passphrase ""; 6 | # verifyPeer true; 7 | # allowSelfSigned true; 8 | #} 9 | # 10 | # Then let's use the context: 11 | # 12 | #Pool:HTTPServer { 13 | # listen "tcp://0.0.0.0:80", "tcp://0.0.0.0:443##myContext"; 14 | # port 80; 15 | # privileged; 16 | # #maxconcurrency 1; 17 | #} -------------------------------------------------------------------------------- /conf/conf.d/WebSocketServer.conf: -------------------------------------------------------------------------------- 1 | #Pool:WebSocketServer { 2 | # you can redefine default settings here 3 | #privileged; 4 | #listen 'tcp://0.0.0.0'; 5 | #port 8047; 6 | #} 7 | -------------------------------------------------------------------------------- /conf/crossdomain.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /conf/logrotate: -------------------------------------------------------------------------------- 1 | /var/log/phpdaemon.log { 2 | daily 3 | missingok 4 | rotate 10 5 | compress 6 | delaycompress 7 | notifempty 8 | sharedscripts 9 | postrotate 10 | [ ! -f /var/run/phpd.pid ] || kill -USR1 `cat /var/run/phpd.pid` 11 | endscript 12 | } 13 | -------------------------------------------------------------------------------- /conf/phpd-ircbnc.conf: -------------------------------------------------------------------------------- 1 | ## Config file 2 | 3 | user root; 4 | group root; 5 | 6 | max-workers 1; 7 | min-workers 1; 8 | max-idle 0; 9 | logstorage /var/log/phpdaemon-debug.log; 10 | start-workers 1; 11 | auto-reload 0; 12 | pid-file /var/run/phpd-ircbot.pid; 13 | path /home/web/AppResolver.php; 14 | auto-load "/{usr/local/quicky/%1$s.class.php,home/web/WakePHP/*/%1$s.class.php,home/web/monoloop/*/%1$s.class.php,home/web/HAgent/*/%1$s.class.php,home/web/HAgent/*/%1$s.php}"; 15 | 16 | #ExampleIRCBot {} 17 | Pool:IRCBouncer { 18 | url "irc://kkspy:jumanji@pratchett.freenode.net/kkspy/John Doe"; 19 | defaultChannels "##unavailable"; 20 | servername "loopback.su"; 21 | protologging 1; 22 | password "QsRoGdpdc1"; 23 | } 24 | Pool:IdentServer { 25 | } -------------------------------------------------------------------------------- /conf/phpd-jw.conf: -------------------------------------------------------------------------------- 1 | ## Config file 2 | 3 | add-include-path '/home/web/WakePHP:/usr/local/quicky:/home/web/vocabio.us'; 4 | pidfile /var/run/phpd-jw.pid; 5 | include /home/web/vocabio.us/conf/JobWorker.conf; 6 | verbose-tty 1; -------------------------------------------------------------------------------- /conf/phpd-sampleapp.conf: -------------------------------------------------------------------------------- 1 | ## Config file 2 | 3 | #user nobody; 4 | #group nobody; 5 | 6 | max-workers 1; 7 | min-workers 1; 8 | start-workers 1; 9 | max-idle 0; 10 | logging 1; 11 | verbose-tty 1; 12 | 13 | #pidfile /var/run/phpd.pid; 14 | #logstorage /var/log/phpdaemon.log; 15 | 16 | #pidfile /tmp/phpd.pid; 17 | #logstorage /tmp/phpdaemon.log; 18 | 19 | # log to current dir 20 | pidfile phpd.pid; 21 | logstorage phpdaemon.log; 22 | 23 | path conf/AppResolver.php; 24 | 25 | #Pool:Servers\WebSocket {privileged;} 26 | \PHPDaemon\Clients\WebSocket\Example {} 27 | 28 | include conf.d/*.conf; 29 | -------------------------------------------------------------------------------- /conf/phpd.conf.example: -------------------------------------------------------------------------------- 1 | ## Config file 2 | 3 | #user john; 4 | #group workgroup; 5 | 6 | max-workers 8; 7 | min-workers 8; 8 | start-workers 8; 9 | max-idle 0; 10 | #add-include-path '/path/to/your/folder'; 11 | 12 | #\Your\Application { 13 | # config of your application which class is "\Your\Application", in 14 | #} 15 | 16 | 17 | # other applications... 18 | 19 | include conf.d/*.conf; 20 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 11 | 12 | 13 | 14 | ./tests/ 15 | 16 | 17 | 18 | 19 | 20 | ./src 21 | 22 | 23 | ./vendor/ 24 | ./vendor/autoload.php 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /tests/AbstractTestCase.php: -------------------------------------------------------------------------------- 1 | stop(); 21 | } 22 | protected function runAsync($method, $timeout = 3e6) 23 | { 24 | Timer::add(function () use ($method) { 25 | self::assertSame(0, 1, 'Some callbacks didnt finished in ' . $method); 26 | }, $timeout); 27 | 28 | EventLoop::$instance->run(); 29 | //EventLoop::$instance->free(); 30 | //EventLoop::$instance = null; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /tests/Clients/Redis/ConnectionTest.php: -------------------------------------------------------------------------------- 1 | 4 | */ 5 | 6 | namespace Tests\Utils; 7 | 8 | class ConnectionTest extends \PHPUnit_Framework_TestCase 9 | { 10 | public function testGetInstance() 11 | { 12 | self::assertTrue(true); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/Utils/CryptTest.php: -------------------------------------------------------------------------------- 1 | 4 | */ 5 | 6 | namespace Tests\Utils; 7 | 8 | use PHPDaemon\Utils\Crypt; 9 | use Tests\AbstractTestCase; 10 | 11 | class CryptTest extends AbstractTestCase 12 | { 13 | 14 | /** 15 | * @covers Crypt::randomInts 16 | */ 17 | public function testRandomInts() 18 | { 19 | $this->prepareAsync(); 20 | Crypt::randomInts(5, function ($ints) { 21 | self::assertCount(5, $ints, '$ints must contain 5 elements'); 22 | $this->completeAsync(); 23 | }); 24 | 25 | $this->runAsync(__METHOD__); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /tests/ci/install.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Install ext-eio 4 | git clone https://github.com/rosmanov/pecl-eio.git 5 | pushd pecl-eio 6 | phpize 7 | ./configure 8 | make 9 | make install 10 | popd 11 | echo "extension=eio.so" >> "$(php -r 'echo php_ini_loaded_file();')" 12 | 13 | # Install ext-event 14 | curl http://pecl.php.net/get/event-2.3.0.tgz | tar -xz 15 | pushd event-2.3.0 16 | phpize 17 | ./configure 18 | make 19 | make install 20 | popd 21 | echo "extension=event.so" >> "$(php -r 'echo php_ini_loaded_file();')" 22 | --------------------------------------------------------------------------------