Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core4/src/network/Host.cc @ 3614

Last change on this file since 3614 was 3214, checked in by scheusso, 16 years ago

merged netp5 back to trunk

  • Property svn:eol-style set to native
File size: 3.3 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Oliver Scheuss <scheusso [at] ee.ethz.ch>, (C) 2008
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "Host.h"
30
31#include <cassert>
32#include <string>
33
34#include "core/ConsoleCommand.h"
35#include "core/ObjectList.h"
36#include "ChatListener.h"
37
38namespace orxonox {
39
40SetConsoleCommandShortcut(Host, Chat);
41
42Host *Host::instance_=0;
43
44/**
45 * @brief Constructor: assures that only one reference will be created and sets the pointer
46 */
47Host::Host()
48{
49  clientID_=0;
50  assert(instance_==0);
51  instance_=this;
52}
53
54
55/**
56 * @brief Destructor: resets the instance pointer to 0
57 */
58Host::~Host()
59{
60  instance_=0;
61}
62
63/**
64 * This function is used to add an enetpacket to be sent to another peer
65 * @param packet Packet to be added
66 * @param clientID ID of the client the packet should be sent to
67 * @return success?
68 */
69bool Host::addPacket(ENetPacket *packet, int clientID){
70  if(instance_)
71    return instance_->queuePacket(packet, clientID);
72  else
73    return false;
74}
75
76
77// bool Host::chat(std::string& message){
78//   if(!instance_)
79//     return false;
80//   packet::Chat *c = new packet::Chat(message, getPlayerID());
81//   return instance_->sendChat(c);
82// }
83
84// bool Host::receiveChat(packet::Chat *message, unsigned int clientID){
85//   if(instance_)
86//     return instance_->processChat(message, clientID);
87//   else
88//     return false;
89// }
90
91/**
92 * This function returns the ID of the player
93 * @return playerID
94 */
95unsigned int Host::getPlayerID(){
96  if(!instance_)
97    return 0;
98  return instance_->clientID_;
99}
100
101bool Host::Chat(const std::string& message){
102  if(!instance_)
103  {
104    for (ObjectList<ChatListener>::iterator it = ObjectList<ChatListener>::begin(); it != ObjectList<ChatListener>::end(); ++it)
105      it->incomingChat(message, 0);
106    return true;
107  }
108  return instance_->chat(message);
109}
110
111bool Host::Broadcast(const std::string& message){
112  if(!instance_)
113  {
114    for (ObjectList<ChatListener>::iterator it = ObjectList<ChatListener>::begin(); it != ObjectList<ChatListener>::end(); ++it)
115      it->incomingChat(message, CLIENTID_UNKNOWN);
116    return true;
117  }
118  else
119    return instance_->broadcast(message);
120}
121
122bool Host::incomingChat(const std::string& message, unsigned int playerID){
123  for (ObjectList<ChatListener>::iterator it = ObjectList<ChatListener>::begin(); it != ObjectList<ChatListener>::end(); ++it)
124    it->incomingChat(message, playerID);
125
126  return instance_->processChat(message, playerID);
127}
128
129}//namespace orxonox
Note: See TracBrowser for help on using the repository browser.