| | |
| | | #include <sys/select.h> |
| | | #include <arpa/inet.h> |
| | | #include <regex> |
| | | #include <pthread.h> |
| | | #include <thread> |
| | | #include <semaphore.h> |
| | | #include <string> |
| | | |
| | |
| | | m_sIp = ip; |
| | | m_nPort = port; |
| | | |
| | | pthread_t platform_pid; |
| | | pthread_attr_t attr; |
| | | pthread_attr_init(&attr); |
| | | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);//detached |
| | | pthread_create(&platform_pid, &attr, TcpConnectThread, this); |
| | | |
| | | std::thread(TcpConnectThread, this).detach(); |
| | | return true; |
| | | } |
| | | |
| | |
| | | return( -1 ); |
| | | } |
| | | |
| | | void *CTcpPort::TcpConnectThread(void *p) |
| | | void CTcpPort::TcpConnectThread(void *p) |
| | | { |
| | | CTcpPort *pCTcpPort = (CTcpPort *)p; |
| | | |
| | |
| | | |
| | | if (pCTcpPort->is_domain_name(pCTcpPort->m_sIp.c_str())) { |
| | | if (pCTcpPort->GetHostIP(pCTcpPort->m_sIp.c_str(), net_addr) != 0) { |
| | | goto CONNECT_END; |
| | | return; |
| | | } |
| | | } else { |
| | | strcpy(net_addr, pCTcpPort->m_sIp.c_str()); |
| | |
| | | pCTcpPort->event_func(0, pCTcpPort, pCTcpPort->event_func_context); |
| | | } |
| | | |
| | | pthread_t platform_pid; |
| | | pthread_attr_t attr; |
| | | pthread_attr_init(&attr); |
| | | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);//detached |
| | | pthread_create(&platform_pid, &attr, TcpListenThread, p); |
| | | std::thread(TcpListenThread, p).detach(); |
| | | } else { |
| | | if (pCTcpPort->event_func != NULL) { |
| | | pCTcpPort->event_func(-1, pCTcpPort, pCTcpPort->event_func_context); |
| | | } |
| | | } |
| | | |
| | | CONNECT_END: |
| | | pthread_exit(NULL); |
| | | } |
| | | |
| | | void *CTcpPort::TcpListenThread(void *p) |
| | | void CTcpPort::TcpListenThread(void *p) |
| | | { |
| | | int fds_ret; |
| | | struct timeval tv; |
| | |
| | | if (pCTcpPort->event_func) { |
| | | pCTcpPort->event_func(-1, pCTcpPort, pCTcpPort->event_func_context); |
| | | } |
| | | |
| | | pthread_exit(NULL); |
| | | } |
| | | |
| | | void CTcpPort::set_event_callback(void (*callback)(int, void *, void *), void *context) |