Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/lib/network/network_manager.cc @ 6095

Last change on this file since 6095 was 6095, checked in by patrick, 18 years ago

network: game server world loadin

File size: 4.2 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Patrick Boenzli
13   co-programmer: ...
14*/
15
16
17/* this is for debug output. It just says, that all calls to PRINT() belong to the DEBUG_MODULE_NETWORK module
18   For more information refere to https://www.orxonox.net/cgi-bin/trac.cgi/wiki/DebugOutput
19*/
20#define DEBUG_MODULE_NETWORK
21
22
23#include "network_stream.h"
24#include "class_list.h"
25
26#include "debug.h"
27
28
29/* include your own header */
30#include "network_manager.h"
31
32
33/* using namespace std is default, this needs to be here */
34using namespace std;
35
36
37NetworkManager* NetworkManager::singletonRef = NULL;
38
39/**
40 *  standard constructor
41 */
42NetworkManager::NetworkManager()
43{
44  /* set the class id for the base object */
45  this->setClassID(CL_NETWORK_MANAGER, "NetworkManager");
46
47  /* initialize the references */
48  this->netStreamList = NULL;
49  this->syncList = NULL;
50  this->tmpStream = NULL;
51  this->hostID = -1;
52
53  PRINTF(0)("NetworkManager created\n");
54}
55
56
57/**
58 *  standard deconstructor
59 */
60NetworkManager::~NetworkManager()
61{}
62
63
64/**
65 *  initializes the network manager
66 */
67void NetworkManager::initialize()
68{
69  /* get the synchronizeable list from the class list */
70  this->netStreamList = ClassList::getList(CL_SYNCHRONIZEABLE);
71  PRINTF(0)("NetworkManager initzalized\n");
72}
73
74
75/**
76 *  shutsdown the network manager
77 */
78void NetworkManager::shutdown()
79{
80
81}
82
83
84/**
85 *  creates a connection from one object to a host
86 * @param hostName: the name of the destination host
87 */
88int NetworkManager::establishConnection(const char* name, unsigned int port)
89{
90  IPaddress ipAddress;
91  int error = SDLNet_ResolveHost(&ipAddress, name, port);
92  if( error == -1) {
93    printf("\n\nerror on address resolution, program inconsistency\n\n");
94    return -1;
95  }
96
97  this->tmpStream = new NetworkStream(ipAddress);
98  return 1;
99}
100
101
102/**
103 *  creates a new NetworkStream of server type
104 * @param port: number of the TCP port
105 */
106int NetworkManager::createServer(unsigned int port)
107{
108  this->tmpStream = new NetworkStream(port);
109  this->bGameServer = true;
110  SDL_Delay(20);
111  return 1;
112}
113
114
115/**
116 *  creates a connection from one object to a host
117 * @param address: the address of the destination host
118 * @param synchronizeable: reference to the sync object
119 */
120NetworkStream& NetworkManager::establishConnection(IPaddress& address, Synchronizeable& sync)
121{
122  /* creating a new network stream, it will register itself automaticaly to the class list */
123  this->tmpStream = new NetworkStream(address);
124  this->tmpStream->connectSynchronizeable(sync);
125}
126
127
128/**
129 *  creates a new NetworkStream of server type
130 * @param sync: the listener
131 */
132NetworkStream& NetworkManager::createServer(Synchronizeable& sync, unsigned int port)
133{
134  PRINTF(0)("Create a new server socket\n");
135  /* creating a new network stream, it will register itself automaticaly to the class list */
136  this->tmpStream = new NetworkStream(port);
137  this->tmpStream->connectSynchronizeable(sync);
138  this->bGameServer = true;
139}
140
141
142/**
143 *  teardown a connection
144 */
145void NetworkManager::shutdownConnection()
146{
147  PRINTF(0)("Shutdown connection\n");
148}
149
150
151void NetworkManager::connectSynchronizeable(Synchronizeable& sync)
152{
153  this->tmpStream->connectSynchronizeable(sync);
154}
155
156
157/**
158 *  sync the network
159 */
160void NetworkManager::synchronize()
161{
162  if (this->netStreamList != NULL || (this->netStreamList = ClassList::getList(CL_NETWORK_STREAM)) != NULL)
163  {
164    std::list<BaseObject*>::const_iterator stream;
165    for (stream = this->netStreamList->begin(); stream != this->netStreamList->end(); ++stream)
166      if( static_cast<NetworkStream*>(*stream)->isActive())
167        static_cast<NetworkStream*>(*stream)->processData();
168
169  }
170
171}
172
173/**
174 * Sets the hostID to a specific number
175 * @param id: The new ID
176 */
177void NetworkManager::setHostID(int id)
178{
179  this->hostID = id;
180}
Note: See TracBrowser for help on using the repository browser.