1 Star 0 Fork 0

d724789975 / GmSocket

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
FxListenSocket.php 2.86 KB
一键复制 编辑 原始数据 按行查看 历史
724789975 提交于 2016-04-24 23:22 . fix
<?php
require_once("FxMySocket.php");
require_once("FxNet.php");
require_once("FxConnectSocket.php");
require_once("log.php");
class FxListenSocket extends FxMySocket
{
function FxListenSocket()
{
$this->m_ConnectionFactroy = NULL;
}
public function Initialize($strIp, $dwPort, $pConnectionFactroy)
{
$this->SetSocket(socket_create(AF_INET, SOCK_STREAM, 0));
if (socket_set_option($this->GetSocket(), SOL_SOCKET, SO_REUSEADDR, 1) === FALSE)
{
MyLog::crt("socket_set_option() failed, reason : " . socket_strerror(socket_last_error($this->GetSocket())) . ", error no : " . socket_last_error($this->GetSocket()));
return false;
}
if(false === socket_bind($this->GetSocket(), $strIp, $dwPort))
{
MyLog::crt("socket_bind() failed, reason : " . socket_strerror(socket_last_error($this->GetSocket())) . ", error no : " . socket_last_error($this->GetSocket()));
return false;
}
if(false === socket_listen($this->GetSocket(), 16))
{
MyLog::crt("socket_listen() failed, reason : " . socket_strerror(socket_last_error($this->GetSocket())) . ", error no : " . socket_last_error($this->GetSocket()));
return false;
}
if(false === socket_set_option($this->GetSocket(), SOL_SOCKET, SO_REUSEADDR, 1))
{
MyLog::crt("socket_set_option() failed, reason : " . socket_strerror(socket_last_error($this->GetSocket())) . ", error no : " . socket_last_error($this->GetSocket()));
return false;
}
if(false === socket_set_nonblock($this->GetSocket()))
{
MyLog::crt("socket_set_nonblock() failed, reason : " . socket_strerror(socket_last_error($this->GetSocket())) . ", error no : " . socket_last_error($this->GetSocket()));
return false;
}
$this->m_ConnectionFactroy = $pConnectionFactroy;
MyLog::dbg("listen ip : " . $strIp . ", port : " . $dwPort . ", fd : " . print_r($this->GetSocket(), true));
return true;
}
public function OnRead()
{
if(($dwConnectSocket = socket_accept($this->GetSocket())) !== false)
{
if (socket_set_option($this->GetSocket(), SOL_SOCKET, SO_REUSEADDR, 1) === FALSE)
{
MyLog::crt("socket_set_option() failed, reason : " . socket_strerror(socket_last_error($this->GetSocket())) . ", error no : " . socket_last_error($this->GetSocket()));
return false;
}
if(socket_set_nonblock($dwConnectSocket) === false)
{
MyLog::crt("socket_set_nonblock() failed, reason : " . socket_strerror(socket_last_error($dwConnectSocket)) . ", error no : " . socket_last_error($dwConnectSocket));
socket_close($dwConnectSocket);
return;
}
$hConnectSocket = $this->m_ConnectionFactroy->CreateConnection();
$hConnectSocket->SetSocket($dwConnectSocket);
FxNet::Instance()->AddSocket($hConnectSocket);
MyLog::dbg(print_r($this->GetSocket(), true) . " accept socket fd : " . print_r($dwConnectSocket, true));
}
}
public function OnWrite()
{
}
private $m_ConnectionFactroy;
}
?>
PHP
1
https://gitee.com/qq724789975/GmSocket.git
git@gitee.com:qq724789975/GmSocket.git
qq724789975
GmSocket
GmSocket
master

搜索帮助