[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() |
---|
[5649] | 42 | : DataStream() |
---|
[5647] | 43 | { |
---|
| 44 | this->init(); |
---|
[5648] | 45 | /* initialize the references */ |
---|
| 46 | this->networkSocket = new NetworkSocket(); |
---|
[5741] | 47 | this->networkProtocol = new NetworkProtocol(); |
---|
[5723] | 48 | this->synchronizeables = NULL; |
---|
[5648] | 49 | this->connectionMonitor = new ConnectionMonitor(); |
---|
[5647] | 50 | } |
---|
| 51 | |
---|
| 52 | |
---|
[5800] | 53 | NetworkStream::NetworkStream(IPaddress& address, Synchronizeable& sync, NodeType type) |
---|
[5727] | 54 | : DataStream() |
---|
[5647] | 55 | { |
---|
| 56 | this->init(); |
---|
[5746] | 57 | this->networkSocket = new NetworkSocket( address, 9999 ); |
---|
[5734] | 58 | // this->networkSocket->connectToServer(address, 88); |
---|
[5741] | 59 | this->networkProtocol = new NetworkProtocol(); |
---|
[5723] | 60 | this->synchronizeables = &sync; |
---|
[5741] | 61 | this->connectionMonitor = new ConnectionMonitor(); |
---|
[5647] | 62 | } |
---|
[5607] | 63 | |
---|
[5610] | 64 | |
---|
[5800] | 65 | NetworkStream::NetworkStream(Synchronizeable& sync, int port, NodeType type) |
---|
[5727] | 66 | : DataStream() |
---|
[5649] | 67 | { |
---|
| 68 | this->init(); |
---|
| 69 | this->networkSocket = new NetworkSocket(/* address, type */); |
---|
[5746] | 70 | this->networkSocket->listen(9999); |
---|
[5741] | 71 | this->networkProtocol = new NetworkProtocol(); |
---|
[5723] | 72 | this->synchronizeables = &sync; |
---|
[5741] | 73 | this->connectionMonitor = new ConnectionMonitor(); |
---|
[5649] | 74 | } |
---|
| 75 | |
---|
| 76 | |
---|
[5647] | 77 | void NetworkStream::init() |
---|
| 78 | { |
---|
| 79 | /* set the class id for the base object */ |
---|
| 80 | this->setClassID(CL_NETWORK_STREAM, "NetworkStream"); |
---|
[5594] | 81 | } |
---|
| 82 | |
---|
[5647] | 83 | |
---|
[5566] | 84 | NetworkStream::~NetworkStream() |
---|
[5598] | 85 | { |
---|
[5723] | 86 | |
---|
| 87 | networkSocket->disconnectServer(); |
---|
| 88 | |
---|
[5649] | 89 | delete networkSocket; |
---|
[5608] | 90 | delete connectionMonitor; |
---|
[5743] | 91 | delete networkProtocol; |
---|
[5598] | 92 | } |
---|
| 93 | |
---|
[5604] | 94 | void NetworkStream::processData() |
---|
| 95 | { |
---|
[5721] | 96 | |
---|
[5800] | 97 | |
---|
[5615] | 98 | int ret = 0; |
---|
[5741] | 99 | |
---|
[5650] | 100 | /* DOWNSTREAM */ |
---|
[5741] | 101 | |
---|
[5800] | 102 | PRINT(0)("\n\n"); |
---|
| 103 | PRINT(0)("============= DOWNSTREAM:===============\n"); |
---|
[5650] | 104 | /* first of all read the synchronizeable's data: */ |
---|
[5741] | 105 | ret = this->synchronizeables->readBytes((byte*)downBuffer); |
---|
[5800] | 106 | |
---|
[5741] | 107 | /* send the received data to connectionMonitor */ |
---|
[5802] | 108 | this->connectionMonitor->processPacket((byte*)downBuffer, ret); |
---|
[5730] | 109 | |
---|
[5802] | 110 | ret = this->networkProtocol->createHeader((byte*)downBuffer, ret, DATA_STREAM_BUFFER_SIZE, |
---|
| 111 | *(this->synchronizeables), 12); |
---|
[5800] | 112 | |
---|
[5802] | 113 | printf("created header: data packet wights: %i bytes\n", ret); |
---|
[5650] | 114 | /* pass the data to the network socket */ |
---|
[5802] | 115 | ret = this->networkSocket->writeBytes((byte*)downBuffer, ret); |
---|
[5650] | 116 | /* check if there was an error */ |
---|
[5802] | 117 | printf("NetworkSocket: sent %i bytes\n", ret); |
---|
[5650] | 118 | if( ret == -1) { PRINTF(0)("Error in writing data to the NetworkSocket\n");} |
---|
[5741] | 119 | |
---|
| 120 | |
---|
[5800] | 121 | |
---|
[5741] | 122 | /* UPSTREAM */ |
---|
[5724] | 123 | |
---|
[5800] | 124 | ret = 0; |
---|
| 125 | PRINT(0)("============== UPSTREAM:================\n"); |
---|
[5742] | 126 | /* first of all read data from networkSocket*/ |
---|
[5802] | 127 | ret = this->networkSocket->readBlock((byte*)upBuffer, 11/* this is very bad: hard coded packet sizes! */); |
---|
[5650] | 128 | /* error checking: data read? */ |
---|
[5802] | 129 | printf("NetworkSocket: received %i bytes\n", ret); |
---|
| 130 | if( ret != PACKAGE_SIZE + sizeof(Header)) { PRINTF(0)("Error while reading data from the NetworkSocket. Skipping further work\n");} |
---|
| 131 | else |
---|
| 132 | { |
---|
| 133 | /* send the received data to connectionMonitor */ |
---|
| 134 | this->connectionMonitor->processPacket((byte*)upBuffer, ret); |
---|
[5730] | 135 | |
---|
[5802] | 136 | /* extract Header */ |
---|
| 137 | this->networkProtocol->extractHeader((byte*) upBuffer , ret); |
---|
[5730] | 138 | |
---|
[5802] | 139 | /* now pass the data to the sync object */ |
---|
| 140 | this->synchronizeables->writeBytes((byte*)upBuffer, ret); |
---|
| 141 | } |
---|
[5800] | 142 | |
---|
[5604] | 143 | } |
---|