[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; |
---|
[5648] | 47 | this->networkSocket = new NetworkSocket(); |
---|
[5741] | 48 | this->networkProtocol = new NetworkProtocol(); |
---|
[5723] | 49 | this->synchronizeables = NULL; |
---|
[5648] | 50 | this->connectionMonitor = new ConnectionMonitor(); |
---|
[5647] | 51 | } |
---|
| 52 | |
---|
[5996] | 53 | NetworkStream::NetworkStream(IPaddress& address, NodeType type) |
---|
| 54 | { |
---|
| 55 | this->type = type; |
---|
| 56 | this->init(); |
---|
| 57 | this->networkSocket = new NetworkSocket(address); |
---|
| 58 | this->networkProtocol = new NetworkProtocol(); |
---|
| 59 | this->synchronizeables = NULL; |
---|
| 60 | this->connectionMonitor = new ConnectionMonitor(); |
---|
| 61 | } |
---|
[5647] | 62 | |
---|
[5996] | 63 | |
---|
| 64 | NetworkStream::NetworkStream(unsigned int port, NodeType type) |
---|
| 65 | { |
---|
| 66 | this->type = type; |
---|
| 67 | this->init(); |
---|
| 68 | this->networkSocket = new NetworkSocket(); |
---|
| 69 | // this->networkSocket->listen(port); |
---|
| 70 | this->networkProtocol = new NetworkProtocol(); |
---|
| 71 | this->synchronizeables = NULL; |
---|
| 72 | this->connectionMonitor = new ConnectionMonitor(); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | |
---|
[5800] | 76 | NetworkStream::NetworkStream(IPaddress& address, Synchronizeable& sync, NodeType type) |
---|
[5996] | 77 | : DataStream() |
---|
[5647] | 78 | { |
---|
[5996] | 79 | this->type = type; |
---|
[5647] | 80 | this->init(); |
---|
[5804] | 81 | this->networkSocket = new NetworkSocket(address); |
---|
[5741] | 82 | this->networkProtocol = new NetworkProtocol(); |
---|
[5723] | 83 | this->synchronizeables = &sync; |
---|
[5741] | 84 | this->connectionMonitor = new ConnectionMonitor(); |
---|
[5996] | 85 | this->bActive = true; |
---|
[5647] | 86 | } |
---|
[5607] | 87 | |
---|
[5610] | 88 | |
---|
[5811] | 89 | NetworkStream::NetworkStream(unsigned int port, Synchronizeable& sync, NodeType type) |
---|
[5996] | 90 | : DataStream() |
---|
[5649] | 91 | { |
---|
[5996] | 92 | this->type = type; |
---|
[5649] | 93 | this->init(); |
---|
[5804] | 94 | this->networkSocket = new NetworkSocket(); |
---|
[5996] | 95 | // this->networkSocket->listen(port); |
---|
[5741] | 96 | this->networkProtocol = new NetworkProtocol(); |
---|
[5723] | 97 | this->synchronizeables = &sync; |
---|
[5741] | 98 | this->connectionMonitor = new ConnectionMonitor(); |
---|
[5996] | 99 | this->bActive = true; |
---|
[5649] | 100 | } |
---|
| 101 | |
---|
| 102 | |
---|
[5647] | 103 | void NetworkStream::init() |
---|
| 104 | { |
---|
| 105 | /* set the class id for the base object */ |
---|
| 106 | this->setClassID(CL_NETWORK_STREAM, "NetworkStream"); |
---|
[5809] | 107 | this->state = NET_REC_HEADER; |
---|
[5996] | 108 | this->bActive = false; |
---|
[5594] | 109 | } |
---|
| 110 | |
---|
[5647] | 111 | |
---|
[5566] | 112 | NetworkStream::~NetworkStream() |
---|
[5598] | 113 | { |
---|
[5723] | 114 | |
---|
[5996] | 115 | networkSocket->disconnectServer(); |
---|
[5723] | 116 | |
---|
[5996] | 117 | if( this->networkSocket) |
---|
| 118 | delete networkSocket; |
---|
[5805] | 119 | |
---|
[5996] | 120 | delete connectionMonitor; |
---|
| 121 | delete networkProtocol; |
---|
[5598] | 122 | } |
---|
| 123 | |
---|
[5996] | 124 | |
---|
| 125 | void NetworkStream::connectSynchronizeable(Synchronizeable& sync) |
---|
| 126 | { |
---|
| 127 | this->synchronizeables = &sync; |
---|
| 128 | if( this->networkSocket != NULL) |
---|
| 129 | this->bActive = true; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | |
---|
[5604] | 133 | void NetworkStream::processData() |
---|
| 134 | { |
---|
[5809] | 135 | int dataLength = 0; |
---|
[5741] | 136 | |
---|
[5650] | 137 | /* DOWNSTREAM */ |
---|
[5804] | 138 | printf("\nSynchronizeable: %s\n", this->synchronizeables->getName()); |
---|
[5800] | 139 | PRINT(0)("============= DOWNSTREAM:===============\n"); |
---|
[5650] | 140 | /* first of all read the synchronizeable's data: */ |
---|
[5996] | 141 | if( this->isServer()) |
---|
| 142 | dataLength = this->synchronizeables->readBytes((byte*)downBuffer); |
---|
[5800] | 143 | |
---|
[5996] | 144 | if( dataLength > 0) |
---|
| 145 | { |
---|
| 146 | /* send the received data to connectionMonitor */ |
---|
| 147 | this->connectionMonitor->processPacket((byte*)downBuffer, dataLength); |
---|
[5730] | 148 | |
---|
[5996] | 149 | dataLength = this->networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE, |
---|
| 150 | *(this->synchronizeables), 12/* some random number (no real id)*/); |
---|
[5800] | 151 | |
---|
[5996] | 152 | /* pass the data to the network socket */ |
---|
| 153 | // dataLength = this->networkSocket->writeBytes((byte*)downBuffer, dataLength); |
---|
| 154 | /* check if there was an error */ |
---|
| 155 | if( dataLength == -1) |
---|
| 156 | { |
---|
| 157 | PRINTF(0)("Error in writing data to the NetworkSocket\n"); |
---|
| 158 | } |
---|
| 159 | } |
---|
[5741] | 160 | |
---|
| 161 | |
---|
| 162 | /* UPSTREAM */ |
---|
[5809] | 163 | dataLength = 0; |
---|
[5800] | 164 | PRINT(0)("============== UPSTREAM:================\n"); |
---|
[5809] | 165 | /* first of all read the next Orxonox Network Header */ |
---|
| 166 | |
---|
[5810] | 167 | if( this->state == NET_REC_HEADER) |
---|
[5802] | 168 | { |
---|
[5996] | 169 | // dataLength = this->networkSocket->readBlock((byte*)upBuffer, sizeof(Header)); |
---|
[5810] | 170 | if( dataLength == sizeof(Header)) |
---|
| 171 | { |
---|
| 172 | this->packetHeader = this->networkProtocol->extractHeader((byte*) upBuffer , dataLength); |
---|
| 173 | printf("NetworkStream::processData() - Got Header: Protocol %u, Version: %u, Sender: %u, Receiver: %u, Length: %u\n", |
---|
| 174 | this->packetHeader.protocol, this->packetHeader.version, this->packetHeader.senderID, |
---|
| 175 | this->packetHeader.receiverID, this->packetHeader.length); |
---|
| 176 | /* FIXME: what if it was no this->packetHeader? catch? eg: the protocol identifier, receiver id*/ |
---|
[5730] | 177 | |
---|
[5810] | 178 | this->state = NET_REC_DATA; |
---|
| 179 | } |
---|
| 180 | } |
---|
| 181 | if( this->state == NET_REC_DATA) |
---|
| 182 | { |
---|
| 183 | /* now read the data */ |
---|
[5996] | 184 | // dataLength = this->networkSocket->readBlock((byte*)upBuffer, this->packetHeader.length); |
---|
[5810] | 185 | /* check if the data is available and process it if so */ |
---|
| 186 | if( dataLength == this->packetHeader.length) |
---|
| 187 | { |
---|
[5996] | 188 | printf("NetworkStream::processData() - Got Data: %i bytes\n", dataLength); |
---|
[5810] | 189 | /* send the received data to connectionMonitor */ |
---|
| 190 | this->connectionMonitor->processPacket((byte*)upBuffer, this->packetHeader.length); |
---|
| 191 | /* now pass the data to the sync object */ |
---|
[5996] | 192 | if( !this->isServer()) |
---|
| 193 | this->synchronizeables->writeBytes((byte*)upBuffer, this->packetHeader.length); |
---|
[5730] | 194 | |
---|
[5810] | 195 | this->state = NET_REC_HEADER; |
---|
| 196 | } |
---|
[5802] | 197 | } |
---|
[5800] | 198 | |
---|
[5809] | 199 | |
---|
[5604] | 200 | } |
---|