\GatewayWorkerGateway

Gateway,基于Worker 开发 用于转发客户端的数据给Worker处理,以及转发Worker的数据给客户端

Summary

Methods
Properties
Constants
runAll()
getAllWorkers()
getEventLoop()
signalHandler()
resetStd()
getStartFilesForWindows()
forkOneWorkerForWindows()
checkWorkerStatusForWindows()
setUserAndGroup()
stopAll()
checkIfChildRunning()
getStatus()
getGracefulStop()
checkErrors()
log()
safeEcho()
__construct()
listen()
unlisten()
pauseAccept()
resumeAccept()
getSocketName()
run()
stop()
acceptConnection()
acceptUdpConnection()
onClientMessage()
onClientConnect()
onWebsocketConnect()
routerRand()
routerBind()
onClientClose()
onWorkerStart()
onWorkerConnect()
onWorkerMessage()
onWorkerClose()
registerAddress()
ping()
pingBusinessWorker()
onWorkerStop()
$id
$name
$count
$user
$group
$reloadable
$reusePort
$onWorkerStart
$onConnect
$onMessage
$onClose
$onError
$onBufferFull
$onBufferDrain
$onWorkerStop
$onWorkerReload
$transport
$connections
$protocol
$stopping
$daemonize
$stdoutFile
$pidFile
$logFile
$globalEvent
$onMasterReload
$onMasterStop
$eventLoopClass
$lanIp
$lanPort
$startPort
$registerAddress
$pingInterval
$pingNotResponseLimit
$pingData
$secretKey
$router
$sendToWorkerBufferSize
$sendToClientBufferSize
$protocolAccelerate
VERSION
STATUS_STARTING
STATUS_RUNNING
STATUS_SHUTDOWN
STATUS_RELOADING
KILL_WORKER_TIMER_TIME
DEFAULT_BACKLOG
MAX_UDP_PACKAGE_SIZE
PERSISTENCE_CONNECTION_PING_INTERVAL
checkSapiEnv()
init()
initWorkers()
initId()
getCurrentUser()
displayUI()
parseCommand()
formatStatusData()
installSignal()
reinstallSignal()
daemonize()
saveMasterPid()
getEventLoopName()
getAllWorkerPids()
forkWorkers()
forkWorkersForLinux()
forkWorkersForWindows()
forkOneWorkerForLinux()
getId()
setProcessTitle()
monitorWorkers()
monitorWorkersForLinux()
monitorWorkersForWindows()
exitAndClearAll()
reload()
writeStatisticsToStatusFile()
writeConnectionsStatisticsToStatusFile()
getErrorType()
generateConnectionId()
sendToWorker()
preEncodeForClient()
$_autoloadRootPath
$_pauseAccept
$_masterPid
$_mainSocket
$_socketName
$_context
$_workers
$_pidMap
$_pidsToRestart
$_idMap
$_status
$_maxWorkerNameLength
$_maxSocketNameLength
$_maxUserNameLength
$_statisticsFile
$_startFile
$_OS
$_processForWindows
$_globalStatistics
$_availableEventLoops
$_builtinTransports
$_gracefulStop
$_outputStream
$_outputDecorated
$_clientConnections
$_uidConnections
$_groupConnections
$_workerConnections
$_innerTcpWorker
$_onWorkerStart
$_onConnect
$_onMessage
$_onClose
$_onWorkerStop
$_startTime
$_gatewayPort
$_connectionIdRecorder
N/A
outputStream()
No private properties found
N/A

Constants

VERSION

VERSION = '3.0.12' : string

版本

STATUS_STARTING

STATUS_STARTING = 1 : integer

Status starting.

STATUS_RUNNING

STATUS_RUNNING = 2 : integer

Status running.

STATUS_SHUTDOWN

STATUS_SHUTDOWN = 4 : integer

Status shutdown.

STATUS_RELOADING

STATUS_RELOADING = 8 : integer

Status reloading.

KILL_WORKER_TIMER_TIME

KILL_WORKER_TIMER_TIME = 2 : integer

After sending the restart command to the child process KILL_WORKER_TIMER_TIME seconds, if the process is still living then forced to kill.

DEFAULT_BACKLOG

DEFAULT_BACKLOG = 102400 : integer

Default backlog. Backlog is the maximum length of the queue of pending connections.

MAX_UDP_PACKAGE_SIZE

MAX_UDP_PACKAGE_SIZE = 65535 : integer

Max udp package size.

PERSISTENCE_CONNECTION_PING_INTERVAL

PERSISTENCE_CONNECTION_PING_INTERVAL = 25 : integer

用于保持长连接的心跳时间间隔

Properties

$id

$id : integer

Worker id.

Type

integer

$name

$name : string

Name of the worker processes.

Type

string

$count

$count : integer

Number of worker processes.

Type

integer

$user

$user : string

Unix user of processes, needs appropriate privileges (usually root).

Type

string

$group

$group : string

Unix group of processes, needs appropriate privileges (usually root).

Type

string

$reloadable

$reloadable : boolean

是否可以平滑重启,gateway 不能平滑重启,否则会导致连接断开

Type

boolean

$reusePort

$reusePort : boolean

reuse port.

Type

boolean

$onWorkerStart

$onWorkerStart : callback

Emitted when worker processes start.

Type

callback

$onConnect

$onConnect : callback

Emitted when a socket connection is successfully established.

Type

callback

$onMessage

$onMessage : callback

Emitted when data is received.

Type

callback

$onClose

$onClose : callback

Emitted when the other end of the socket sends a FIN packet.

Type

callback

$onError

$onError : callback

Emitted when an error occurs with connection.

Type

callback

$onBufferFull

$onBufferFull : callback

Emitted when the send buffer becomes full.

Type

callback

$onBufferDrain

$onBufferDrain : callback

Emitted when the send buffer becomes empty.

Type

callback

$onWorkerStop

$onWorkerStop : callback

Emitted when worker processes stoped.

Type

callback

$onWorkerReload

$onWorkerReload : callback

Emitted when worker processes get reload signal.

Type

callback

$transport

$transport : string

Transport layer protocol.

Type

string

$connections

$connections : array

Store all connections of clients.

Type

array

$protocol

$protocol : string

Application layer protocol.

Type

string

$stopping

$stopping : boolean

Is worker stopping ?

Type

boolean

$daemonize

$daemonize : boolean

Daemonize.

Type

boolean

$stdoutFile

$stdoutFile : string

Stdout file.

Type

string

$pidFile

$pidFile : string

The file to store master process PID.

Type

string

$logFile

$logFile : mixed

Log file.

Type

mixed

$onMasterReload

$onMasterReload : callback

Emitted when the master process get reload signal.

Type

callback

$onMasterStop

$onMasterStop : callback

Emitted when the master process terminated.

Type

callback

$eventLoopClass

$eventLoopClass : string

EventLoopClass

Type

string

$lanIp

$lanIp : string

本机 IP 单机部署默认 127.0.0.1,如果是分布式部署,需要设置成本机 IP

Type

string

$lanPort

$lanPort : string

本机端口

Type

string

$startPort

$startPort : integer

gateway 内部通讯起始端口,每个 gateway 实例应该都不同,步长1000

Type

integer

$registerAddress

$registerAddress : string|array

注册服务地址,用于注册 Gateway BusinessWorker,使之能够通讯

Type

string|array

$pingInterval

$pingInterval : integer

心跳时间间隔

Type

integer

$pingNotResponseLimit

$pingNotResponseLimit : integer

$pingNotResponseLimit * $pingInterval 时间内,客户端未发送任何数据,断开客户端连接

Type

integer

$pingData

$pingData : string

服务端向客户端发送的心跳数据

Type

string

$secretKey

$secretKey : string

秘钥

Type

string

$router

$router : callback

路由函数

Type

callback

$sendToWorkerBufferSize

$sendToWorkerBufferSize : integer

gateway进程转发给businessWorker进程的发送缓冲区大小

Type

integer

$sendToClientBufferSize

$sendToClientBufferSize : integer

gateway进程将数据发给客户端时每个客户端发送缓冲区大小

Type

integer

$protocolAccelerate

$protocolAccelerate : boolean

协议加速

Type

boolean

$_autoloadRootPath

$_autoloadRootPath : string

Root path for autoload.

Type

string

$_pauseAccept

$_pauseAccept : boolean

Pause accept new connections or not.

Type

boolean

$_masterPid

$_masterPid : integer

The PID of master process.

Type

integer

$_mainSocket

$_mainSocket : resource

Listening socket.

Type

resource

$_socketName

$_socketName : string

Socket name. The format is like this http://0.0.0.0:80 .

Type

string

$_context

$_context : resource

Context of socket.

Type

resource

$_workers

$_workers : array<mixed,\Workerman\Worker>

All worker instances.

Type

array<mixed,\Workerman\Worker>

$_pidMap

$_pidMap : array

All worker porcesses pid.

The format is like this [worker_id=>[pid=>pid, pid=>pid, ..], ..]

Type

array

$_pidsToRestart

$_pidsToRestart : array

All worker processes waiting for restart.

The format is like this [pid=>pid, pid=>pid].

Type

array

$_idMap

$_idMap : array

Mapping from PID to worker process ID.

The format is like this [worker_id=>[0=>$pid, 1=>$pid, ..], ..].

Type

array

$_status

$_status : integer

Current status.

Type

integer

$_maxWorkerNameLength

$_maxWorkerNameLength : integer

Maximum length of the worker names.

Type

integer

$_maxSocketNameLength

$_maxSocketNameLength : integer

Maximum length of the socket names.

Type

integer

$_maxUserNameLength

$_maxUserNameLength : integer

Maximum length of the process user names.

Type

integer

$_statisticsFile

$_statisticsFile : string

The file to store status info of current worker process.

Type

string

$_startFile

$_startFile : string

Start file.

Type

string

$_OS

$_OS : string

OS.

Type

string

$_processForWindows

$_processForWindows : array

Processes for windows.

Type

array

$_globalStatistics

$_globalStatistics : array

Status info of current worker process.

Type

array

$_availableEventLoops

$_availableEventLoops : array

Available event loops.

Type

array

$_builtinTransports

$_builtinTransports : array

PHP built-in protocols.

Type

array

$_gracefulStop

$_gracefulStop : string

Graceful stop or not.

Type

string

$_outputStream

$_outputStream : resource

Standard output stream

Type

resource

$_outputDecorated

$_outputDecorated : boolean

If $outputStream support decorated

Type

boolean

$_clientConnections

$_clientConnections : array

保存客户端的所有 connection 对象

Type

array

$_uidConnections

$_uidConnections : 

uid 到 connection 的映射,一对多关系

Type

$_groupConnections

$_groupConnections : array

group 到 connection 的映射,一对多关系

Type

array

$_workerConnections

$_workerConnections : array

保存所有 worker 的内部连接的 connection 对象

Type

array

$_innerTcpWorker

$_innerTcpWorker : \Workerman\Worker

gateway 内部监听 worker 内部连接的 worker

Type

\Workerman\Worker

$_onWorkerStart

$_onWorkerStart : callback

当 worker 启动时

Type

callback

$_onConnect

$_onConnect : callback

当有客户端连接时

Type

callback

$_onMessage

$_onMessage : callback

当客户端发来消息时

Type

callback

$_onClose

$_onClose : callback

当客户端连接关闭时

Type

callback

$_onWorkerStop

$_onWorkerStop : callback

当 worker 停止时

Type

callback

$_startTime

$_startTime : integer

进程启动时间

Type

integer

$_gatewayPort

$_gatewayPort : integer

gateway 监听的端口

Type

integer

$_connectionIdRecorder

$_connectionIdRecorder : integer

connectionId 记录器

Type

integer

Methods

runAll()

runAll() : void

Run all worker instances.

getAllWorkers()

getAllWorkers() : array

Get all worker instances.

Returns

array

signalHandler()

signalHandler(integer  $signal) 

Signal handler.

Parameters

integer $signal

resetStd()

resetStd() 

Redirect standard input and output.

Throws

\Exception

getStartFilesForWindows()

getStartFilesForWindows() : array

Get start files for windows.

Returns

array

forkOneWorkerForWindows()

forkOneWorkerForWindows(string  $start_file) 

Fork one worker process.

Parameters

string $start_file

checkWorkerStatusForWindows()

checkWorkerStatusForWindows() : void

check worker status for windows.

setUserAndGroup()

setUserAndGroup() : void

Set unix user and group for current process.

stopAll()

stopAll() : void

Stop.

checkIfChildRunning()

checkIfChildRunning() 

check if child processes is really running

getStatus()

getStatus() : \Workerman\number

Get process status.

Returns

\Workerman\number

getGracefulStop()

getGracefulStop() : boolean

If stop gracefully.

Returns

boolean

checkErrors()

checkErrors() : void

Check errors when current process exited.

log()

log(string  $msg) : void

Log.

Parameters

string $msg

safeEcho()

safeEcho(  $msg, boolean  $decorated = false) : boolean

Safe Echo.

Parameters

$msg
boolean $decorated

Returns

boolean

__construct()

__construct(string  $socket_name, array  $context_option = array()) 

构造函数

Parameters

string $socket_name
array $context_option

listen()

listen() 

Listen.

Throws

\Exception

unlisten()

unlisten() : void

Unlisten.

pauseAccept()

pauseAccept() : void

Pause accept new connections.

resumeAccept()

resumeAccept() : void

Resume accept new connections.

getSocketName()

getSocketName() : string

Get socket name.

Returns

string

run()

run() : void

Run worker instance.

stop()

stop() : void

Stop current worker instance.

acceptConnection()

acceptConnection(resource  $socket) : void

Accept a connection.

Parameters

resource $socket

acceptUdpConnection()

acceptUdpConnection(resource  $socket) : boolean

For udp package.

Parameters

resource $socket

Returns

boolean

onClientMessage()

onClientMessage(\Workerman\Connection\TcpConnection  $connection, mixed  $data) 

当客户端发来数据时,转发给worker处理

Parameters

\Workerman\Connection\TcpConnection $connection
mixed $data

onClientConnect()

onClientConnect(\Workerman\Connection\TcpConnection  $connection) 

当客户端连接上来时,初始化一些客户端的数据 包括全局唯一的client_id、初始化session等

Parameters

\Workerman\Connection\TcpConnection $connection

onWebsocketConnect()

onWebsocketConnect(  $connection,   $http_buffer) 

websocket握手时触发

Parameters

$connection
$http_buffer

routerRand()

routerRand(array  $worker_connections, \Workerman\Connection\TcpConnection  $client_connection, integer  $cmd, mixed  $buffer) : \Workerman\Connection\TcpConnection

随机路由,返回 worker connection 对象

Parameters

array $worker_connections
\Workerman\Connection\TcpConnection $client_connection
integer $cmd
mixed $buffer

Returns

\Workerman\Connection\TcpConnection

routerBind()

routerBind(array  $worker_connections, \Workerman\Connection\TcpConnection  $client_connection, integer  $cmd, mixed  $buffer) : \Workerman\Connection\TcpConnection

client_id 与 worker 绑定

Parameters

array $worker_connections
\Workerman\Connection\TcpConnection $client_connection
integer $cmd
mixed $buffer

Returns

\Workerman\Connection\TcpConnection

onWorkerStart()

onWorkerStart() : void

当 Gateway 启动的时候触发的回调函数

onWorkerConnect()

onWorkerConnect(\Workerman\Connection\TcpConnection  $connection) 

当 worker 通过内部通讯端口连接到 gateway 时

Parameters

\Workerman\Connection\TcpConnection $connection

onWorkerMessage()

onWorkerMessage(\Workerman\Connection\TcpConnection  $connection, mixed  $data) : void

当 worker 发来数据时

Parameters

\Workerman\Connection\TcpConnection $connection
mixed $data

Throws

\Exception

registerAddress()

registerAddress() : boolean

存储当前 Gateway 的内部通信地址

Returns

boolean

ping()

ping() : void

心跳逻辑

pingBusinessWorker()

pingBusinessWorker() : void

向 BusinessWorker 发送心跳数据,用于保持长连接

onWorkerStop()

onWorkerStop() : void

当 gateway 关闭时触发,清理数据

checkSapiEnv()

checkSapiEnv() : void

Check sapi.

init()

init() : void

Init.

initWorkers()

initWorkers() : void

Init All worker instances.

initId()

initId() 

Init idMap.

return void

getCurrentUser()

getCurrentUser() : string

Get unix user of current porcess.

Returns

string

displayUI()

displayUI() : void

Display staring UI.

parseCommand()

parseCommand() : void

Parse command.

formatStatusData()

formatStatusData() : string

Format status data.

Returns

string

installSignal()

installSignal() : void

Install signal handler.

reinstallSignal()

reinstallSignal() : void

Reinstall signal handler.

daemonize()

daemonize() 

Run as deamon mode.

Throws

\Exception

saveMasterPid()

saveMasterPid() 

Save pid.

Throws

\Exception

getEventLoopName()

getEventLoopName() : string

Get event loop name.

Returns

string

getAllWorkerPids()

getAllWorkerPids() : array

Get all pids of worker processes.

Returns

array

forkWorkers()

forkWorkers() : void

Fork some worker processes.

forkWorkersForLinux()

forkWorkersForLinux() : void

Fork some worker processes.

forkWorkersForWindows()

forkWorkersForWindows() : void

Fork some worker processes.

forkOneWorkerForLinux()

forkOneWorkerForLinux(\Workerman\Worker  $worker) 

Fork one worker process.

Parameters

\Workerman\Worker $worker

Throws

\Exception

getId()

getId(integer  $worker_id, integer  $pid) : integer

Get worker id.

Parameters

integer $worker_id
integer $pid

Returns

integer

setProcessTitle()

setProcessTitle(string  $title) : void

Set process name.

Parameters

string $title

monitorWorkers()

monitorWorkers() : void

Monitor all child processes.

monitorWorkersForLinux()

monitorWorkersForLinux() : void

Monitor all child processes.

monitorWorkersForWindows()

monitorWorkersForWindows() : void

Monitor all child processes.

exitAndClearAll()

exitAndClearAll() : void

Exit current process.

reload()

reload() : void

Execute reload.

writeStatisticsToStatusFile()

writeStatisticsToStatusFile() : void

Write statistics data to disk.

writeConnectionsStatisticsToStatusFile()

writeConnectionsStatisticsToStatusFile() : void

Write statistics data to disk.

getErrorType()

getErrorType(integer  $type) : string

Get error message by error code.

Parameters

integer $type

Returns

string

generateConnectionId()

generateConnectionId() : integer

生成connection id

Returns

integer

sendToWorker()

sendToWorker(integer  $cmd, \Workerman\Connection\TcpConnection  $connection, mixed  $body = '') : boolean

发送数据给 worker 进程

Parameters

integer $cmd
\Workerman\Connection\TcpConnection $connection
mixed $body

Returns

boolean

preEncodeForClient()

preEncodeForClient(mixed  $data) : string

Parameters

mixed $data

Returns

string

outputStream()

outputStream(null  $stream = null) : boolean|resource

Parameters

null $stream

Returns

boolean|resource