tahoma2d/toonz/sources/toonzfarm/tfarm/tbaseserver.cpp

198 lines
4.3 KiB
C++
Raw Normal View History

2016-03-19 06:57:51 +13:00
#include "tbaseserver.h"
#ifndef WIN32
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#endif
//#include "tcommon.h"
#include <string>
#include <vector>
using namespace std;
//-----------------------------------------------------------------------------
TBaseServer::TBaseServer(int port)
2016-06-15 18:43:10 +12:00
: m_port(port), m_socketId(-1), m_stopped(false) {}
2016-03-19 06:57:51 +13:00
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
TBaseServer::~TBaseServer() {
if (m_socketId != -1) stop();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void TBaseServer::start() {
2016-03-19 06:57:51 +13:00
#ifdef WIN32
2016-06-15 18:43:10 +12:00
// Windows Socket startup
WSADATA wsaData;
WORD wVersionRequested = MAKEWORD(1, 1);
int irc = WSAStartup(wVersionRequested, &wsaData);
if (irc != 0) throw("Windows Socket Startup failed");
2016-03-19 06:57:51 +13:00
#else
2016-06-15 18:43:10 +12:00
int c;
FILE *fp;
register int ns;
2016-03-19 06:57:51 +13:00
#endif
2016-06-15 18:43:10 +12:00
if ((m_socketId = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
2016-03-19 06:57:51 +13:00
#ifdef WIN32
2016-06-15 18:43:10 +12:00
char szError[1024];
wsprintf(szError, TEXT("Allocating socket failed. Error: %d"),
WSAGetLastError());
throw(szError);
2016-03-19 06:57:51 +13:00
#else
2016-06-15 18:43:10 +12:00
throw("Allocating socket failed.");
2016-03-19 06:57:51 +13:00
#endif
2016-06-15 18:43:10 +12:00
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
struct sockaddr_in myname;
myname.sin_family = AF_INET;
myname.sin_port = htons(m_port);
myname.sin_addr.s_addr = INADDR_ANY;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (bind(m_socketId, (sockaddr *)&myname, sizeof myname) < 0) {
throw("Unable to bind to connection");
}
2016-03-19 06:57:51 +13:00
#ifdef TRACE
2016-06-15 18:43:10 +12:00
cout << "Listening ..." << endl;
2016-03-19 06:57:51 +13:00
#endif
2016-06-15 18:43:10 +12:00
// length of the queue of pending connections
int maxPendingConnections = 5;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (listen(m_socketId, maxPendingConnections) < 0) {
throw("Unable to start listening");
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
while (true) {
register int clientSocket;
if ((clientSocket = accept(m_socketId, 0, 0)) < 0) {
if (m_stopped)
return;
else
throw("Error accepting from client");
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
printf("ho accettato\n");
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
char msgFromClient[1024];
memset(msgFromClient, 0, sizeof(msgFromClient));
2016-03-19 06:57:51 +13:00
#ifdef WIN32
2016-06-15 18:43:10 +12:00
// Receive data from the client.
int iReturn = recv(clientSocket, msgFromClient, sizeof(msgFromClient), 0);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Verify that data was received. If yes, use it.
if (iReturn == SOCKET_ERROR) {
char szError[1024];
wsprintf(szError,
TEXT("No data is received, receive failed.") TEXT(" Error: %d"),
WSAGetLastError());
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
throw(szError);
}
2016-03-19 06:57:51 +13:00
#else
2016-06-15 18:43:10 +12:00
fp = fdopen(clientSocket, "r");
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
printf("Partito!\n");
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
int i = 0;
while ((int)(c = fgetc(fp)) != EOF) {
putchar(c);
msgFromClient[i] = c;
}
2016-03-19 06:57:51 +13:00
#endif
2016-06-15 18:43:10 +12:00
printf("msg finito\n");
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
char *argv[128];
int argc = extractArgs(msgFromClient, argv);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
string reply = exec(argc, argv);
reply += "\n";
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
for (int i = 0; i < argc; ++i) delete[] argv[i];
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Send the reply string from the server to the client.
if (send(clientSocket, reply.c_str(), reply.length() + 1, 0) < 0) {
char szError[1024];
2016-03-19 06:57:51 +13:00
#ifdef WIN32
2016-06-15 18:43:10 +12:00
wsprintf(szError, TEXT("Sending data to the client failed. Error: %d"),
WSAGetLastError());
2016-03-19 06:57:51 +13:00
#endif
2016-06-15 18:43:10 +12:00
throw(szError);
}
2016-03-19 06:57:51 +13:00
#ifndef WIN32
2016-06-15 18:43:10 +12:00
close(clientSocket);
2016-03-19 06:57:51 +13:00
#endif
2016-06-15 18:43:10 +12:00
}
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void TBaseServer::stop() {
// TThread::ScopedLock sl(m_mutex);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (!m_stopped) {
m_stopped = true;
2016-03-19 06:57:51 +13:00
#ifdef WIN32
2016-06-15 18:43:10 +12:00
if (closesocket(m_socketId) != 0) {
char szError[1024];
wsprintf(szError, TEXT(" Error: %d"), WSAGetLastError());
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
throw(szError);
}
2016-03-19 06:57:51 +13:00
#else
2016-06-15 18:43:10 +12:00
close(m_socketId);
2016-03-19 06:57:51 +13:00
#endif
2016-06-15 18:43:10 +12:00
m_socketId = -1;
}
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
int TBaseServer::extractArgs(char *s, char *argv[]) {
string ss(s);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
string::size_type pos1 = 0;
string::size_type pos2 = ss.find(",");
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
vector<string> args;
while (pos2 != string::npos) {
string arg = ss.substr(pos1, pos2 - pos1);
args.push_back(arg);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
pos1 = pos2 + 1;
pos2 = ss.find(",", pos1);
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
string arg = ss.substr(pos1, ss.length() - pos1);
args.push_back(arg);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
vector<string>::iterator it = args.begin();
for (int i = 0; it != args.end(); ++it, ++i) {
string arg = *it;
argv[i] = new char[arg.size() + 1];
strcpy(argv[i], arg.c_str());
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
return args.size();
2016-03-19 06:57:51 +13:00
}