bool Socket::Close() {
if (!m_event) {
sys_err("Tried to close socket while m_event is a nullptr");
return false;
}
evutil_socket_t fd = bufferevent_getfd(m_event);
if (fd < 0) {
sys_err("Tried to close socket while m_event is not
connected (fd < 0)");
return false;
}
if (evutil_closesocket(fd) < 0) {
int err = evutil_socket_geterror(fd);
sys_err("Closing the socket %d failed. %s errorcode (%d)",
fd, evutil_socket_error_to_string(err), err);
return false;
}
// Simulate EOf event so we can handle disconnect properly
OnEvent(BEV_EVENT_EOF);
return true;
}