Skip to content

Commit

Permalink
v1.4.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Edward-Wu committed Mar 2, 2020
1 parent 89b31e6 commit 888bb19
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 173 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ v1.4.1
v1.4.2
1. add remote_ip and remote_port to on_event_url which can be as the unique identification for player or publisher.

v1.4.3
1. change the tcp'epoll mode to select mode for compatible MAC os.
2. modify the http check repeat bug for reopen.



83 changes: 35 additions & 48 deletions slscore/HttpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,6 @@ int CHttpClient::open(const char *url, const char *method, int interval)
this, m_remote_host, m_remote_port);
goto FUNC_END;
}
ret = init_epoll();
if (SLS_OK != ret) {
sls_log(SLS_LOG_INFO, "[%p]CHttpClient::open, init_epoll failed, remote_host='%s', remote_port=%d.",
this, m_remote_host, m_remote_port);
goto FUNC_END;
}
ret = add_to_epoll();
if (SLS_OK != ret) {
sls_log(SLS_LOG_INFO, "[%p]CHttpClient::open, add_to_epoll failed, remote_host='%s', remote_port=%d.",
this, m_remote_host, m_remote_port);
goto FUNC_END;
}
//send data
handler();
FUNC_END:
Expand All @@ -146,18 +134,6 @@ int CHttpClient::close()
int ret = SLS_OK;
sls_log(SLS_LOG_TRACE, "[%p]CHttpClient::close, m_url='%s', m_response_content_length=%d.",
this, m_url, m_response_info.m_response_content_length);

ret = remove_from_epoll();
if (SLS_OK != ret) {
sls_log(SLS_LOG_INFO, "[%p]CHttpClient::close, remove_from_epoll failed, remote_host='%s', remote_port=%d.",
this, m_remote_host, m_remote_port);
}
ret = uninit_epoll();
if (SLS_OK != ret) {
sls_log(SLS_LOG_INFO, "[%p]CHttpClient::close, uninit_epoll failed, remote_host='%s', remote_port=%d.",
this, m_remote_host, m_remote_port);
}

ret = CTCPRole::close();
if (NULL != m_callback) {
m_callback(this, HCT_CLOSE, &ret, m_callback_context);
Expand Down Expand Up @@ -209,9 +185,6 @@ int CHttpClient::check_timeout(int64_t cur_tm_ms)

int CHttpClient::check_repeat(int64_t cur_tm_ms)
{
if (0 == m_fd) {
return SLS_ERROR;
}
if (m_interval <= 0) {
//no repeat
return SLS_ERROR;
Expand Down Expand Up @@ -412,33 +385,47 @@ int CHttpClient::handler(DATA_PARAM *p)

int CHttpClient::handler()
{
int ret = 0;
int ready_count = 0;
struct epoll_event ready_events[1];
int timeout = 10;//10ms

if (!m_valid)
return 0;
ready_count = epoll_wait(m_eid, ready_events, 1, timeout);//等待事件发生
if (ready_count <= 0 || ready_events[0].data.fd != m_fd) {
int ret = 0;
int timeout = 10;//10ms

int sfd = m_fd;
fd_set read_fds;
fd_set write_fds;
struct timeval tout;
int max_sockfd;

FD_ZERO(&read_fds);
FD_SET(sfd, &read_fds);
FD_ZERO(&write_fds);
FD_SET(sfd, &write_fds);
max_sockfd = sfd;

if (!m_valid)
return 0;
tout.tv_sec = 0;
tout.tv_usec = timeout * 1000;
ret = select (max_sockfd + 1, &read_fds, &write_fds, NULL, &tout);
if (ret == 0) {
return SLS_OK;
}
bool readable = ready_events[0].events & EPOLLIN;
bool writable = ready_events[0].events & EPOLLOUT;
if (writable) {
send();
}
if (readable) {
recv();
if (ret == -1) {
sls_log(SLS_LOG_INFO, "[%p]CHttpClient::handler, ret=%d, errno=%d, err='%s'",
this, ret, errno, strerror(errno));
return SLS_ERROR;
}

//trigger EPOLLOUT event again.
remove_from_epoll();
add_to_epoll();

return ret;
//write is ready?
if(FD_ISSET(sfd, &write_fds)){
send();
}
//read is ready?
if(FD_ISSET(sfd, &read_fds)){
recv();
}
return SLS_OK;
}



int CHttpClient::parse_url()
{
Expand Down
117 changes: 0 additions & 117 deletions slscore/TCPRole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ CTCPRole::CTCPRole()
m_port = 0;
m_remote_port = 0;
m_valid = false;
m_eid = 0;

strcpy(m_remote_host, "");
sprintf(m_role_name, "tcp_role");
Expand All @@ -56,24 +55,6 @@ int CTCPRole::handler(DATA_PARAM *p)
return ret;
}

int CTCPRole::epoll_write(const char * buf, int size)
{
int ret = 0;
int ev = 0;
int ready_count = 0;
struct epoll_event ready_events[MAX_TCP_SOCK_COUNT];
int timeout = 10;//10ms

ready_count = epoll_wait(m_eid, ready_events, MAX_TCP_SOCK_COUNT, timeout);//等待事件发生
if (ready_count <= 0 || ready_events[0].data.fd != m_fd) {
return 0;
}
bool readable = ev & EPOLLIN;
if (readable)
return write(buf, size);
return 0;
}

int CTCPRole::write(const char * buf, int size)
{
int len = 0;
Expand All @@ -86,28 +67,6 @@ int CTCPRole::write(const char * buf, int size)
return len;
}

int CTCPRole::epoll_read(char * buf, int size)
{
int ret = 0;
int ev = 0;
int ready_count = 0;
struct epoll_event ready_events[MAX_TCP_SOCK_COUNT];
int timeout = 10;//10ms

ready_count = epoll_wait(m_eid, ready_events, MAX_TCP_SOCK_COUNT, timeout);//等待事件发生
if (ready_count <= 0 || ready_events[0].data.fd != m_fd) {
return 0;
}
bool readable = ev & EPOLLOUT;
if (readable) {
ret = read(buf, size);
remove_from_epoll();
//trigger EPOLLOUT event.
add_to_epoll();
}
return ret;
}

int CTCPRole::read(char * buf, int size)
{
int len = 0;
Expand Down Expand Up @@ -292,82 +251,6 @@ int CTCPRole::close() {
return SLS_OK;
}

int CTCPRole::init_epoll()
{
int ret = 0;
m_eid = epoll_create(MAX_TCP_SOCK_COUNT);
if (m_eid <= 0) {
sls_log(SLS_LOG_ERROR, "[%p]CTCPRole::init_epoll, epoll_create failed.", this);
return SLS_ERROR;
}
sls_log(SLS_LOG_INFO, "[%p]CTCPRole::init_epoll, ok, m_eid=%d.", this, m_eid);
return SLS_OK;
}

int CTCPRole::uninit_epoll()
{
int ret = 0;
if (m_eid <= 0) {
sls_log(SLS_LOG_TRACE, "[%p]CTCPRole::uninit_epoll, failed, m_eid=%d.", this, m_eid);
return SLS_OK;
}
sls_log(SLS_LOG_INFO, "[%p]CTCPRole::uninit_epoll, uninit_epoll ok.", this);
::close(m_eid);
m_eid = 0;
return ret;
}

int CTCPRole::add_to_epoll(bool readable, bool writable)
{
if (m_eid <= 0) {
sls_log(SLS_LOG_ERROR, "[%p]CTCPRole::add_to_epoll failure, m_eid=%d.",
this, m_eid);
return SLS_ERROR;
}
if (m_fd <= 0) {
sls_log(SLS_LOG_ERROR, "[%p]CTCPRole::add_to_epoll failure, m_fd=%d.",
this, m_fd);
return SLS_ERROR;
}

struct epoll_event ev;
ev.data.fd = m_fd;
ev.events = EPOLLET | EPOLLERR;
if (readable)
ev.events |= EPOLLIN;
if (writable)
ev.events |= EPOLLOUT;
int ret = epoll_ctl(m_eid, EPOLL_CTL_ADD, m_fd, &ev);
if (ret != 0) {
sls_log(SLS_LOG_ERROR, "[%p]CTCPRole::add_to_epoll, epoll_ctl failed, ret=%d, m_eid=%d, fd=%d, ev.events=0x%x.",
this, ret, m_eid, m_fd, ev.events);
return SLS_ERROR;
}
return SLS_OK;
}

int CTCPRole::remove_from_epoll()
{
if (m_eid <= 0) {
sls_log(SLS_LOG_TRACE, "[%p]CTCPRole::remove_from_epoll failure, m_eid=%d.",
this, m_eid);
return SLS_OK;
}
if (m_fd <= 0) {
sls_log(SLS_LOG_TRACE, "[%p]CTCPRole::remove_from_epoll failure, m_fd=%d.",
this, m_fd);
return SLS_OK;
}

int ret = epoll_ctl(m_eid, EPOLL_CTL_DEL, m_fd, NULL);
if (ret != 0) {
sls_log(SLS_LOG_INFO, "[%p]CTCPRole::remove_from_epoll, epoll_ctl failed, maybe invalid sock, ret=%d, fd=%d.",
this, ret, m_fd);
return SLS_ERROR;
}
return SLS_OK;
}


char * CTCPRole::get_role_name()
{
Expand Down
8 changes: 0 additions & 8 deletions slscore/TCPRole.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <sys/epoll.h>

#include "conf.hpp"

Expand All @@ -66,8 +65,6 @@ public :

int write(const char *buf, int size);
int read(char *buf, int size);
int epoll_write(const char *buf, int size);
int epoll_read(char *buf, int size);

char *get_role_name();
int set_nonblock();
Expand All @@ -81,15 +78,10 @@ public :
char m_remote_host[256];
int m_remote_port;
bool m_valid;
int m_eid;

int setup();
int listen(int port, int backlog);
int connect(char *host, int port);
int init_epoll();
int uninit_epoll();
int add_to_epoll(bool readable=true, bool writable=true);
int remove_from_epoll();

};

Expand Down

0 comments on commit 888bb19

Please sign in to comment.