metacall/mc_server.cpp

174 lines
4.3 KiB
C++
Raw Normal View History

2011-08-28 15:45:42 +00:00
//
// Copyright (c) 2011 Alex Yatskov
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
#include "metacall.hpp"
namespace metacall {
//
// Server
//
Server::Server() :
2012-02-19 17:55:16 +00:00
m_clientMax(1),
m_clientActive(CLIENT_ID_INVALID)
2011-08-28 15:45:42 +00:00
{
}
Server::~Server() {
disconnectAll();
}
void Server::advance() {
advanceConnecting();
advanceDisconnecting();
advanceConnected();
}
const Binding* Server::binding() const {
2012-02-19 17:55:16 +00:00
return &m_binding;
2011-08-28 15:45:42 +00:00
}
Binding* Server::binding() {
2012-02-19 17:55:16 +00:00
return &m_binding;
2011-08-28 15:45:42 +00:00
}
bool Server::start(int serverPort, int clientsMax) {
stop();
2012-02-19 17:55:16 +00:00
if (m_server.open() && m_server.bind(serverPort) && m_server.listen()) {
m_server.setBlocking(false);
m_server.setNagle(false);
m_clientMax = clientsMax;
2011-08-28 15:45:42 +00:00
return true;
}
stop();
return false;
}
void Server::stop() {
disconnectAll();
2012-02-19 17:55:16 +00:00
m_clientMax = 0;
2011-08-28 15:45:42 +00:00
}
2012-02-19 17:55:16 +00:00
void Server::clients(std::vector<ClientId>* clients) const {
for (ClientMap::const_iterator iter = m_clients.begin(); iter != m_clients.end(); ++iter) {
2011-08-28 15:45:42 +00:00
clients->push_back(iter->first);
}
}
2012-02-19 17:55:16 +00:00
bool Server::clients(ClientId id, ClientData* data) const {
const ClientMap::const_iterator iter = m_clients.find(id);
if (iter == m_clients.end()) {
2011-09-05 03:05:33 +00:00
return false;
2011-08-28 15:45:42 +00:00
}
2011-09-05 03:05:33 +00:00
data->id = iter->first;
data->name = iter->second->socket.hostname();
data->protocol = &iter->second->protocol;
return true;
2011-08-28 15:45:42 +00:00
}
void Server::disconnect(ClientId id) {
2012-02-19 17:55:16 +00:00
const ClientMap::iterator iter = m_clients.find(id);
if (iter != m_clients.end()) {
2011-08-28 15:45:42 +00:00
delete iter->second;
2012-02-19 17:55:16 +00:00
m_clients.erase(iter);
2011-08-28 15:45:42 +00:00
}
}
void Server::disconnectAll() {
2012-02-19 17:55:16 +00:00
for (ClientMap::iterator iter = m_clients.begin(); iter != m_clients.end(); ++iter) {
2011-08-28 15:45:42 +00:00
delete iter->second;
}
2012-02-19 17:55:16 +00:00
m_clients.clear();
2011-08-28 15:45:42 +00:00
}
int Server::clientCount() const {
2012-02-19 17:55:16 +00:00
return m_clients.size();
2011-08-28 15:45:42 +00:00
}
2011-09-05 03:05:33 +00:00
Server::ClientId Server::clientActive() const {
2012-02-19 17:55:16 +00:00
return m_clientActive;
2011-08-28 15:45:42 +00:00
}
void Server::advanceConnecting() {
2012-02-19 17:55:16 +00:00
if (m_clientMax == 0 || !m_server.opened()) {
2011-08-28 15:45:42 +00:00
return;
}
Socket client;
2012-02-19 17:55:16 +00:00
if (!m_server.accept(&client)) {
2011-08-28 15:45:42 +00:00
return;
}
2011-09-05 03:05:33 +00:00
const std::pair<ClientId, ClientEntry*> entry(
registerClientId(),
2012-02-19 17:55:16 +00:00
new ClientEntry(&m_binding)
2011-09-05 03:05:33 +00:00
);
2012-02-19 17:55:16 +00:00
const ClientMap::iterator iter = m_clients.insert(entry).first;
2011-08-28 15:45:42 +00:00
iter->second->socket.set(client.release());
iter->second->socket.setNagle(false);
iter->second->socket.setBlocking(false);
}
void Server::advanceDisconnecting() {
std::vector<ClientMap::iterator> clients;
2012-02-19 17:55:16 +00:00
for (ClientMap::iterator iter = m_clients.begin(); iter != m_clients.end(); ++iter) {
2011-08-28 15:45:42 +00:00
if (!iter->second->socket.connected()) {
clients.push_back(iter);
}
}
for (std::vector<ClientMap::iterator>::iterator iter = clients.begin(); iter != clients.end(); ++iter) {
disconnect((*iter)->first);
}
}
void Server::advanceConnected() {
2012-02-19 17:55:16 +00:00
ASSERT(m_clientActive == CLIENT_ID_INVALID);
2011-08-28 15:45:42 +00:00
2012-02-19 17:55:16 +00:00
for (ClientMap::iterator iter = m_clients.begin(); iter != m_clients.end(); ++iter) {
m_clientActive = iter->first;
2011-08-28 15:45:42 +00:00
iter->second->protocol.advance();
}
2012-02-19 17:55:16 +00:00
m_clientActive = CLIENT_ID_INVALID;
2011-08-28 15:45:42 +00:00
}
2011-09-05 03:05:33 +00:00
Server::ClientId Server::registerClientId() {
2012-02-19 17:55:16 +00:00
static int s_id = CLIENT_ID_INVALID;
while (++s_id == CLIENT_ID_INVALID);
return static_cast<ClientId>(s_id);
2011-08-28 15:45:42 +00:00
}
}