#pragma once
#include "EventLoop.h"
#include "Acceptor.h"
#include "InetAddress.h"
#include "noncopyable.h"
#include "EventLoopThreadPool.h"
#include "Callbacks.h"
#include "TcpConnection.h"
#include "Buffer.h"
#include <functional>
#include <string>
#include <memory>
#include <atomic>
#include <unordered_map>
class TcpServer : noncopyable
{
public:
using ThreadInitCallback = std::function<void(EventLoop*)>;
enum Option
{
kNoReusePort,
kReusePort,
};
TcpServer(EventLoop *loop,
const InetAddress &listenAddr,
const std::string &nameArg,
Option option = kNoReusePort);
~TcpServer();
void setThreadInitcallback(const ThreadInitCallback &cb) { threadInitCallback_ = cb; }
void setConnectionCallback(const ConnectionCallback &cb) { connectionCallback_ = cb; }
void setMessageCallback(const MessageCallback &cb) { messageCallback_ = cb; }
void setWriteCompleteCallback(const WriteCompleteCallback &cb) { writeCompleteCallback_ = cb; }
void setThreadNum(int numThreads);
void start();
private:
void newConnection(int sockfd, const InetAddress &peerAddr);
void removeConnection(const TcpConnectionPtr &conn);
void removeConnectionInLoop(const TcpConnectionPtr &conn);
using ConnectionMap = std::unordered_map<std::string, TcpConnectionPtr>;
EventLoop *loop_;
const std::string ipPort_;
const std::string name_;
std::unique_ptr<Acceptor> acceptor_;
std::shared_ptr<EventLoopThreadPool> threadPool_;
ConnectionCallback connectionCallback_;
MessageCallback messageCallback_;
WriteCompleteCallback writeCompleteCallback_;
ThreadInitCallback threadInitCallback_;
std::atomic_int started_;
int nextConnId_;
ConnectionMap connections_;
};
#include"TcpServer.h"
#include"Logger.h"
#include"EventLoop.h"
#include"Acceptor.h"
#include"EventLoopThreadPool.h"
#include"InetAddress.h"
#include"TcpConnection.h"
#include<strings.h>
#include<functional>
static EventLoop*CheckLoopNotNull(EventLoop*loop)
{
if(loop==nullptr)
{
LOG_FATAL("%s:%s:%d baseloop is a nullptr\n",__FILE__,__func__,__LINE__);
}
return loop;
}
TcpServer::TcpServer(EventLoop *loop,
const InetAddress &listenAddr,
const std::string &nameArg,
Option option )
: loop_(CheckLoopNotNull(loop))
, ipPort_(listenAddr.toIpPort())
, name_(nameArg)
, acceptor_(new Acceptor(loop,listenAddr,option==kReusePort))
, threadPool_(new EventLoopThreadPool(loop,name_))
, connectionCallback_()
, messageCallback_()
, nextConnId_(1)
, started_(0)
{
acceptor_->setNewConnectionCallback(std::bind(&TcpServer::newConnection,this,
std::placeholders::_1,std::placeholders::_2));
LOG_INFO("in tcpserver:accpetor bind newconnectioncallback!!!!!!!!!!!!\n");
}
TcpServer::~TcpServer()
{
for(auto &item : connections_)
{
TcpConnectionPtr conn(item.second);
item.second.reset();
conn->getLoop()->runInLoop(
std::bind(&TcpConnection::connectDestroyed,conn)
);
}
}
void TcpServer::setThreadNum(int numThreads)
{
threadPool_->setThreadNum(numThreads);
}
void TcpServer::start()
{
if(started_++ ==0)
{
threadPool_->start(threadInitCallback_);
loop_->runInLoop(std::bind(&Acceptor::listen,acceptor_.get()));
}
}
void TcpServer:: newConnection(int sockfd,const InetAddress &peerAddr)
{
LOG_INFO("tcpserver is going to newConnection!!!!!!!!!!!!!!!\n");
EventLoop *ioLoop=threadPool_->getNextLoop();
char buf[64]={0};
snprintf(buf,sizeof buf,"-%s#%d",ipPort_.c_str(),nextConnId_);
++nextConnId_;
std::string connName = name_ + buf;
LOG_INFO("TcpServer::newConnection [%s] - new connection [%s] from %s \n",
name_.c_str(),connName.c_str(),peerAddr.toIpPort().c_str());
sockaddr_in local;
::bzero(&local,sizeof local);
socklen_t addrlen = sizeof local;
if(::getsockname(sockfd,(sockaddr*)&local,&addrlen)<0)
{
LOG_ERROR("sockets::getlocalAddr \n");
}
InetAddress localAddr(local);
TcpConnectionPtr conn(new TcpConnection(
ioLoop,
connName,
sockfd,
localAddr,
peerAddr ));
connections_[connName] = conn;
conn->setConnectionCallback(connectionCallback_);
conn->setMessageCallback(messageCallback_);
LOG_INFO("tcpserver set tcpconnection messagexcallback success!\n");
conn->setWriteCompleteCallback(writeCompleteCallback_);
conn->setCloseCallback(
std::bind(&TcpServer::removeConnection,this,std::placeholders::_1)
);
ioLoop->runInLoop(std::bind(&TcpConnection::connectEstablished,conn));
}
void TcpServer::removeConnection(const TcpConnectionPtr &conn)
{
loop_->runInLoop(
std::bind(&TcpServer::removeConnectionInLoop,this,conn)
);
}
void TcpServer::removeConnectionInLoop(const TcpConnectionPtr &conn)
{
LOG_INFO("TcpServer::removeConnectionInLoop [%s] - connection %s\n",
name_.c_str(),conn->name().c_str());
connections_.erase(conn->name());
EventLoop*ioLoop=conn->getLoop();
ioLoop->queueInLoop(
std::bind(&TcpConnection::connectDestroyed,conn)
);
}