1 Star 0 Fork 0

d724789975 / GmSocket

Create your Gitee Account
Explore and code with more than 8 million developers,Free private repositories !:)
Sign up
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
FxListenSocket.php 2.86 KB
Copy Edit Web IDE Raw Blame History
724789975 authored 2016-04-24 23:22 . Loading......
<?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://git.oschina.net/qq724789975/GmSocket.git
git@git.oschina.net:qq724789975/GmSocket.git
qq724789975
GmSocket
GmSocket
master

Search