proxy.cpp

Go to the documentation of this file.
00001 /* $Id: proxy.cpp 24730 2008-03-17 05:11:14Z dave $ */
00002 /*
00003    Copyright (C) 2007 - 2008
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License version 2
00008    or at your option any later version.
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY.
00011 
00012    See the COPYING file for more details.
00013 */
00014 
00015 #include "global.hpp"
00016 
00017 #include "proxy.hpp"
00018 
00019 #include <map>
00020 
00021 namespace
00022 {
00023 
00024 typedef std::map<network::connection,network::connection> connection_map;
00025 connection_map clients_to_servers, servers_to_clients;
00026 
00027 network::connection find_peer(network::connection sock)
00028 {
00029     const connection_map::const_iterator i = clients_to_servers.find(sock);
00030     if(i == clients_to_servers.end()) {
00031         const connection_map::const_iterator i = servers_to_clients.find(sock);
00032         if(i == servers_to_clients.end()) {
00033             return 0;
00034         } else {
00035             return i->second;
00036         }
00037     } else {
00038         return i->second;
00039     }
00040 }
00041 
00042 }
00043 
00044 namespace proxy
00045 {
00046 
00047 void create_proxy(network::connection sock, const std::string& host, int port)
00048 {
00049     const network::connection peer = network::connect(host,port);
00050     if(!peer) {
00051         network::disconnect(sock);
00052     } else {
00053         clients_to_servers.insert(std::pair<network::connection,network::connection>(sock,peer));
00054         servers_to_clients.insert(std::pair<network::connection,network::connection>(peer,sock));
00055     }
00056 }
00057 
00058 bool is_proxy(network::connection sock)
00059 {
00060     return clients_to_servers.count(sock) || servers_to_clients.count(sock);
00061 }
00062 
00063 void disconnect(network::connection sock)
00064 {
00065     const network::connection peer = find_peer(sock);
00066     if(!peer) {
00067         return;
00068     }
00069 
00070     servers_to_clients.erase(sock);
00071     servers_to_clients.erase(peer);
00072     clients_to_servers.erase(sock);
00073     clients_to_servers.erase(peer);
00074 
00075     network::disconnect(peer);
00076 }
00077 
00078 void received_data(network::connection sock, simple_wml::document& data)
00079 {
00080     const network::connection peer = find_peer(sock);
00081     if(!peer) {
00082         return;
00083     }
00084 
00085     const simple_wml::string_span& output = data.output_compressed();
00086     network::send_raw_data(output.begin(), output.size(), peer);
00087 }
00088 
00089 }

Generated by doxygen 1.5.5 on 23 May 2008 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs