[5566] | 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: |
---|
[5601] | 12 | main-programmer: claudio |
---|
[5800] | 13 | co-programmer: |
---|
[5566] | 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 | |
---|
[5747] | 22 | |
---|
[5647] | 23 | #include "base_object.h" |
---|
[5731] | 24 | #include "network_protocol.h" |
---|
[5647] | 25 | #include "network_socket.h" |
---|
| 26 | #include "connection_monitor.h" |
---|
| 27 | #include "synchronizeable.h" |
---|
| 28 | #include "list.h" |
---|
[5649] | 29 | #include "debug.h" |
---|
[5647] | 30 | |
---|
[5566] | 31 | /* include your own header */ |
---|
| 32 | #include "network_stream.h" |
---|
| 33 | |
---|
[5595] | 34 | /* probably unnecessary */ |
---|
[5594] | 35 | using namespace std; |
---|
| 36 | |
---|
[5595] | 37 | |
---|
[5747] | 38 | #define PACKAGE_SIZE 256 |
---|
[5647] | 39 | |
---|
[5747] | 40 | |
---|
[5800] | 41 | NetworkStream::NetworkStream() |
---|
[5996] | 42 | : DataStream() |
---|
[5647] | 43 | { |
---|
| 44 | this->init(); |
---|
[5648] | 45 | /* initialize the references */ |
---|
[5996] | 46 | this->type = NET_CLIENT; |
---|
[5741] | 47 | this->networkProtocol = new NetworkProtocol(); |
---|
[5648] | 48 | this->connectionMonitor = new ConnectionMonitor(); |
---|
[5647] | 49 | } |
---|
| 50 | |
---|
[6007] | 51 | NetworkStream::NetworkStream(IPaddress& address) |
---|
[5996] | 52 | { |
---|
[6007] | 53 | this->type = NET_CLIENT; |
---|
[5996] | 54 | this->init(); |
---|
[6007] | 55 | this->networkSockets.push_back(new NetworkSocket(address)); |
---|
[5996] | 56 | this->networkProtocol = new NetworkProtocol(); |
---|
| 57 | this->connectionMonitor = new ConnectionMonitor(); |
---|
| 58 | } |
---|
[5647] | 59 | |
---|
[5996] | 60 | |
---|
[6007] | 61 | NetworkStream::NetworkStream(unsigned int port) |
---|
[5996] | 62 | { |
---|
[6007] | 63 | this->type = NET_SERVER; |
---|
[5996] | 64 | this->init(); |
---|
[6007] | 65 | this->serverSocket = new ServerSocket(port); |
---|
[5996] | 66 | this->networkProtocol = new NetworkProtocol(); |
---|
| 67 | this->connectionMonitor = new ConnectionMonitor(); |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | |
---|
[5647] | 71 | void NetworkStream::init() |
---|
| 72 | { |
---|
| 73 | /* set the class id for the base object */ |
---|
| 74 | this->setClassID(CL_NETWORK_STREAM, "NetworkStream"); |
---|
[5996] | 75 | this->bActive = false; |
---|
[6007] | 76 | this->serverSocket = NULL; |
---|
[5594] | 77 | } |
---|
| 78 | |
---|
[5647] | 79 | |
---|
[5566] | 80 | NetworkStream::~NetworkStream() |
---|
[5598] | 81 | { |
---|
[6007] | 82 | if ( this->serverSocket ) |
---|
| 83 | { |
---|
| 84 | serverSocket->close(); |
---|
| 85 | delete serverSocket; |
---|
| 86 | } |
---|
[5723] | 87 | |
---|
[6007] | 88 | for (NetworkSocketVector::iterator i = networkSockets.begin(); i!=networkSockets.end(); i++) |
---|
| 89 | { |
---|
| 90 | if ( *i ) |
---|
| 91 | { |
---|
| 92 | (*i)->disconnectServer(); |
---|
| 93 | delete *i; |
---|
| 94 | } |
---|
| 95 | } |
---|
[5723] | 96 | |
---|
[5996] | 97 | delete connectionMonitor; |
---|
| 98 | delete networkProtocol; |
---|
[5598] | 99 | } |
---|
| 100 | |
---|
[5996] | 101 | |
---|
| 102 | void NetworkStream::connectSynchronizeable(Synchronizeable& sync) |
---|
| 103 | { |
---|
[6007] | 104 | this->synchronizeables.push_back(&sync); |
---|
| 105 | |
---|
| 106 | if( this->networkSockets.size()>0 ) |
---|
[5996] | 107 | this->bActive = true; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | |
---|
[5604] | 111 | void NetworkStream::processData() |
---|
| 112 | { |
---|
[6007] | 113 | #if 0 |
---|
[5809] | 114 | int dataLength = 0; |
---|
[5741] | 115 | |
---|
[5650] | 116 | /* DOWNSTREAM */ |
---|
[5804] | 117 | printf("\nSynchronizeable: %s\n", this->synchronizeables->getName()); |
---|
[5800] | 118 | PRINT(0)("============= DOWNSTREAM:===============\n"); |
---|
[5650] | 119 | /* first of all read the synchronizeable's data: */ |
---|
[5996] | 120 | if( this->isServer()) |
---|
| 121 | dataLength = this->synchronizeables->readBytes((byte*)downBuffer); |
---|
[5800] | 122 | |
---|
[5996] | 123 | if( dataLength > 0) |
---|
| 124 | { |
---|
| 125 | /* send the received data to connectionMonitor */ |
---|
| 126 | this->connectionMonitor->processPacket((byte*)downBuffer, dataLength); |
---|
[5730] | 127 | |
---|
[5996] | 128 | dataLength = this->networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE, |
---|
| 129 | *(this->synchronizeables), 12/* some random number (no real id)*/); |
---|
[5800] | 130 | |
---|
[5996] | 131 | /* pass the data to the network socket */ |
---|
| 132 | // dataLength = this->networkSocket->writeBytes((byte*)downBuffer, dataLength); |
---|
| 133 | /* check if there was an error */ |
---|
| 134 | if( dataLength == -1) |
---|
| 135 | { |
---|
| 136 | PRINTF(0)("Error in writing data to the NetworkSocket\n"); |
---|
| 137 | } |
---|
| 138 | } |
---|
[5741] | 139 | |
---|
| 140 | |
---|
| 141 | /* UPSTREAM */ |
---|
[5809] | 142 | dataLength = 0; |
---|
[5800] | 143 | PRINT(0)("============== UPSTREAM:================\n"); |
---|
[5809] | 144 | /* first of all read the next Orxonox Network Header */ |
---|
| 145 | |
---|
[5810] | 146 | if( this->state == NET_REC_HEADER) |
---|
[5802] | 147 | { |
---|
[5996] | 148 | // dataLength = this->networkSocket->readBlock((byte*)upBuffer, sizeof(Header)); |
---|
[5810] | 149 | if( dataLength == sizeof(Header)) |
---|
| 150 | { |
---|
| 151 | this->packetHeader = this->networkProtocol->extractHeader((byte*) upBuffer , dataLength); |
---|
| 152 | printf("NetworkStream::processData() - Got Header: Protocol %u, Version: %u, Sender: %u, Receiver: %u, Length: %u\n", |
---|
| 153 | this->packetHeader.protocol, this->packetHeader.version, this->packetHeader.senderID, |
---|
| 154 | this->packetHeader.receiverID, this->packetHeader.length); |
---|
| 155 | /* FIXME: what if it was no this->packetHeader? catch? eg: the protocol identifier, receiver id*/ |
---|
[5730] | 156 | |
---|
[5810] | 157 | this->state = NET_REC_DATA; |
---|
| 158 | } |
---|
| 159 | } |
---|
| 160 | if( this->state == NET_REC_DATA) |
---|
| 161 | { |
---|
| 162 | /* now read the data */ |
---|
[5996] | 163 | // dataLength = this->networkSocket->readBlock((byte*)upBuffer, this->packetHeader.length); |
---|
[5810] | 164 | /* check if the data is available and process it if so */ |
---|
| 165 | if( dataLength == this->packetHeader.length) |
---|
| 166 | { |
---|
[5996] | 167 | printf("NetworkStream::processData() - Got Data: %i bytes\n", dataLength); |
---|
[5810] | 168 | /* send the received data to connectionMonitor */ |
---|
| 169 | this->connectionMonitor->processPacket((byte*)upBuffer, this->packetHeader.length); |
---|
| 170 | /* now pass the data to the sync object */ |
---|
[5996] | 171 | if( !this->isServer()) |
---|
| 172 | this->synchronizeables->writeBytes((byte*)upBuffer, this->packetHeader.length); |
---|
[5730] | 173 | |
---|
[5810] | 174 | this->state = NET_REC_HEADER; |
---|
| 175 | } |
---|
[5802] | 176 | } |
---|
[5800] | 177 | |
---|
[6007] | 178 | #endif |
---|
[5604] | 179 | } |
---|