/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: claudio co-programmer: */ /* this is for debug output. It just says, that all calls to PRINT() belong to the DEBUG_MODULE_NETWORK module For more information refere to https://www.orxonox.net/cgi-bin/trac.cgi/wiki/DebugOutput */ #define DEBUG_MODULE_NETWORK #include "base_object.h" #include "network_protocol.h" #include "network_socket.h" #include "connection_monitor.h" #include "synchronizeable.h" #include "list.h" #include "debug.h" /* include your own header */ #include "network_stream.h" /* probably unnecessary */ using namespace std; #define PACKAGE_SIZE 256 NetworkStream::NetworkStream() : DataStream() { this->init(); /* initialize the references */ this->networkSocket = new NetworkSocket(); this->networkProtocol = new NetworkProtocol(); this->synchronizeables = NULL; this->connectionMonitor = new ConnectionMonitor(); } NetworkStream::NetworkStream(IPaddress& address, Synchronizeable& sync, NodeType type) : DataStream() { this->init(); this->networkSocket = new NetworkSocket(address); this->networkProtocol = new NetworkProtocol(); this->synchronizeables = &sync; this->connectionMonitor = new ConnectionMonitor(); } NetworkStream::NetworkStream(Synchronizeable& sync, unsigned int port, NodeType type) : DataStream() { this->init(); this->networkSocket = new NetworkSocket(); this->networkSocket->listen(port); this->networkProtocol = new NetworkProtocol(); this->synchronizeables = &sync; this->connectionMonitor = new ConnectionMonitor(); } void NetworkStream::init() { /* set the class id for the base object */ this->setClassID(CL_NETWORK_STREAM, "NetworkStream"); } NetworkStream::~NetworkStream() { networkSocket->disconnectServer(); delete networkSocket; delete connectionMonitor; delete networkProtocol; } void NetworkStream::processData() { int ret = 0; /* DOWNSTREAM */ printf("\nSynchronizeable: %s\n", this->synchronizeables->getName()); PRINT(0)("============= DOWNSTREAM:===============\n"); /* first of all read the synchronizeable's data: */ ret = this->synchronizeables->readBytes((byte*)downBuffer); /* send the received data to connectionMonitor */ this->connectionMonitor->processPacket((byte*)downBuffer, ret); ret = this->networkProtocol->createHeader((byte*)downBuffer, ret, DATA_STREAM_BUFFER_SIZE, *(this->synchronizeables), 12); printf("created header: data packet wights: %i bytes\n", ret); /* pass the data to the network socket */ ret = this->networkSocket->writeBytes((byte*)downBuffer, ret); /* check if there was an error */ printf("NetworkSocket: sent %i bytes\n", ret); if( ret == -1) { PRINTF(0)("Error in writing data to the NetworkSocket\n");} /* UPSTREAM */ ret = 0; PRINT(0)("============== UPSTREAM:================\n"); /* first of all read data from networkSocket*/ ret = this->networkSocket->readBlock((byte*)upBuffer, 11/* this is very bad: hard coded packet sizes! */); /* error checking: data read? */ printf("NetworkSocket: received %i bytes\n", ret); if( ret != 11 /*PACKAGE_SIZE + sizeof(Header)*/) { PRINTF(0)("Error while reading data from the NetworkSocket. Skipping further work\n");} else { /* send the received data to connectionMonitor */ this->connectionMonitor->processPacket((byte*)upBuffer, ret); /* extract Header */ Header header = this->networkProtocol->extractHeader((byte*) upBuffer , ret); /* now pass the data to the sync object */ this->synchronizeables->writeBytes((byte*)upBuffer, header.length); } }