mirror of
https://github.com/yse/easy_profiler.git
synced 2024-12-27 08:41:02 +08:00
Add server function for EasySock
This commit is contained in:
parent
7b502c5a5d
commit
0fe6690545
@ -28,7 +28,7 @@ include_directories(
|
||||
|
||||
if(UNIX)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-long-long -Wno-reorder -pedantic -g -ggdb" )
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-long-long -Wno-reorder -pedantic -O3" )
|
||||
else()
|
||||
add_definitions(
|
||||
-D_CRT_SECURE_NO_WARNINGS
|
||||
|
@ -233,6 +233,8 @@ class ThreadStorage;
|
||||
|
||||
namespace profiler {
|
||||
|
||||
const uint32_t DEFAULT_PORT = 28077;
|
||||
|
||||
class Block;
|
||||
|
||||
typedef uint64_t timestamp_t;
|
||||
|
@ -95,12 +95,12 @@ EasyMainWindow::EasyMainWindow() : Parent(), m_treeWidget(nullptr), m_graphicsVi
|
||||
fileToolBar->addAction(newAct);
|
||||
connect(newAct, &QAction::triggered, this, &This::onCaptureClicked);
|
||||
|
||||
m_server = new QTcpServer( this );
|
||||
m_server = new QTcpSocket( this );
|
||||
|
||||
//m_server->connectToHost("127.0.0.1",28077);
|
||||
//connect( m_server, SIGNAL(readyRead()), SLOT(readTcpData()) );
|
||||
m_server->connectToHost("127.0.0.1",profiler::DEFAULT_PORT);
|
||||
connect( m_server, SIGNAL(readyRead()), SLOT(readTcpData()) );
|
||||
|
||||
if (!m_server->listen(QHostAddress(QHostAddress::Any), 28077)) {
|
||||
/*if (!m_server->listen(QHostAddress(QHostAddress::Any), 28077)) {
|
||||
QMessageBox::critical(0,
|
||||
"Server Error",
|
||||
"Unable to start the server:"
|
||||
@ -110,9 +110,9 @@ EasyMainWindow::EasyMainWindow() : Parent(), m_treeWidget(nullptr), m_graphicsVi
|
||||
}
|
||||
|
||||
connect(m_server, SIGNAL(newConnection()),
|
||||
this, SLOT(onNewConnection())
|
||||
this, SLOT(onNewConnection())
|
||||
);
|
||||
|
||||
*/
|
||||
loadSettings();
|
||||
|
||||
|
||||
@ -547,6 +547,7 @@ void EasyMainWindow::onCaptureClicked(bool)
|
||||
void EasyMainWindow::readTcpData()
|
||||
{
|
||||
QTcpSocket* pClientSocket = (QTcpSocket*)sender();
|
||||
m_client = pClientSocket;
|
||||
static int necessarySize = 0;
|
||||
static int loadedSize = 0;
|
||||
while(pClientSocket->bytesAvailable())
|
||||
@ -563,20 +564,11 @@ void EasyMainWindow::readTcpData()
|
||||
loadedSize += data.size();
|
||||
if (loadedSize == necessarySize)
|
||||
{
|
||||
qInfo() << "Write FILE";
|
||||
std::string tempfilename = "test_rec.prof";
|
||||
std::ofstream of(tempfilename, std::fstream::binary);
|
||||
of << m_receivedProfileData.str();
|
||||
of.close();
|
||||
|
||||
m_receivedProfileData.str(std::string());
|
||||
m_receivedProfileData.clear();
|
||||
loadFile(QString(tempfilename.c_str()));
|
||||
m_recFrames = false;
|
||||
}
|
||||
//qInfo() << necessarySize << " " << loadedSize;
|
||||
if (m_recFrames)
|
||||
continue;
|
||||
//if (m_recFrames)
|
||||
// continue;
|
||||
}
|
||||
|
||||
|
||||
@ -597,10 +589,19 @@ void EasyMainWindow::readTcpData()
|
||||
case profiler::net::MESSAGE_TYPE_REPLY_END_SEND_BLOCKS:
|
||||
{
|
||||
qInfo() << "Receive MESSAGE_TYPE_REPLY_END_SEND_BLOCKS";
|
||||
//m_recFrames = false;
|
||||
m_recFrames = false;
|
||||
|
||||
|
||||
qInfo() << "Write FILE";
|
||||
std::string tempfilename = "test_rec.prof";
|
||||
std::ofstream of(tempfilename, std::fstream::binary);
|
||||
of << m_receivedProfileData.str();
|
||||
of.close();
|
||||
|
||||
m_receivedProfileData.str(std::string());
|
||||
m_receivedProfileData.clear();
|
||||
loadFile(QString(tempfilename.c_str()));
|
||||
m_recFrames = false;
|
||||
|
||||
}
|
||||
break;
|
||||
@ -628,7 +629,7 @@ void EasyMainWindow::readTcpData()
|
||||
void EasyMainWindow::onNewConnection()
|
||||
{
|
||||
|
||||
m_client = m_server->nextPendingConnection();
|
||||
//m_client = m_server->nextPendingConnection();
|
||||
|
||||
qInfo() << "New connection!" << m_client;
|
||||
|
||||
|
@ -97,7 +97,7 @@ protected:
|
||||
::profiler::SerializedData m_serializedDescriptors;
|
||||
EasyFileReader m_reader;
|
||||
|
||||
QTcpServer* m_server = nullptr;
|
||||
QTcpSocket* m_server = nullptr;
|
||||
QTcpSocket* m_client = nullptr;
|
||||
std::stringstream m_receivedProfileData;
|
||||
bool m_recFrames = false;
|
||||
|
@ -22,6 +22,17 @@ along with this program.If not, see <http://www.gnu.org/licenses/>.
|
||||
#ifndef _WIN32
|
||||
#include <strings.h>
|
||||
|
||||
int EasySocket::bind(uint16_t portno)
|
||||
{
|
||||
if(m_socket < 0 ) return -1;
|
||||
struct sockaddr_in serv_addr;
|
||||
bzero((char *) &serv_addr, sizeof(serv_addr));
|
||||
serv_addr.sin_family = AF_INET;
|
||||
serv_addr.sin_addr.s_addr = INADDR_ANY;
|
||||
serv_addr.sin_port = htons(portno);
|
||||
return ::bind(m_socket, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
|
||||
}
|
||||
|
||||
EasySocket::EasySocket()
|
||||
{
|
||||
m_socket = socket(AF_INET, SOCK_STREAM, 0);
|
||||
@ -30,27 +41,36 @@ EasySocket::EasySocket()
|
||||
tv.tv_sec = 1;
|
||||
tv.tv_usec = 0;
|
||||
|
||||
setsockopt(m_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,sizeof(struct timeval));
|
||||
//setsockopt(m_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,sizeof(struct timeval));
|
||||
}
|
||||
|
||||
EasySocket::~EasySocket()
|
||||
{
|
||||
}
|
||||
|
||||
int EasySocket::write(const void *buf, size_t nbyte)
|
||||
int EasySocket::send(const void *buf, size_t nbyte)
|
||||
{
|
||||
if(m_socket <= 0){
|
||||
return -1;
|
||||
}
|
||||
return ::write(m_socket,buf,nbyte);
|
||||
if(m_replySocket <= 0) return -1;
|
||||
return ::write(m_replySocket,buf,nbyte);
|
||||
}
|
||||
|
||||
int EasySocket::read(void *buf, size_t nbyte)
|
||||
int EasySocket::receive(void *buf, size_t nbyte)
|
||||
{
|
||||
if(m_socket <= 0){
|
||||
return -1;
|
||||
}
|
||||
return ::read(m_socket,buf,nbyte);
|
||||
if(m_replySocket <= 0) return -1;
|
||||
return ::read(m_replySocket,buf,nbyte);
|
||||
}
|
||||
|
||||
int EasySocket::listen(int count)
|
||||
{
|
||||
if(m_socket < 0 ) return -1;
|
||||
return ::listen(m_socket,count);
|
||||
}
|
||||
|
||||
int EasySocket::accept()
|
||||
{
|
||||
if(m_socket < 0 ) return -1;
|
||||
m_replySocket = ::accept(m_socket,nullptr,nullptr);
|
||||
return m_replySocket;
|
||||
}
|
||||
|
||||
bool EasySocket::setAddress(const char *serv, uint16_t portno)
|
||||
@ -111,7 +131,7 @@ EasySocket::~EasySocket()
|
||||
WSACleanup();
|
||||
}
|
||||
|
||||
int EasySocket::write(const void *buf, size_t nbyte)
|
||||
int EasySocket::send(const void *buf, size_t nbyte)
|
||||
{
|
||||
if (m_socket <= 0){
|
||||
return -1;
|
||||
@ -121,7 +141,7 @@ int EasySocket::write(const void *buf, size_t nbyte)
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int EasySocket::read(void *buf, size_t nbyte)
|
||||
int EasySocket::receive(void *buf, size_t nbyte)
|
||||
{
|
||||
if (m_socket <= 0){
|
||||
return -1;
|
||||
|
@ -41,6 +41,7 @@ class EasySocket
|
||||
{
|
||||
#ifndef _WIN32
|
||||
int m_socket = 0;
|
||||
int m_replySocket = 0;
|
||||
uint16_t m_port = 0;
|
||||
struct sockaddr_in serv_addr;
|
||||
struct hostent *server = nullptr;
|
||||
@ -50,12 +51,17 @@ class EasySocket
|
||||
struct addrinfo hints;
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
public:
|
||||
EasySocket();
|
||||
~EasySocket();
|
||||
|
||||
int write(const void *buf, size_t nbyte);
|
||||
int read(void *buf, size_t nbyte);
|
||||
int send(const void *buf, size_t nbyte);
|
||||
int receive(void *buf, size_t nbyte);
|
||||
int listen(int count=5);
|
||||
int accept();
|
||||
int bind(uint16_t portno);
|
||||
|
||||
bool setAddress(const char* serv, uint16_t port);
|
||||
int connect();
|
||||
|
@ -453,8 +453,17 @@ void ProfileManager::startListen()
|
||||
|
||||
EasySocket socket;
|
||||
|
||||
socket.setAddress("192.224.4.115",28077);
|
||||
while(!m_stopListen.load() && (socket.connect() < 0 ) ) {}
|
||||
socket.bind(profiler::DEFAULT_PORT);
|
||||
|
||||
socket.listen();
|
||||
|
||||
socket.accept();
|
||||
|
||||
int foo = 0;
|
||||
socket.send(&foo,sizeof(foo));
|
||||
|
||||
//socket.setAddress("192.224.4.115",28077);
|
||||
//while(!m_stopListen.load() && (socket.connect() < 0 ) ) {}
|
||||
|
||||
profiler::net::Message replyMessage(profiler::net::MESSAGE_TYPE_REPLY_START_CAPTURING);
|
||||
|
||||
@ -462,16 +471,10 @@ void ProfileManager::startListen()
|
||||
//bzero(buffer,256);
|
||||
while(!m_stopListen.load())
|
||||
{
|
||||
auto bytes = socket.read(buffer,255);
|
||||
auto bytes = socket.receive(buffer,255);
|
||||
|
||||
char *buf = &buffer[0];
|
||||
|
||||
if (bytes == 0)
|
||||
{
|
||||
while (!m_stopListen.load() && (socket.connect() < 0)) {}
|
||||
continue;
|
||||
}
|
||||
|
||||
if(bytes > 0)
|
||||
{
|
||||
profiler::net::Message* message = (profiler::net::Message*)buf;
|
||||
@ -486,7 +489,7 @@ void ProfileManager::startListen()
|
||||
profiler::setEnabled(true);
|
||||
|
||||
replyMessage.type = profiler::net::MESSAGE_TYPE_REPLY_START_CAPTURING;
|
||||
socket.write(&replyMessage,sizeof(replyMessage));
|
||||
socket.send(&replyMessage,sizeof(replyMessage));
|
||||
}
|
||||
break;
|
||||
case profiler::net::MESSAGE_TYPE_REQUEST_STOP_CAPTURE:
|
||||
@ -495,7 +498,7 @@ void ProfileManager::startListen()
|
||||
profiler::setEnabled(false);
|
||||
|
||||
replyMessage.type = profiler::net::MESSAGE_TYPE_REPLY_PREPARE_BLOCKS;
|
||||
auto send_bytes = socket.write(&replyMessage,sizeof(replyMessage));
|
||||
auto send_bytes = socket.send(&replyMessage,sizeof(replyMessage));
|
||||
|
||||
|
||||
profiler::net::DataMessage dm;
|
||||
@ -511,7 +514,7 @@ void ProfileManager::startListen()
|
||||
memcpy(sendbuf,&dm,sizeof(dm));
|
||||
memcpy(sendbuf + sizeof(dm),os.stream().str().c_str(),dm.size);
|
||||
|
||||
send_bytes = socket.write(sendbuf,packet_size);
|
||||
send_bytes = socket.send(sendbuf,packet_size);
|
||||
|
||||
|
||||
/*std::string tempfilename = "test_snd.prof";
|
||||
@ -522,7 +525,7 @@ void ProfileManager::startListen()
|
||||
delete [] sendbuf;
|
||||
//std::this_thread::sleep_for(std::chrono::seconds(2));
|
||||
replyMessage.type = profiler::net::MESSAGE_TYPE_REPLY_END_SEND_BLOCKS;
|
||||
//send_bytes = socket.write(&replyMessage,sizeof(replyMessage));
|
||||
send_bytes = socket.send(&replyMessage,sizeof(replyMessage));
|
||||
}
|
||||
break;
|
||||
default:
|
||||
|
Loading…
x
Reference in New Issue
Block a user