Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

# Platform/Aperios1.3.2/TCPHandler.cpp

Go to the documentation of this file.

    00001 /**
    00002 * @file  Platform/Aperios1.3.2/TCPHandler.cpp
    00003 *
    00004 * Class for using NetSender/NetReceivers over a TCP connection
    00005 *
    00006 * @author <A href=mailto:robocup@m-wachter.de>Michael Wachter</A>
    00007 *
    00008 */
    00009
    00010 #include "TCPHandler.h"
    00011
    00012 #include <stdio.h>
    00013 #include <iostream.h>
    00014 #include <iomanip.h>
    00015
    00016 /*
    00017   * The associated sender first finds out the size of the data to send
    00018   * and streams the data into a memory buffer. After that the sender calls
    00019   * sendPackage in the TCPHandler and the TCPHandler takes care of sending
    00020   * the data to the other host.
    00021   *
    00022   * the TCPHandler first sends the size of the data and after that the data
    00023   * itself.
    00024   *
    00025   * If there is incomining data the TCPHandler first reads the size of the
    00026   * incoming Data and after that all received data is collected in a memory
    00027   * buffer until it is complete. Now the associated receiver is called to
    00028   * stream the data from the buffer.
    00029 */
    00030
    00031 TCPHandler::TCPHandler(NetSenderBase &theSender, NetReceiverBase &theReceiver)
    00032 :TCPEndpoint(16384,16384)
    00033 {
    00034    sender = &theSender;
    00035    receiver = &theReceiver;
    00036    sender->setHandler(this);
    00037    sizeOfDataToRead = 0;
    00038    sendingDone = true;
    00039
    00040    sender->getPackage(131072*2);
    00041    receiver->getPackage(8192);
    00042
    00043 }
    00044
    00045 void TCPHandler::setTransmitters(NetSenderBase& theSender, NetReceiverBase& theReceiver)
    00046 {
    00047   sender = &theSender;
    00048   receiver = &theReceiver;
    00049   sender->setHandler(this);
    00050 }
    00051
    00052 void TCPHandler::onReceive(void* someData, int size)
    00053 {
    00054    // "size" is always the number of bytes left to read
    00055
    00056    // cout << "TCPHandler::onReceive(" << someData <<","<<size<<") \n";
    00057
    00058    // if(sendingDone) sender->sendDone();
    00059
    00060
    00061    while(size > 0)
    00062    {
    00063
    00064   //   cout << "Bytes left: " << size << " SomeData " << someData << " \n" << flush;
    00065
    00066      if (sizeOfDataToRead == 0)
    00067      {
    00068        // Get size of Data
    00069        // cout << "   - Stream start \n " ;
    00070        memcpy(&sizeOfDataToRead,someData,sizeof(int));
    00071
    00072        // Todo : Unwahrscheinlichen Fall das die Size auf 2 Packete verteilt
    00073        // ankommt abfangen
    00074        ASSERT(size>3);
    00075
    00076        // cout << "    Size = " << sizeOfDataToRead << "\n" ;
    00077        (char*)someData+=sizeof(int);
    00078        size-=sizeof(int);
    00079
    00080        // Get the buffer to store the data into
    00081        if (sizeOfDataToRead > 0)
    00082        {
    00083           receiveBuffer=receiver->getPackage(sizeOfDataToRead);
    00084        }
    00085       /* Heartbeat or other data received received  */
    00086      /*if(sendingDone) sender->sendDone(); */
    00087      }
    00088      else
    00089      {
    00090         // Get Data
    00091         if (sizeOfDataToRead > 0 && size != 0)
    00092         {
    00093           int bytesToRead = sizeOfDataToRead;
    00094           if (bytesToRead > size) bytesToRead = size;
    00095
    00096            // cout << "   - need " << sizeOfDataToRead << " more bytes "
    00097            //     << "- Getting " << bytesToRead << " bytes \n" << flush;
    00098
    00099           memcpy(receiveBuffer,someData,bytesToRead);
    00100
    00101           sizeOfDataToRead -= bytesToRead;
    00102           (char*)receiveBuffer += bytesToRead; // Position to copy next package
    00103
    00104           size -= bytesToRead;
    00105           (char*)someData += bytesToRead;       // Position to read next data
    00106
    00107         }
    00108               // All Data copied ?
    00109         if (sizeOfDataToRead == 0)
    00110         {
    00111            // cout << "   - All Data collected : Streaming - \n";
    00112            receiver->onReceive();
    00113         }
    00114      }
    00115   }
    00116 }
    00117
    00118 void TCPHandler::onConnect(void)
    00119 {
    00120   cout << "TCPHandler::Connection established \n" << flush;
    00121   sizeOfDataToRead = 0;
    00122   sendingDone = true;
    00123   sender->sendDone();
    00124 }
    00125
    00126 void TCPHandler::onClose(int reason)
    00127 {
    00128     cout << "TCPHandler::Connection closed \n" << flush;
    00129
    00130 }
    00131
    00132 void TCPHandler::onSendingDone(void)
    00133 {
    00134    sendingDone=true;
    00135    sender->sendDone();
    00136 }
    00137
    00138 void TCPHandler::sendPackage(int senderNumber, void* someData,int size)
    00139 {
    00140   // cout << "TCPHandler::sendPackage : size " << size << "\n" << flush;
    00141   sendingDone=false;
    00142   if(status==connected)
    00143   {
    00144     send(someData,size);  // Calls send from TCPEndpoint
    00145   }
    00146 }
    00147