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

117 lines
2.9 KiB
C++
Raw Normal View History

2016-03-19 06:57:51 +13:00
#include "tserverproxy.h"
#include <iostream.h>
#include <Winsock.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <memory.h>
#include <time.h>
#include <assert.h>
using namespace std;
//=============================================================================
enum {
2016-06-15 18:43:10 +12:00
OK,
WSASTARTUP_FAILED,
HOST_UNKNOWN,
SOCKET_CREATION_FAILED,
CONNECTION_FAILED,
SEND_FAILED,
RECEIVE_FAILED
2016-03-19 06:57:51 +13:00
};
//=============================================================================
2016-06-15 18:43:10 +12:00
int askServer(string hostName, int portId, string question, string &answer) {
struct hostent *he = gethostbyname(hostName.c_str());
if (!he) return HOST_UNKNOWN;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
int socket_id = socket(AF_INET, SOCK_STREAM, 0);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
struct sockaddr_in addr;
memset((char *)&addr, 0, sizeof addr);
addr.sin_family = he->h_addrtype;
addr.sin_port = htons(portId);
memcpy((char *)&(addr.sin_addr), he->h_addr, he->h_length);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
int rcConnect = connect(socket_id, (struct sockaddr *)&(addr), sizeof addr);
if (rcConnect == SOCKET_ERROR) return CONNECTION_FAILED;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
string message = question;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
int rcSend = send(socket_id, message.c_str(), message.size(), 0);
if (rcSend == SOCKET_ERROR) return SEND_FAILED;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
char retBuffer[1024];
memset(&retBuffer, 0, 1024);
int rcRecv = recv(socket_id, retBuffer, sizeof(retBuffer) - 1, 0);
if (rcRecv == SOCKET_ERROR) return RECEIVE_FAILED;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
retBuffer[rcRecv] = '\0';
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
closesocket(socket_id);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
answer = retBuffer;
return OK;
2016-03-19 06:57:51 +13:00
}
//=============================================================================
TServerProxy::TServerProxy(const string hostName, int portId)
2016-06-15 18:43:10 +12:00
: m_hostName(hostName), m_portId(portId) {
2016-03-19 06:57:51 +13:00
#ifdef WIN32
2016-06-15 18:43:10 +12:00
WSADATA wsaData;
WORD wVersionRequested = MAKEWORD(1, 1);
int irc = WSAStartup(wVersionRequested, &wsaData);
2016-03-19 06:57:51 +13:00
#endif
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
TServerProxy::~TServerProxy() {
2016-03-19 06:57:51 +13:00
#ifdef WIN32
2016-06-15 18:43:10 +12:00
WSACleanup();
2016-03-19 06:57:51 +13:00
#endif
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
bool TServerProxy::testConnection() const {
struct hostent *he = gethostbyname(m_hostName.c_str());
if (!he) return false;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
int socket_id = socket(AF_INET, SOCK_STREAM, 0);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
struct sockaddr_in addr;
memset((char *)&addr, 0, sizeof addr);
addr.sin_family = he->h_addrtype;
addr.sin_port = htons(m_portId);
memcpy((char *)&(addr.sin_addr), he->h_addr, he->h_length);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
bool ret = (SOCKET_ERROR !=
connect(socket_id, (struct sockaddr *)&(addr), sizeof addr));
closesocket(socket_id);
return ret;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
int TServerProxy::exec(int argc, char *argv[], string &reply) {
string cmd;
assert(argc > 0);
if (argc > 0) {
cmd = argv[0];
for (int i = 1; i < argc; ++i) {
cmd += ",";
cmd += argv[i];
}
}
return askServer(m_hostName, m_portId, cmd, reply);
2016-03-19 06:57:51 +13:00
}