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
|
// Cyphesis Online RPG Server and AI Engine
// Copyright (C) 2000,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: CommClient.cpp,v 1.62 2008-01-28 23:48:32 alriddoch Exp $
#include "CommClient.h"
#include "CommServer.h"
#include "ServerRouting.h"
#include "common/log.h"
#include "common/debug.h"
#include "common/compose.hpp"
#include <Atlas/Objects/Operation.h>
#include <Atlas/Objects/Encoder.h>
#include <Atlas/Net/Stream.h>
#include <Atlas/Codec.h>
#include <iostream>
#include <sstream>
#include <stdexcept>
static const bool debug_flag = false;
CommClient::CommClient(CommServer & svr, int fd, IdentifiedRouter & c) :
CommSocket(svr), Idle(svr),
m_clientIos(fd),
m_codec(NULL), m_encoder(NULL),
m_connection(c), m_connectTime(svr.time())
{
m_clientIos.setTimeout(0,1000);
m_negotiate = new Atlas::Net::StreamAccept("cyphesis " + m_commServer.m_server.getName(), m_clientIos);
}
CommClient::CommClient(CommServer & svr, IdentifiedRouter & c) :
CommSocket(svr), Idle(svr),
m_codec(NULL), m_encoder(NULL),
m_connection(c), m_connectTime(svr.time())
{
m_clientIos.setTimeout(0,1000);
m_negotiate = new Atlas::Net::StreamConnect("cyphesis " + m_commServer.m_server.getName(), m_clientIos);
}
CommClient::~CommClient()
{
delete &m_connection;
if (m_negotiate != NULL) {
delete m_negotiate;
}
if (m_encoder != NULL) {
delete m_encoder;
}
if (m_codec != NULL) {
delete m_codec;
}
m_clientIos.close();
}
void CommClient::setup()
{
debug( std::cout << "Negotiating started" << std::endl << std::flush; );
// Create the server side negotiator
m_negotiate->poll(false);
m_clientIos << std::flush;
}
int CommClient::negotiate()
{
debug(std::cout << "Negotiating... " << std::flush;);
// poll and check if negotiation is complete
m_negotiate->poll();
if (m_negotiate->getState() == Atlas::Negotiate::IN_PROGRESS) {
return 0;
}
debug(std::cout << "done" << std::endl;);
// Check if negotiation failed
if (m_negotiate->getState() == Atlas::Negotiate::FAILED) {
log(NOTICE, "Failed to negotiate");
return -1;
}
// Negotiation was successful
// Get the codec that negotiation established
m_codec = m_negotiate->getCodec(*this);
// Create a new encoder to send high level objects to the codec
m_encoder = new Atlas::Objects::ObjectsEncoder(*m_codec);
// This should always be sent at the beginning of a session
m_codec->streamBegin();
// Acceptor is now finished with
delete m_negotiate;
m_negotiate = NULL;
return 0;
}
int CommClient::operation(const Atlas::Objects::Operation::RootOperation & op)
{
OpVector reply;
long serialno = op->getSerialno();
m_connection.operation(op, reply);
OpVector::const_iterator Iend = reply.end();
for(OpVector::const_iterator I = reply.begin(); I != Iend; ++I) {
debug(std::cout << "sending reply" << std::endl << std::flush;);
if (!op->isDefaultSerialno()) {
// Should we respect existing refnos?
if ((*I)->isDefaultRefno()) {
(*I)->setRefno(serialno);
}
}
if (send(*I) != 0) {
return -1;
}
}
return 0;
}
void CommClient::dispatch()
{
DispatchQueue::const_iterator Iend = m_opQueue.end();
for(DispatchQueue::const_iterator I = m_opQueue.begin(); I != Iend; ++I) {
debug(std::cout << "dispatching op" << std::endl << std::flush;);
if (operation(*I) != 0) {
return;
}
}
m_opQueue.clear();
}
void CommClient::objectArrived(const Atlas::Objects::Root & obj)
{
Atlas::Objects::Operation::RootOperation op = Atlas::Objects::smart_dynamic_cast<Atlas::Objects::Operation::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 client",
obj->getObjtype()));
} else {
log(ERROR, String::compose("Object of type \"%1\" with parent "
"\"%2\" arrived from client",
obj->getObjtype(),
obj->getParents().front()));
}
return;
}
debug(std::cout << "A " << op->getParents().front() << " op from client!" << std::endl << std::flush;);
m_opQueue.push_back(op);
}
void CommClient::idle(time_t t)
{
if (m_negotiate != 0) {
if ((t - m_connectTime) > 10) {
log(NOTICE, "Client disconnected because of negotiation timeout.");
m_clientIos.shutdown();
}
}
}
int CommClient::read()
{
if (m_codec != NULL) {
m_codec->poll();
return 0;
} else {
return negotiate();
}
}
int CommClient::getFd() const
{
return m_clientIos.getSocket();
}
bool CommClient::isOpen() const
{
return m_clientIos.is_open();
}
bool CommClient::eof()
{
return (m_clientIos.fail() || m_clientIos.peek() == EOF);
}
int CommClient::send(const Atlas::Objects::Operation::RootOperation & op)
{
if (!isOpen()) {
log(ERROR, "Writing to closed client");
return -1;
}
if (m_clientIos.fail()) {
return -1;
}
m_encoder->streamObjectsMessage(op);
struct timeval tv = {0, 0};
fd_set sfds;
int cfd = m_clientIos.getSocket();
FD_ZERO(&sfds);
FD_SET(cfd, &sfds);
if (select(++cfd, NULL, &sfds, NULL, &tv) > 0) {
// We only flush to the client if the client is ready
m_clientIos << std::flush;
} else {
debug(std::cout << "Client not ready" << std::endl << std::flush;);
}
// This timeout should only occur if the client was really not
// ready
if (m_clientIos.timeout()) {
log(NOTICE, "Client disconnected because of write timeout.");
m_clientIos.shutdown();
m_clientIos.setstate(std::iostream::failbit);
return -1;
}
return 0;
}
|