/* 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, 9999 ); // this->networkSocket->connectToServer(address, 88); this->networkProtocol = new NetworkProtocol(); this->synchronizeables = &sync; this->connectionMonitor = new ConnectionMonitor(); } NetworkStream::NetworkStream(Synchronizeable& sync, int port, NodeType type) : DataStream() { this->init(); this->networkSocket = new NetworkSocket(/* address, type */); this->networkSocket->listen(9999); 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 */ /* 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 + sizeof(Header)); this->networkProtocol->createHeader((byte*)downBuffer, ret, DATA_STREAM_BUFFER_SIZE, *(this->synchronizeables), 12); /* pass the data to the network socket */ ret = this->networkSocket->writeBytes((byte*)downBuffer, PACKAGE_SIZE + sizeof(Header)); /* check if there was an error */ if( ret == -1) { PRINTF(0)("Error in writing data to the NetworkSocket\n");} /* UPSTREAM */ ret = 0; /* first of all read data from networkSocket*/ ret = this->networkSocket->readBlock((byte*)upBuffer, PACKAGE_SIZE + sizeof(Header)); /* error checking: data read? */ if( ret != PACKAGE_SIZE + sizeof(Header)) { PRINTF(0)("Error while reading data from the NetworkSocket\n");} /* send the received data to connectionMonitor */ this->connectionMonitor->processPacket((byte*)upBuffer, PACKAGE_SIZE + sizeof(Header)); /* extract Header */ this->networkProtocol->extractHeader((byte*) upBuffer , PACKAGE_SIZE + sizeof(Header)); /* now pass the data to the sync object */ this->synchronizeables->writeBytes((byte*)upBuffer, PACKAGE_SIZE); }