0
0
mirror of https://github.com/yse/easy_profiler.git synced 2024-12-30 02:16:55 +08:00

346 lines
8.5 KiB
C++
Raw Normal View History

2016-09-18 19:11:22 +03:00
/**
Lightweight profiler library for c++
2017-03-30 06:55:15 +03:00
Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin
2016-09-18 19:11:22 +03:00
2017-03-30 06:55:15 +03:00
Licensed under either of
* MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT)
* Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0)
at your option.
2016-11-13 16:39:59 +03:00
2017-03-30 06:55:15 +03:00
The MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.
The Apache License, Version 2.0 (the "License");
You may not use this file except in compliance with the License.
2016-11-13 16:39:59 +03:00
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
2016-09-18 19:11:22 +03:00
**/
#include <easy/easy_socket.h>
2016-09-18 19:11:22 +03:00
2016-09-19 23:59:55 +03:00
#include <string.h>
2016-09-27 23:00:49 +03:00
#include <thread>
2016-09-18 19:11:22 +03:00
2016-09-19 23:59:55 +03:00
#ifdef _WIN32
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")
#else
2016-09-18 19:11:22 +03:00
#include <errno.h>
2016-09-19 23:59:55 +03:00
#include <sys/ioctl.h>
#endif
bool EasySocket::checkSocket(socket_t s) const
{
return s > 0;
}
int EasySocket::_close(EasySocket::socket_t s)
{
#ifdef _WIN32
return ::closesocket(s);
#else
//TODO
//return close(s);
2016-11-13 15:20:25 +03:00
return 0;
2016-09-19 23:59:55 +03:00
#endif
}
void EasySocket::setBlocking(EasySocket::socket_t s, bool blocking)
{
#ifdef _WIN32
u_long iMode = blocking ? 0 : 1;//0 - blocking, 1 - non blocking
ioctlsocket(s, FIONBIO, &iMode);
#else
const int iMode = blocking ? 0 : 1;//0 - blocking, 1 - non blocking
ioctl(s, FIONBIO, (char *)&iMode);
#endif
}
2016-09-18 19:11:22 +03:00
int EasySocket::bind(uint16_t portno)
{
2016-09-19 23:59:55 +03:00
if (!checkSocket(m_socket)) return -1;
2016-09-18 19:11:22 +03:00
struct sockaddr_in serv_addr;
2016-09-19 23:59:55 +03:00
memset(&serv_addr, 0, sizeof(serv_addr));
2016-09-18 19:11:22 +03:00
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
2016-09-19 23:59:55 +03:00
auto res = ::bind(m_socket, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
return res;
2016-09-18 19:11:22 +03:00
}
void EasySocket::flush()
{
if (m_socket){
2016-09-19 23:59:55 +03:00
_close(m_socket);
2016-09-18 19:11:22 +03:00
}
if (m_replySocket != m_socket){
2016-09-19 23:59:55 +03:00
_close(m_replySocket);
2016-09-18 19:11:22 +03:00
}
2016-09-19 23:59:55 +03:00
#ifdef _WIN32
2016-09-18 19:11:22 +03:00
m_socket = 0;
m_replySocket = 0;
2016-09-27 23:00:49 +03:00
#else
wsaret = 0;
2016-09-19 23:59:55 +03:00
#endif
2016-09-18 19:11:22 +03:00
}
2016-09-19 23:59:55 +03:00
void EasySocket::checkResult(int result)
{
2016-11-18 22:58:44 +03:00
//printf("Errno: %s\n", strerror(errno));
2016-09-19 23:59:55 +03:00
if(result >= 0){
m_state = CONNECTION_STATE_SUCCESS;
return;
}else if(result == -1){
int error_code = 0;
#ifdef _WIN32
error_code = WSAGetLastError();
const int CONNECTION_ABORTED = WSAECONNABORTED;
const int CONNECTION_RESET = WSAECONNRESET;
2016-09-27 23:00:49 +03:00
const int CONNECTION_IN_PROGRESS = WSAEINPROGRESS;
2016-09-19 23:59:55 +03:00
#else
error_code = errno;
const int CONNECTION_ABORTED = ECONNABORTED;
const int CONNECTION_RESET = ECONNRESET;
2016-09-27 23:00:49 +03:00
const int CONNECTION_IN_PROGRESS = EINPROGRESS;
2016-11-18 22:58:44 +03:00
const int CONNECTION_BROKEN_PIPE = EPIPE;
const int CONNECTION_ENOENT = ENOENT;
2016-09-19 23:59:55 +03:00
#endif
switch(error_code)
{
case CONNECTION_ABORTED:
case CONNECTION_RESET:
#ifndef _WIN32
case CONNECTION_BROKEN_PIPE:
case CONNECTION_ENOENT:
#endif
2016-09-19 23:59:55 +03:00
m_state = CONNECTION_STATE_DISCONNECTED;
break;
2016-09-27 23:00:49 +03:00
case CONNECTION_IN_PROGRESS:
m_state = CONNECTION_STATE_IN_PROGRESS;
break;
2016-09-19 23:59:55 +03:00
default:
break;
}
}
}
2016-09-18 19:11:22 +03:00
void EasySocket::init()
{
2016-09-19 23:59:55 +03:00
if (wsaret == 0)
{
int protocol = 0;
#ifdef _WIN32
protocol = IPPROTO_TCP;
#endif
m_socket = socket(AF_INET, SOCK_STREAM, protocol);
if (!checkSocket(m_socket)) {
return;
}
}else
return;
setBlocking(m_socket,true);
2016-09-27 23:00:49 +03:00
#ifndef _WIN32
wsaret = 1;
#endif
2016-09-19 23:59:55 +03:00
int opt = 1;
setsockopt(m_socket, SOL_SOCKET, SO_REUSEADDR, (const char *)&opt, sizeof(opt));
2016-09-18 19:11:22 +03:00
}
EasySocket::EasySocket()
{
2016-09-19 23:59:55 +03:00
#ifdef _WIN32
WSADATA wsaData;
wsaret = WSAStartup(0x101, &wsaData);
#else
wsaret = 0;
#endif
2016-09-18 19:11:22 +03:00
init();
2016-09-19 23:59:55 +03:00
#ifndef _WIN32
wsaret = 1;
#endif
2016-09-18 19:11:22 +03:00
}
EasySocket::~EasySocket()
{
flush();
2016-09-19 23:59:55 +03:00
#ifdef _WIN32
if (wsaret == 0)
WSACleanup();
#endif
2016-09-18 19:11:22 +03:00
}
int EasySocket::send(const void *buf, size_t nbyte)
{
2016-09-19 23:59:55 +03:00
if(!checkSocket(m_replySocket)) return -1;
int res = 0;
#ifdef _WIN32
2016-09-20 23:25:13 +03:00
res = ::send(m_replySocket, (const char*)buf, (int)nbyte, 0);
2016-09-19 23:59:55 +03:00
#else
2016-11-18 22:58:44 +03:00
res = ::send(m_replySocket,buf,nbyte,MSG_NOSIGNAL);
2016-09-19 23:59:55 +03:00
#endif
2016-09-18 19:11:22 +03:00
checkResult(res);
return res;
}
int EasySocket::receive(void *buf, size_t nbyte)
{
2016-09-19 23:59:55 +03:00
if(!checkSocket(m_replySocket)) return -1;
int res = 0;
#ifdef _WIN32
2016-09-20 23:25:13 +03:00
res = ::recv(m_replySocket, (char*)buf, (int)nbyte, 0);
2016-09-19 23:59:55 +03:00
#else
res = ::read(m_replySocket,buf,nbyte);
#endif
2016-09-18 19:11:22 +03:00
checkResult(res);
if (res == 0){
m_state = CONNECTION_STATE_DISCONNECTED;
}
return res;
}
int EasySocket::listen(int count)
{
2016-09-19 23:59:55 +03:00
if(!checkSocket(m_socket)) return -1;
2016-09-18 19:11:22 +03:00
int res = ::listen(m_socket,count);
checkResult(res);
return res;
}
int EasySocket::accept()
{
2016-09-19 23:59:55 +03:00
if(!checkSocket(m_socket)) return -1;
fd_set fdread, fdwrite, fdexcl;
timeval tv = { 0 };
FD_ZERO (&fdread);
FD_SET (m_socket, &fdread);
fdwrite = fdread;
fdexcl = fdread;
tv.tv_sec = 0; tv.tv_usec = 500;
int rc =select (m_socket+1, &fdread, &fdwrite, &fdexcl, &tv);
if(rc <= 0){
//there is no connection for accept
return -1;
}
2016-09-18 19:11:22 +03:00
m_replySocket = ::accept(m_socket,nullptr,nullptr);
2016-09-20 23:25:13 +03:00
checkResult((int)m_replySocket);
2016-09-19 23:59:55 +03:00
if(checkSocket(m_replySocket))
{
int send_buffer = 64*1024*1024;
int send_buffer_sizeof = sizeof(int);
setsockopt(m_replySocket, SOL_SOCKET, SO_SNDBUF, (char*)&send_buffer, send_buffer_sizeof);
2016-09-20 23:25:13 +03:00
2016-09-19 23:59:55 +03:00
//int flag = 1;
//int result = setsockopt(m_replySocket,IPPROTO_TCP,TCP_NODELAY,(char *)&flag,sizeof(int));
2016-09-18 19:11:22 +03:00
2016-09-19 23:59:55 +03:00
//setBlocking(m_replySocket,true);
}
2016-09-20 23:25:13 +03:00
return (int)m_replySocket;
2016-09-18 19:11:22 +03:00
}
2016-09-19 23:59:55 +03:00
bool EasySocket::setAddress(const char *serv, uint16_t portno)
2016-09-18 19:11:22 +03:00
{
2016-09-19 23:59:55 +03:00
server = gethostbyname(serv);
if (server == NULL) {
return false;
//fprintf(stderr,"ERROR, no such host\n");
2016-09-18 19:11:22 +03:00
}
2016-09-20 00:54:56 +03:00
memset((char *)&serv_addr, 0, sizeof(serv_addr));
2016-09-19 23:59:55 +03:00
serv_addr.sin_family = AF_INET;
2016-09-20 00:54:56 +03:00
memcpy((char *)&serv_addr.sin_addr.s_addr, (char *)server->h_addr, server->h_length);
2016-09-18 19:11:22 +03:00
2016-09-20 00:54:56 +03:00
serv_addr.sin_port = htons(portno);
2016-09-18 19:11:22 +03:00
return true;
}
int EasySocket::connect()
{
2016-09-20 00:54:56 +03:00
if (server == NULL || m_socket <=0 ) {
2016-09-18 19:11:22 +03:00
return -1;
2016-09-20 00:54:56 +03:00
//fprintf(stderr,"ERROR, no such host\n");
2016-09-18 19:11:22 +03:00
}
2016-09-27 23:14:00 +03:00
int res = 0;
//TODO: more intelligence
#ifndef _WIN32
2016-09-27 23:00:49 +03:00
setBlocking(m_socket,false);
int counter = 0;
int sleepMs = 20;
int waitSec = 1;
int waitMs = waitSec*1000/sleepMs;
2016-09-27 23:14:00 +03:00
2016-09-27 23:00:49 +03:00
while(counter++ < waitMs)
{
res = ::connect(m_socket,(struct sockaddr *) &serv_addr,sizeof(serv_addr));
checkResult(res);
if (res == 0)
break;
if (m_state == CONNECTION_STATE_IN_PROGRESS)
{
std::this_thread::sleep_for(std::chrono::milliseconds(sleepMs));
continue;
}
if(m_state != CONNECTION_STATE_IN_PROGRESS && m_state != CONNECTION_STATE_SUCCESS )
break;
}
setBlocking(m_socket,true);
2016-09-27 23:14:00 +03:00
#else
res = ::connect(m_socket, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
checkResult(res);
#endif
2016-09-20 00:54:56 +03:00
if(res == 0){
2016-09-18 19:11:22 +03:00
2016-09-20 00:54:56 +03:00
struct timeval tv;
2016-09-18 19:11:22 +03:00
2016-09-20 00:54:56 +03:00
tv.tv_sec = 1;
tv.tv_usec = 0;
2016-09-18 19:11:22 +03:00
2016-09-20 00:54:56 +03:00
setsockopt(m_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,sizeof(struct timeval));
2016-09-18 19:11:22 +03:00
2016-09-20 00:54:56 +03:00
m_replySocket = m_socket;
}
return res;
2016-09-18 19:11:22 +03:00
}