Skip to content

Commit

Permalink
add comment and fix small problems
Browse files Browse the repository at this point in the history
  • Loading branch information
liangdong committed Oct 24, 2012
1 parent e89d8d1 commit 0951889
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
18 changes: 11 additions & 7 deletions src/listener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@

Listener::Listener(const std::string &ip, short port)
{
m_event = NULL;
m_event = NULL;
m_count_client = 0;

m_listen_addr.sin_family = AF_INET;
m_listen_addr.sin_port = htons(port);
//ipv4 only
m_listen_addr.sin_family = AF_INET;
m_listen_addr.sin_port = htons(port);
m_listen_addr.sin_addr.s_addr = inet_addr(ip.c_str());
}

Listener::~Listener()
{
if (m_event)
{
close(m_sockfd);
//first event_free, close socket secondly
event_free(m_event);
close(m_sockfd);
}
}

Expand All @@ -31,8 +34,8 @@ void Listener::InitListener(Server *server)
bind(m_sockfd, (struct sockaddr*)&m_listen_addr, sizeof(m_listen_addr));
listen(m_sockfd, 5);

m_server = server;
m_event = event_new(m_server->m_server_base, m_sockfd, EV_READ | EV_PERSIST, Listener::ListenerEventCallback, this);
m_server = server;
m_event = event_new(m_server->m_server_base, m_sockfd, EV_READ | EV_PERSIST, Listener::ListenerEventCallback, this);
event_add(m_event, NULL);
}

Expand Down Expand Up @@ -63,7 +66,8 @@ void Listener::ListenerEventCallback(evutil_socket_t sockfd, short event, void *

if (errno != EAGAIN && errno != EINTR)
{
event_base_loopexit(listener->m_server->m_server_base, NULL);
//exit event loop next round
event_base_loopexit(listener->m_server->m_server_base, NULL);
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

int main(int argc, char * const argv[])
{
Server server("0.0.0.0", 10000);
Server server("0.0.0.0", 10000);

std::cout << "Server Start" << std::endl;
server.StartServer();

server.StartServer(); //Start From Here :)

return 0;
}
4 changes: 3 additions & 1 deletion src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ Server::~Server()
++ iter;
}

// plugins can't be remove until client's plugin_data_slots has been clear by plugins
// plugins can't be remove until client's plugin_data_slots has been cleared by plugins
RemovePlugins();

event_free(m_exit_event);
event_base_free(m_server_base);

std::cerr << "Total Client: " << m_listener.m_count_client << std::endl;
}

std::cerr << "Server Closed" << std::endl;
Expand Down

0 comments on commit 0951889

Please sign in to comment.