本文實(shí)例講述了php實(shí)現(xiàn)的簡(jiǎn)單多進(jìn)程服務(wù)器類(lèi)。分享給大家供大家參考,具體如下:
php寫(xiě)的一個(gè)簡(jiǎn)單的多進(jìn)程服務(wù)器。
<?phpclass server{ public $port; public $ip; protected $server; public function __construct($ip = '0.0.0.0', $port) { $this->ip = $ip; $this->port = $port; $this->createSocket(); //創(chuàng)建一個(gè)通訊節(jié)點(diǎn) } public function listen($callback) { if(!is_callable($callback)){ throw new Exception('不是閉包,請(qǐng)傳遞正確的參數(shù)'); } //只要我們接收到客戶(hù)端的數(shù)據(jù),就fork一個(gè)子進(jìn)程處理 while ($client = socket_accept($this->server)) { //等待客戶(hù)端接入,返回的是客戶(hù)端的連接 $buf = socket_read($client, 1024); //讀取客戶(hù)端內(nèi)容 $pid=pcntl_fork(); //創(chuàng)建子進(jìn)程 //父進(jìn)程和子進(jìn)程都會(huì)執(zhí)行下面代碼 if ($pid == -1) { //錯(cuò)誤處理:創(chuàng)建子進(jìn)程失敗時(shí)返回-1. die('could not fork'); } else if ($pid) { //父進(jìn)程會(huì)得到子進(jìn)程號(hào),所以這里是父進(jìn)程執(zhí)行的邏輯 var_dump('父進(jìn)程',$pid); pcntl_wait($status); //等待子進(jìn)程中斷,防止子進(jìn)程成為僵尸進(jìn)程。 } else { //子進(jìn)程得到的$pid為0, 所以這里是子進(jìn)程執(zhí)行的邏輯。 //睡眠 if($this->checkRule("/sleep/i",$buf)){ sleep(10); $this->response('休眠10S',$client); socket_close($client); return; } //請(qǐng)求過(guò)濾 if(empty($this->checkRule("/GET/s(.*?)/sHTTP//1.1/i",$buf))){ socket_close($client); return; } //響應(yīng) $response= call_user_func($callback,$buf); //回調(diào)$callback函數(shù) $this->response($response,$client); usleep(1000); //微妙為單位,1000000 微妙等于1秒 socket_close($client); exit(); //直接退出 } }// while (true) {// $client = socket_accept($this->server); //等待客戶(hù)端接入,返回的是客戶(hù)端的連接// $buf = socket_read($client, 1024); //讀取客戶(hù)端內(nèi)容//// //睡眠// if($this->checkRule("/sleep/i",$buf)){// sleep(10);// $this->response('休眠10S',$client);// socket_close($client);// return;// }// //請(qǐng)求過(guò)濾// if(empty($this->checkRule("/GET/s(.*?)/sHTTP//1.1/i",$buf))){// socket_close($client);// return;// }//// //響應(yīng)// $response= call_user_func($callback,$buf); //回調(diào)$callback函數(shù)// $this->response($response,$client);// usleep(1000); //微妙為單位,1000000 微妙等于1秒// socket_close($client);//// } socket_close($this->server); } //io 復(fù)用 //epoll 模型 //多進(jìn)程 protected function createSocket() { $this->server = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); //bind socket_set_option($this->server, SOL_SOCKET, SO_REUSEADDR, 1); //復(fù)用還處于 TIME_WAIT socket_bind($this->server, $this->ip, $this->port); //細(xì)節(jié)性的處理自行完成 socket_listen($this->server); //開(kāi)始監(jiān)聽(tīng) } /** * 協(xié)議過(guò)濾 * @param $reg * @param $buf * @return mixed */ protected function checkRule($reg,$buf){ if(preg_match($reg,$buf,$matchs)){ return $matchs; } return false; } //請(qǐng)求處理類(lèi) public function request($buf){ //1.只允許http協(xié)議訪(fǎng)問(wèn)// if(preg_match("GET/s(.*?)/sHTTP/1.1",$buf,$matchs)){ //匹配到http協(xié)議// return true;// }else{// return false;// } //2.過(guò)濾掉/favicon.ico //3.獲取請(qǐng)求信息 } protected function response($content,$client){ //返回?cái)?shù)據(jù)給客戶(hù)端,響應(yīng)處理 $string="HTTP/1.1 200 OK/r/n"; $string.="Content-Type: text/html;charset=utf-8/r/n"; $string.="Content-Length: ".strlen($content)."/r/n/r/n"; socket_write($client,$string.$content); }}
新聞熱點(diǎn)
疑難解答