Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2020
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.00 KB | None | 0 0
  1. #include "Socket.h"
  2. #include "Define.h"
  3. #include "Server.h"
  4.  
  5. using namespace Srv;
  6.  
  7. Socket::Socket(Server* server, int32 epfd, int32 socket): m_server(server), m_epfd(epfd), m_socket(socket), m_closed(false)
  8. {
  9.     m_server->log(LOG_DEBUG, "Socket create");
  10.     m_ev.data.fd = m_socket;
  11.     m_buf_input.clear();
  12.     m_buf_output.clear();
  13.     m_session = new SocketSession(m_server, this);
  14. }
  15.  
  16. Socket::~Socket()
  17. {
  18.     m_server->log(LOG_DEBUG, "Socket destroy");
  19.     if (m_session) {
  20.         m_session->setSocket(NULL);
  21.         m_session = NULL;
  22.     }
  23.     shutdown(m_socket, SHUT_RDWR);
  24.     ::close(m_socket);
  25. }
  26.  
  27. bool Socket::epollAdd(uint32 events)
  28. {
  29.     m_ev.events = events;
  30.     if (!epoll_ctl(m_epfd, EPOLL_CTL_ADD, m_socket, &m_ev) < 0)
  31.     {
  32.         m_server->log(LOG_ERROR, "EPOLL_CTL_ADD");
  33.         return false;
  34.     }
  35.     return true;
  36. }
  37.  
  38. bool Socket::epollMod(uint32 events)
  39. {
  40.     m_ev.events = events;
  41.     if (!epoll_ctl(m_epfd, EPOLL_CTL_MOD, m_socket, &m_ev) < 0)
  42.     {
  43.         m_server->log(LOG_ERROR, "EPOLL_CTL_MOD");
  44.         return false;
  45.     }
  46.     return true;
  47. }
  48.  
  49. bool Socket::epollDel()
  50. {
  51.     if (!epoll_ctl(m_epfd, EPOLL_CTL_DEL, m_socket, &m_ev) < 0)
  52.     {
  53.         m_server->log(LOG_ERROR, "EPOLL_CTL_DEL");
  54.         return false;
  55.     }
  56.     return true;
  57. }
  58.  
  59. void Socket::close()
  60. {
  61.     if (m_closed)
  62.         return;
  63.     m_closed = true;
  64.     if (!epollDel()) {
  65.         m_server->log(LOG_ERROR, "SocketThread::close EPOLL_CTL_DEL");
  66.     }
  67. }
  68.  
  69. bool Socket::onRead()
  70. {
  71.     char buf[BUFF_SIZE];
  72.     int32 len = 0;
  73.     int32 readed = 0;
  74.     while( (readed = ::recv(m_socket, &buf[0], BUFF_SIZE, 0)) > 0 )
  75.     {
  76.         m_buf_input.append(&buf[0], readed);
  77.         len += readed;
  78.     }
  79.  
  80.     if (len <= 0) {
  81.         m_server->log(LOG_DEBUG, "SocketSession::onData() from socket(%i) len = %i, errno = %i", m_socket, len, errno);
  82.         if (len == 0 || (errno != EWOULDBLOCK && errno != EAGAIN))
  83.             return false;
  84.     }
  85.  
  86.     return m_session->onRead();
  87. }
  88.  
  89. bool Socket::recv(char* buf, uint32 len, bool soft)
  90. {
  91.     if (len > m_buf_input.size())
  92.         return false;
  93.    
  94.     if (soft) {
  95.         size_t p = m_buf_input.rpos();
  96.         m_buf_input.read((uint8*)&buf[0], len);
  97.         m_buf_input.rpos(p);
  98.     } else {
  99.         m_buf_input.pop((uint8*)&buf[0], len);
  100.     }
  101.    
  102.     return true;
  103. }
  104.  
  105. bool Socket::recv(ByteBuffer &buf, uint32 len, bool soft)
  106. {
  107.     if (len > m_buf_input.size())
  108.         return false;
  109.    
  110.     if (soft) {
  111.         size_t p = m_buf_input.rpos();
  112.         m_buf_input.read(buf, len);
  113.         m_buf_input.rpos(p);
  114.     } else {
  115.         m_buf_input.pop(buf, len);
  116.     }
  117.    
  118.     return true;
  119. }
  120.  
  121. bool Socket::onWrite()
  122. {
  123.     if (isClosed())
  124.         return false;
  125.    
  126.     m_session->onWrite(m_buf_output);
  127.  
  128.     if (!m_buf_output.empty()) {
  129.         int32 len = ::send(m_socket, m_buf_output.contents(), m_buf_output.size(), 0); //MSG_NOSIGNAL
  130.  
  131.         if (len <= 0)
  132.         {
  133.             if (len == 0 || (errno != EWOULDBLOCK && errno != EAGAIN))
  134.                 return false;
  135.         }
  136.         else if (len < m_buf_output.size())
  137.         {
  138.             m_buf_output.pop((uint8*)NULL, len);
  139.         }
  140.         else
  141.         {
  142.             m_buf_output.clear();
  143.         }
  144.     }
  145.     if (m_buf_output.empty())
  146.         epollMod(EPOLLIN | EPOLLERR | EPOLLHUP);
  147.     return true;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement