修复连接地址错误
This commit is contained in:
parent
6cfb31e1b9
commit
771e4a8684
@ -33,15 +33,24 @@ ConnectionLibevent::ConnectionLibevent(struct bufferevent* ev, uint32_t fd, stru
|
|||||||
m_fd(-1),
|
m_fd(-1),
|
||||||
m_addr(nullptr)
|
m_addr(nullptr)
|
||||||
{
|
{
|
||||||
|
|
||||||
m_event = ev;
|
m_event = ev;
|
||||||
m_fd = fd;
|
m_fd = fd;
|
||||||
m_addr = p1;
|
m_addr = p1;
|
||||||
|
std::cout << "\r\n " << m_addr << " ConnectionLibevent " << inet_ntoa(m_addr->sin_addr) << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
ConnectionLibevent* defaultConnAccept(struct bufferevent* ev, uint32_t fd, struct sockaddr_in* p1) {
|
ConnectionLibevent* defaultConnAccept(struct bufferevent* ev, uint32_t fd, struct sockaddr_in* p1) {
|
||||||
|
std::cout << "defaultConnAccept " << inet_ntoa(p1->sin_addr);
|
||||||
return new ConnectionLibevent(ev, fd, p1);
|
return new ConnectionLibevent(ev, fd, p1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void defaultConnClose(ConnectionLibevent*p) {
|
||||||
|
std::cout << "defaultConnClose close connection " << p->IpAddress() << p->SocketFd()<<std::endl;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int ConnectionLibevent::OnRecv(char* p, uint32_t len) {
|
int ConnectionLibevent::OnRecv(char* p, uint32_t len) {
|
||||||
std::cout << "OnRecv " << p << std::endl;
|
std::cout << "OnRecv " << p << std::endl;
|
||||||
m_bytes_recv += len;
|
m_bytes_recv += len;
|
||||||
@ -84,10 +93,9 @@ int ConnectionLibevent::SetServer(TcpServerLibevent* p) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
string ConnectionLibevent::IpAddress() {
|
string ConnectionLibevent::IpAddress() {
|
||||||
if (nullptr != m_addr) {
|
|
||||||
return string(inet_ntoa(m_addr->sin_addr));
|
std::cout<< m_addr << " IpAddress: " << inet_ntoa(m_addr->sin_addr)<<"port " << htons(m_addr->sin_port) << std::endl;
|
||||||
}
|
return string(inet_ntoa(this->m_addr->sin_addr));
|
||||||
return "";
|
|
||||||
}
|
}
|
||||||
TcpServerLibevent* ConnectionLibevent::Server() {
|
TcpServerLibevent* ConnectionLibevent::Server() {
|
||||||
return m_parent_server;
|
return m_parent_server;
|
||||||
@ -96,16 +104,16 @@ TcpServerLibevent* ConnectionLibevent::Server() {
|
|||||||
void read_cb(struct bufferevent* bev, void* arg)
|
void read_cb(struct bufferevent* bev, void* arg)
|
||||||
{
|
{
|
||||||
char buf[1024] = { 0 };
|
char buf[1024] = { 0 };
|
||||||
ConnectionLibevent* conn = (ConnectionLibevent*)arg;
|
ConnectionLibevent* conn = static_cast<ConnectionLibevent*> (arg);
|
||||||
bufferevent_read(bev, buf, sizeof(buf));
|
bufferevent_read(bev, buf, sizeof(buf));
|
||||||
cout << "client " << conn->IpAddress() << " say:" << buf << endl;
|
cout << "client " << conn->IpAddress().c_str() << " say:" << buf << endl;
|
||||||
conn->OnRecv(buf, sizeof(buf));
|
conn->OnRecv(buf, sizeof(buf));
|
||||||
}
|
}
|
||||||
|
|
||||||
void write_cb(struct bufferevent* bev, void* arg)
|
void write_cb(struct bufferevent* bev, void* arg)
|
||||||
{
|
{
|
||||||
ConnectionLibevent* conn = (ConnectionLibevent*)arg;
|
ConnectionLibevent* conn = (ConnectionLibevent*)arg;
|
||||||
std::cout << "connection " << conn->IpAddress() << " sended data success" << std::endl;
|
std::cout << "connection " << conn->IpAddress() << " sended data " << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void event_cb(struct bufferevent* bev, short events, void* arg)
|
void event_cb(struct bufferevent* bev, short events, void* arg)
|
||||||
@ -131,11 +139,14 @@ void event_cb(struct bufferevent* bev, short events, void* arg)
|
|||||||
//delete conn;
|
//delete conn;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ServerCallbacks::cb_listener(struct evconnlistener* listener, evutil_socket_t fd, struct sockaddr* addr, int len, void* ptr)
|
void ServerCallbacks::cb_listener(struct evconnlistener* listener, evutil_socket_t fd,
|
||||||
|
struct sockaddr* addr, int len, void* ptr)
|
||||||
{
|
{
|
||||||
struct sockaddr_in* client = (sockaddr_in*)addr;
|
struct sockaddr_in* client = new(struct sockaddr_in);
|
||||||
|
memcpy(client, addr, sizeof(struct sockaddr));
|
||||||
|
|
||||||
cout << "connect new client: " << inet_ntoa(client->sin_addr)
|
cout << "connect new client: " << inet_ntoa(client->sin_addr)
|
||||||
<< " " << fd << " ::" << ntohs(client->sin_port) << endl;
|
<< " port: " << " ::" << ntohs(client->sin_port) << endl;
|
||||||
TcpServerLibevent* server = (TcpServerLibevent*)ptr;
|
TcpServerLibevent* server = (TcpServerLibevent*)ptr;
|
||||||
if (server != nullptr) {
|
if (server != nullptr) {
|
||||||
std::cout << "null 2" << std::endl;
|
std::cout << "null 2" << std::endl;
|
||||||
@ -232,6 +243,7 @@ int TcpServerLibevent::AddConnection(uint32_t fd, ConnectionLibevent* p) {
|
|||||||
int TcpServerLibevent::RemoveConnection(uint32_t fd) {
|
int TcpServerLibevent::RemoveConnection(uint32_t fd) {
|
||||||
if (m_map_client.find(fd) != m_map_client.end()) {
|
if (m_map_client.find(fd) != m_map_client.end()) {
|
||||||
auto pClient = m_map_client[fd];
|
auto pClient = m_map_client[fd];
|
||||||
|
this->m_handle_disconnect(pClient);
|
||||||
m_map_client.erase(fd);
|
m_map_client.erase(fd);
|
||||||
delete pClient;
|
delete pClient;
|
||||||
return 0;
|
return 0;
|
||||||
@ -264,6 +276,7 @@ TcpServerLibevent::TcpServerLibevent(int port, string bindip) :
|
|||||||
m_event_listener(nullptr)
|
m_event_listener(nullptr)
|
||||||
{
|
{
|
||||||
m_handle_accept = defaultConnAccept;
|
m_handle_accept = defaultConnAccept;
|
||||||
|
m_handle_disconnect = defaultConnClose;
|
||||||
m_backlog = 10000;
|
m_backlog = 10000;
|
||||||
this->m_bind_ip = bindip;
|
this->m_bind_ip = bindip;
|
||||||
this->m_port = port;
|
this->m_port = port;
|
||||||
@ -294,6 +307,7 @@ TcpServerLibevent::TcpServerLibevent(int port, string bindip) :
|
|||||||
m_status = STOP;
|
m_status = STOP;
|
||||||
std::cout << "3" << std::endl;
|
std::cout << "3" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description: start server synchronous
|
* @description: start server synchronous
|
||||||
* @param {*}
|
* @param {*}
|
||||||
|
Loading…
Reference in New Issue
Block a user