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