1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
|
// Cyphesis Online RPG Server and AI Engine
// Copyright (C) 2001 Alistair Riddoch
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// $Id: ClientConnection.cpp,v 1.45 2008-01-13 01:32:54 alriddoch Exp $
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif // HAVE_CONFIG_H
#include "ClientConnection.h"
#include "common/log.h"
#include "common/debug.h"
#include "common/globals.h"
#include "common/compose.hpp"
#include <Atlas/Codecs/XML.h>
#include <Atlas/Net/Stream.h>
#include <Atlas/Objects/Encoder.h>
#include <Atlas/Objects/Operation.h>
#include <Atlas/Objects/Anonymous.h>
#ifdef HAVE_SYS_UN_H
#include <sys/un.h>
#endif // HAVE_SYS_UN_H
using Atlas::Objects::Root;
using Atlas::Objects::Entity::Anonymous;
using Atlas::Objects::Operation::RootOperation;
static bool debug_flag = false;
ClientConnection::ClientConnection() :
client_fd(-1), encoder(NULL), serialNo(512)
{
}
ClientConnection::~ClientConnection()
{
if (encoder != NULL) {
delete encoder;
}
}
void ClientConnection::operation(const RootOperation & op)
{
#if 0
const std::string & from = op->getFrom();
if (from.empty()) {
std::cerr << "ERROR: Operation with no destination" << std::endl << std::flush;
return;
}
dict_t::const_iterator I = objects.find(from);
if (I == objects.end()) {
std::cerr << "ERROR: Operation with invalid destination" << std::endl << std::flush;
return;
}
OpVector res = I->second->message(op);
OpVector::const_iterator Jend = res.end();
fora (OpVector::const_iterator J = res.begin(); J != Jend; ++J) {
(*J)->setFrom(I->first);
send(*(*J));
}
#endif
}
void ClientConnection::objectArrived(const Atlas::Objects::Root & obj)
{
RootOperation op = Atlas::Objects::smart_dynamic_cast<RootOperation>(obj);
if (!op.isValid()) {
const std::list<std::string> & parents = obj->getParents();
if (parents.empty()) {
log(ERROR, String::compose("Object of type \"%1\" with no parent arrived from server", obj->getObjtype()));
} else {
log(ERROR, String::compose("Object of type \"%1\" with parent \"%2\" arrived from server", obj->getObjtype(), obj->getParents().front()));
}
return;
}
debug(std::cout << "A " << op->getParents().front() << " op from server!" << std::endl << std::flush;);
reply_flag = true;
operationQueue.push_back(op);
if (op->getClassNo() == Atlas::Objects::Operation::ERROR_NO) {
errorArrived(op);
} else if (op->getClassNo() == Atlas::Objects::Operation::INFO_NO) {
infoArrived(op);
}
}
void ClientConnection::errorArrived(const RootOperation & op)
{
debug(std::cout << "ERROR" << std::endl << std::flush;);
error_flag = true;
}
void ClientConnection::infoArrived(const RootOperation & op)
{
debug(std::cout << "INFO" << std::endl << std::flush;);
const std::string & from = op->getFrom();
if (from.empty()) {
try {
const Root & ac = op->getArgs().front();
reply = ac;
// const std::string & acid = reply["id"].asString();
// objects[acid] = new ClientAccount(acid, *this);
}
catch (...) {
std::cerr << "WARNING: Malformed account from server" << std::endl << std::flush;
}
} else {
operation(op);
}
}
int ClientConnection::read() {
if (ios.is_open()) {
codec->poll();
return 0;
} else {
return -1;
}
}
int ClientConnection::connectLocal(const std::string & sockname)
{
#ifdef HAVE_SYS_UN_H
debug(std::cout << "Attempting local connect." << std::endl << std::flush;);
std::string socket;
if (sockname == "") {
socket = var_directory + "/tmp/" + client_socket_name;
} else if (sockname[0] != '/') {
socket = var_directory + "/tmp/" + sockname;
} else {
socket = sockname;
}
struct sockaddr_un sun;
sun.sun_family = AF_UNIX;
strncpy(sun.sun_path, socket.c_str(), sizeof(sun.sun_path));
int fd = ::socket(PF_UNIX, SOCK_STREAM, 0);
if (0 != ::connect(fd, (struct sockaddr *)&sun, sizeof(sun))) {
debug(std::cout << "Local connect refused" << std::endl << std::flush;);
return -1;
}
ios.setSocket(fd);
if (!ios.is_open()) {
std::cerr << "ERROR: For some reason " << sockname << " not open."
<< std::endl << std::flush;
return -1;
}
client_fd = ios.getSocket();
linger();
int ret = negotiate();
if (ret == -1) {
ios.close();
}
return ret;
#else // HAVE_SYS_UN_H
return -1;
#endif // HAVE_SYS_UN_H
}
int ClientConnection::connect(const std::string & server)
{
debug(std::cout << "Connecting to " << server << std::endl << std::flush;);
ios.open(server, client_port_num);
if (!ios.is_open()) {
std::cerr << "ERROR: Could not connect to " << server << "."
<< std::endl << std::flush;
return -1;
}
client_fd = ios.getSocket();
linger();
return negotiate();
}
int ClientConnection::linger()
{
struct linger {
int l_onoff;
int l_linger;
} listenLinger = { 1, 10 };
::setsockopt(client_fd, SOL_SOCKET, SO_LINGER, (char *)&listenLinger,
sizeof(listenLinger));
// Ensure the address can be reused once we are done with it.
return 0;
}
int ClientConnection::negotiate()
{
Atlas::Net::StreamConnect conn("cyphesis_aiclient", ios);
debug(std::cout << "Negotiating... " << std::flush;);
while (conn.getState() == Atlas::Net::StreamConnect::IN_PROGRESS) {
conn.poll();
}
debug(std::cout << "done" << std::endl;);
if (conn.getState() == Atlas::Net::StreamConnect::FAILED) {
std::cerr << "Failed to negotiate" << std::endl;
return -1;
}
codec = conn.getCodec(*this);
encoder = new Atlas::Objects::ObjectsEncoder(*codec);
codec->streamBegin();
return 0;
}
void ClientConnection::login(const std::string & account,
const std::string & password)
{
Atlas::Objects::Operation::Login l;
Anonymous login_arg;
login_arg->setAttr("username", account);
login_arg->setAttr("password", password);
l->setArgs1(login_arg);
reply_flag = false;
error_flag = false;
send(l);
}
void ClientConnection::create(const std::string & account,
const std::string & password)
{
Atlas::Objects::Operation::Create c;
Anonymous create_arg;
create_arg->setAttr("username", account);
create_arg->setAttr("password", password);
c->setArgs1(create_arg);
reply_flag = false;
error_flag = false;
send(c);
}
int ClientConnection::wait()
// Waits for response from server. Used when we are expecting a login response
// Return whether or not an error occured
{
error_flag = false;
reply_flag = false;
while (!reply_flag) {
poll(1);
}
return error_flag ? -1 : 0;
}
void ClientConnection::send(const RootOperation & op)
{
/* debug(Atlas::Codecs::XML c((std::iostream&)std::cout, (Atlas::Bridge*)this);
Atlas::Objects::Encoder enc(&c);
enc.streamMessage(&op);
std::cout << std::endl << std::flush;); */
op->setSerialno(++serialNo);
encoder->streamObjectsMessage(op);
ios << std::flush;
}
void ClientConnection::poll(int timeOut)
{
fd_set infds;
struct timeval tv;
FD_ZERO(&infds);
FD_SET(client_fd, &infds);
tv.tv_sec = timeOut;
tv.tv_usec = 0;
int retval = select(client_fd+1, &infds, NULL, NULL, &tv);
if (retval < 1) {
return;
}
if (FD_ISSET(client_fd, &infds)) {
if (ios.peek() == -1) {
std::cerr << "Server disconnected" << std::endl << std::flush;
error_flag = true;
reply_flag = true;
} else {
codec->poll();
}
}
}
RootOperation ClientConnection::pop()
{
poll();
if (operationQueue.empty()) {
return RootOperation(0);
}
RootOperation op = operationQueue.front();
operationQueue.pop_front();
return op;
}
bool ClientConnection::pending()
{
return !operationQueue.empty();
}
|