[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 |
---|
[5566] | 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 |
---|
[5724] | 21 | #define PACKAGE_SIZE 8 |
---|
[5566] | 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 | |
---|
[5647] | 38 | |
---|
[5649] | 39 | NetworkStream::NetworkStream() |
---|
| 40 | : DataStream() |
---|
[5647] | 41 | { |
---|
| 42 | this->init(); |
---|
[5648] | 43 | /* initialize the references */ |
---|
| 44 | this->networkSocket = new NetworkSocket(); |
---|
[5723] | 45 | this->synchronizeables = NULL; |
---|
[5648] | 46 | this->connectionMonitor = new ConnectionMonitor(); |
---|
[5647] | 47 | } |
---|
| 48 | |
---|
| 49 | |
---|
[5649] | 50 | NetworkStream::NetworkStream(IPaddress& address, Synchronizeable& sync, NodeType type) |
---|
[5727] | 51 | : DataStream() |
---|
[5647] | 52 | { |
---|
| 53 | this->init(); |
---|
[5734] | 54 | this->networkSocket = new NetworkSocket( address, 88 ); |
---|
| 55 | // this->networkSocket->connectToServer(address, 88); |
---|
[5723] | 56 | |
---|
| 57 | this->synchronizeables = &sync; |
---|
[5647] | 58 | } |
---|
[5607] | 59 | |
---|
[5610] | 60 | |
---|
[5649] | 61 | NetworkStream::NetworkStream(Synchronizeable& sync, int port, NodeType type) |
---|
[5727] | 62 | : DataStream() |
---|
[5649] | 63 | { |
---|
| 64 | this->init(); |
---|
| 65 | this->networkSocket = new NetworkSocket(/* address, type */); |
---|
[5723] | 66 | |
---|
| 67 | this->synchronizeables = &sync; |
---|
[5649] | 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"); |
---|
[5594] | 75 | } |
---|
| 76 | |
---|
[5647] | 77 | |
---|
[5566] | 78 | NetworkStream::~NetworkStream() |
---|
[5598] | 79 | { |
---|
[5723] | 80 | |
---|
| 81 | networkSocket->disconnectServer(); |
---|
| 82 | |
---|
[5649] | 83 | delete networkSocket; |
---|
[5608] | 84 | delete connectionMonitor; |
---|
[5566] | 85 | |
---|
[5598] | 86 | } |
---|
| 87 | |
---|
[5604] | 88 | void NetworkStream::processData() |
---|
| 89 | { |
---|
[5721] | 90 | |
---|
[5723] | 91 | |
---|
[5615] | 92 | int ret = 0; |
---|
[5650] | 93 | |
---|
| 94 | |
---|
[5730] | 95 | |
---|
[5650] | 96 | /* DOWNSTREAM */ |
---|
| 97 | /* first of all read the synchronizeable's data: */ |
---|
[5730] | 98 | |
---|
| 99 | ret = this->synchronizeables->readBytes((byte*)downBuffer); |
---|
| 100 | |
---|
[5650] | 101 | /* pass the data to the network socket */ |
---|
[5730] | 102 | ret = this->networkSocket->writeBytes((byte*)downBuffer, PACKAGE_SIZE); |
---|
[5650] | 103 | /* check if there was an error */ |
---|
| 104 | if( ret == -1) { PRINTF(0)("Error in writing data to the NetworkSocket\n");} |
---|
| 105 | |
---|
| 106 | |
---|
| 107 | /* UPSTREAM */ |
---|
| 108 | /* first read 10bytes of data (debug) */ |
---|
[5724] | 109 | |
---|
[5734] | 110 | ret = 0; |
---|
| 111 | while(ret == 0) ret = this->networkSocket->readBlock((byte*)upBuffer,PACKAGE_SIZE); |
---|
[5650] | 112 | /* error checking: data read? */ |
---|
[5734] | 113 | if( ret != PACKAGE_SIZE) { PRINTF(0)("Error while reading data from the NetworkSocket\n");} |
---|
[5650] | 114 | /* now pass the data to the sync object */ |
---|
[5730] | 115 | |
---|
| 116 | this->synchronizeables->writeBytes((byte*)upBuffer, PACKAGE_SIZE); |
---|
| 117 | |
---|
[5650] | 118 | |
---|
| 119 | |
---|
| 120 | |
---|
[5604] | 121 | } |
---|
[5598] | 122 | |
---|