Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Socket.h"
- #include "Define.h"
- #include "Server.h"
- using namespace Srv;
- Socket::Socket(Server* server, int32 epfd, int32 socket): m_server(server), m_epfd(epfd), m_socket(socket), m_closed(false)
- {
- m_server->log(LOG_DEBUG, "Socket create");
- m_ev.data.fd = m_socket;
- m_buf_input.clear();
- m_buf_output.clear();
- m_session = new SocketSession(m_server, this);
- }
- Socket::~Socket()
- {
- m_server->log(LOG_DEBUG, "Socket destroy");
- if (m_session) {
- m_session->setSocket(NULL);
- m_session = NULL;
- }
- shutdown(m_socket, SHUT_RDWR);
- ::close(m_socket);
- }
- bool Socket::epollAdd(uint32 events)
- {
- m_ev.events = events;
- if (!epoll_ctl(m_epfd, EPOLL_CTL_ADD, m_socket, &m_ev) < 0)
- {
- m_server->log(LOG_ERROR, "EPOLL_CTL_ADD");
- return false;
- }
- return true;
- }
- bool Socket::epollMod(uint32 events)
- {
- m_ev.events = events;
- if (!epoll_ctl(m_epfd, EPOLL_CTL_MOD, m_socket, &m_ev) < 0)
- {
- m_server->log(LOG_ERROR, "EPOLL_CTL_MOD");
- return false;
- }
- return true;
- }
- bool Socket::epollDel()
- {
- if (!epoll_ctl(m_epfd, EPOLL_CTL_DEL, m_socket, &m_ev) < 0)
- {
- m_server->log(LOG_ERROR, "EPOLL_CTL_DEL");
- return false;
- }
- return true;
- }
- void Socket::close()
- {
- if (m_closed)
- return;
- m_closed = true;
- if (!epollDel()) {
- m_server->log(LOG_ERROR, "SocketThread::close EPOLL_CTL_DEL");
- }
- }
- bool Socket::onRead()
- {
- char buf[BUFF_SIZE];
- int32 len = 0;
- int32 readed = 0;
- while( (readed = ::recv(m_socket, &buf[0], BUFF_SIZE, 0)) > 0 )
- {
- m_buf_input.append(&buf[0], readed);
- len += readed;
- }
- if (len <= 0) {
- m_server->log(LOG_DEBUG, "SocketSession::onData() from socket(%i) len = %i, errno = %i", m_socket, len, errno);
- if (len == 0 || (errno != EWOULDBLOCK && errno != EAGAIN))
- return false;
- }
- return m_session->onRead();
- }
- bool Socket::recv(char* buf, uint32 len, bool soft)
- {
- if (len > m_buf_input.size())
- return false;
- if (soft) {
- size_t p = m_buf_input.rpos();
- m_buf_input.read((uint8*)&buf[0], len);
- m_buf_input.rpos(p);
- } else {
- m_buf_input.pop((uint8*)&buf[0], len);
- }
- return true;
- }
- bool Socket::recv(ByteBuffer &buf, uint32 len, bool soft)
- {
- if (len > m_buf_input.size())
- return false;
- if (soft) {
- size_t p = m_buf_input.rpos();
- m_buf_input.read(buf, len);
- m_buf_input.rpos(p);
- } else {
- m_buf_input.pop(buf, len);
- }
- return true;
- }
- bool Socket::onWrite()
- {
- if (isClosed())
- return false;
- m_session->onWrite(m_buf_output);
- if (!m_buf_output.empty()) {
- int32 len = ::send(m_socket, m_buf_output.contents(), m_buf_output.size(), 0); //MSG_NOSIGNAL
- if (len <= 0)
- {
- if (len == 0 || (errno != EWOULDBLOCK && errno != EAGAIN))
- return false;
- }
- else if (len < m_buf_output.size())
- {
- m_buf_output.pop((uint8*)NULL, len);
- }
- else
- {
- m_buf_output.clear();
- }
- }
- if (m_buf_output.empty())
- epollMod(EPOLLIN | EPOLLERR | EPOLLHUP);
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement