代码拉取完成,页面将自动刷新
同步操作将从 harris2016/oneproxy-monitor 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* @ClassName: tcpserver.cpp
* @Description: TODO
* All rights Reserved, Designed By huih
* @Company: onexsoft
* @Author: hui
* @Version: V1.0
* @Date: 2016年8月11日
*
*/
#include <string.h>
#include <stdio.h>
#include "tcpserver.h"
#include "record.h"
#include "util/logger.h"
#include "util/systemapi.h"
#include "util/tool.h"
TcpServer::TcpServer():
ioEvent(IoEvent::get_instance("tcpServer_ioevent"))
{
}
TcpServer::TcpServer(std::string serverAddr, unsigned int serverPort):
ioEvent(IoEvent::get_instance("tcpServer_ioevent"))
{
this->servSct.push_back(NetworkSocket(serverAddr, serverPort));
}
TcpServer::~TcpServer()
{
if (this->ioEvent != NULL) {
this->ioEvent->destory_instance();
}
}
int TcpServer::create_tcpServer() {
//1. create server socket;
if (this->create_servers() < 0) {
logs(Logger::ERR, "create server error");
return -1;
}
//2. add server to epoll
std::vector<NetworkSocket>::iterator it = this->servSct.begin();
for (; it != this->servSct.end(); ++it) {
if (it->get_fd() > 0) {
this->ioEvent->add_ioEventAccept(it->get_fd(), TcpServer::accept_connect, this);
this->fdPortMap[it->get_fd()] = it->get_port();
}
}
return 0;
}
void TcpServer::run_server(int timeout) {
if (this->ioEvent != NULL)
this->ioEvent->run_loopWithTimeout(timeout);
}
void TcpServer::set_tcpServer(std::string serverAddr, std::set<unsigned int>& portList)
{
std::set<unsigned int>::iterator it = portList.begin();
for (; it != portList.end(); ++it) {
this->servSct.push_back(NetworkSocket(serverAddr, *it));
}
}
void TcpServer::stop_tcpServer()
{
std::vector<NetworkSocket>::iterator it = this->servSct.begin();
for (; it != this->servSct.end(); ++it) {
it->closeSocket(it->get_fd());
}
this->servSct.clear();
this->fdPortMap.clear();
if (this->ioEvent != NULL) {
this->ioEvent->destory_instance();
this->ioEvent = NULL;
}
}
int TcpServer::create_servers()
{
std::vector<NetworkSocket>::iterator it = this->servSct.begin();
for (; it != this->servSct.end(); ++it) {
NetworkSocket& ns = (*it);
//1. parse address
if (ns.parse_address() < 0) {
logs(Logger::ERR, "parse address error");
return -1;
}
//2. create listen socket
if (this->create_listenSocket(ns, (int)(ns.get_addr().sa.sa_family), &ns.get_addr().sa, sizeof(ns.get_addr().sa)) < 0) {
logs(Logger::ERR, "create listen socket error, port: %u", it->get_port());
return -1;
}
}
return 0;
}
int TcpServer::create_listenSocket(NetworkSocket& ns, int af, const struct sockaddr *sa, int salen)
{
int sfd = 0;
if ((sfd = ::socket(PF_INET, SOCK_STREAM, 0)) <= 0) {
logs(Logger::ERR, "create socket error");
return -1;
}
if (af != AF_UNIX) {
if (ns.set_sockReUseAddr(sfd) < 0) {
logs(Logger::ERR, "set resueaddr error");
ns.closeSocket(sfd);
return -1;
}
if (ns.set_sockReUsePort(sfd) < 0) {
logs(Logger::ERR, "set socket reuseport error");
ns.closeSocket(sfd);
return -1;
}
}
if (bind(sfd, sa, salen) < 0) {
int errorno = SystemApi::system_errno();
logs(Logger::ERR, "bind socket(%d) to address error(%d:%s)", sfd, errorno, SystemApi::system_strerror(errorno));
ns.closeSocket(sfd);
return -1;
}
if (ns.set_sockCommonOpt(sfd, af == AF_UNIX) < 0) {
logs(Logger::ERR, "set common option error");
ns.closeSocket(sfd);
return -1;
}
if (::listen(sfd, cf_listen_backlog) < 0) {
int errorno = SystemApi::system_errno();
logs(Logger::ERR, "listen error(%d:%s)", errorno, SystemApi::system_strerror(errorno));
ns.closeSocket(sfd);
return -1;
}
ns.set_fd(sfd);
return 0;
}
NetworkSocket* TcpServer::accept_connect(unsigned int sfd)
{
struct sockaddr client_addr;
socklen_t client_addr_len = sizeof(client_addr);
int cfd = 0;
if ((cfd = accept(sfd, &client_addr, &client_addr_len)) < 0) {
logs(Logger::ERR, "call accept error(%s)", SystemApi::system_strerror());
return NULL;
}
uif(SystemApi::system_setSockNonblocking(cfd, true)) {
logs(Logger::ERR, "set socket(%d) non blocking error(%s)", cfd, SystemApi::system_strerror());
SystemApi::close(cfd);
return NULL;
}
NetworkSocket *clientSocket = new NetworkSocket();
clientSocket->addr_assign(&client_addr);
clientSocket->addr_ntop();
clientSocket->set_fd(cfd);
clientSocket->get_attachData().set_listenPort(this->fdPortMap[sfd]);
logs(Logger::INFO, "accept client fd(%d) address (%s) port(%d) listenPort(%d)", cfd,
clientSocket->get_address().c_str(), clientSocket->get_port(),
clientSocket->get_attachData().get_listenPort());
return clientSocket;
}
void TcpServer::accept_connect(unsigned int fd, unsigned int events, void *args)
{
TcpServer *ts = (TcpServer*)args;
uif (ts == NULL)
return;
NetworkSocket *clientSocket = ts->accept_connect(fd);
uif (clientSocket == NULL) {
logs(Logger::ERR, "accept client error");
return;
}
ts->accept_clientRequest(clientSocket);
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。