GithubHelp home page GithubHelp logo

phvia / firman Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 1.0 48 KB

PHP Multi-process and Non-blocking I/O Socket Library.

License: MIT License

PHP 100.00%
php7 multi-process non-blocking-io socket php-socket firman

firman's Introduction

Firman

PHP multi-process and non-blocking I/O library.

Dependency

  • PHP>=7. (for new features and high performance)
  • PCNTL extension. (compile PHP with --enable-pcntl option to enable)
  • Sockets extension. (compile PHP with --enable-sockets option to enable)

Install

$ composer require phvia/firman:dev-master

Run example

  • Show usage
$ php /path/to/firman/examples/via_websocket_serv_builtin.php

Firman package 0.0.1

Usage:
  command [options] [arguments]

Options:
  -h, --help            Display this help message
  -q, --quiet           Do not output any message
  -V, --version         Display this application version
      --ansi            Force ANSI output
      --no-ansi         Disable ANSI output
  -n, --no-interaction  Do not ask any interactive question
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:
  help     Displays help for a command
  list     Lists commands
  restart  Restart Firman server
  start    Start Firman server
  stop     Stop Firman server
  • Show usage detail
$ php /path/to/firman/examples/via_websocket_serv_builtin.php start -h

Usage:
  start [options]

Options:
  -e, --env=ENV         The Environment name (support: dev, prod) [default: "dev"]
  -h, --help            Display this help message
  -q, --quiet           Do not output any message
  -V, --version         Display this application version
      --ansi            Force ANSI output
      --no-ansi         Disable ANSI output
  -n, --no-interaction  Do not ask any interactive question
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Help:
  Start Firman server
  • Process control
# After start server, you can access examples/ws.html in web browser
$ php examples/via_websocket_serv_builtin.php start

# Restart Firman server
$ php examples/via_websocket_serv_builtin.php restart

# Stop Firman server
$ php examples/via_websocket_serv_builtin.php stop
  • Run in daemon
$ php /path/to/firman/xxx.php start --env=prod
# OR
$ php /path/to/firman/xxx.php start --eprod
$
$ ps auxf | grep Firman

Do it yourself

  • Simplest configure
include '/path/to/vendor/autoload.php';

(new \Firman\Server('tcp://0.0.0.0:8080'))->run();
  • Full configure
inlcude '/path/to/vendor/autoload.php';

$server = new \Firman\Server();

$socket = 'tcp://0.0.0.0:8080';

$server
    // Parameter.
    
    // optional, default is 1
    // Set child process number
    ->setCount(1)
    
    // optional, can also be in constructor
    // Set socket
    ->setSocket($socket)
    
    // optional, default is Firman
    // Set process title
    ->setProcessTitle('Firman')
    
    // optional, default is /tmp
    // Set the path of file saved ppid
    ->setPpidPath('/tmp')
    
    // optional, default is 100
    // Set socket backlog number
    ->setBacklog(100)
    
    // optional, default is 200
    // Set select system call timeout value
    ->setSelectTimeout(5)
    
    // optional, default is 60
    // Set accept timeout value
    ->setAcceptTimeout(10)

    // Event callback.
    
    // optional, when client connected with server, callback trigger.
    // Set connection event callback task
    ->onConnection(function($connection) {
        echo "New client connected." . PHP_EOL;
    })
    
    // optional, when client send message to server, callback trigger.
    // Set message event callback task
    ->onMessage(function($connection) {
        // implement your logic
    })
    
    // Run server.
    
    ->run();

Server workflow explain

  • Check environment.
  • Parse command.
  • Initialize master process information.
  • Fork child process, install signal for child, poll on child.
  • Create socket server (like: create socket, bind, listen, set option).
  • Block on master, monitor any child process and restart who exited.

Tests

$ ./vendor/bin/phpunit --bootstrap=vendor/autoload.php tests

Todo

Our position is focus on socket :

  • Implement protocol parse built-in.
  • Support Unix domain, UDP, ect.
  • Robustness.

Resources:

Composer Document: https://getcomposer.org/doc/

Symfony Console Component: http://symfony.com/doc/current/components/console.html

Contribute:

Any pull requests to improve phvia/firman are welcome.

Coding style follow PSR2: https://www.php-fig.org/psr/psr-2/
Using PHP_CodeSniffer tool: https://github.com/squizlabs/PHP_CodeSniffer
Running check: php phpcs.phar --standard=psr2 ./

Recommend Coding Standards: https://symfony.com/doc/current/contributing/code/standards.html

Group

QQ group: 377154148

License

MIT

firman's People

Contributors

farwish avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

xiechetech

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.