Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

RemoteEvents.cc

Go to the documentation of this file.
00001 #include "Events/RemoteEvents.h"
00002 #include "Events/EventRouter.h"
00003 #include "Shared/string_util.h"
00004 
00005 RemoteEvents::RemoteEvents() : sck(NULL),
00006                                sizeLeft(0), vecbuf(), bufType(Invalid) {
00007 
00008 }
00009 
00010 RemoteEvents::~RemoteEvents() {
00011 
00012 }
00013 
00014 std::string RemoteEvents::remoteIPString() {
00015   return string_util::intToStringIP(remoteIPInt());
00016 }
00017 
00018 int RemoteEvents::remoteIPInt() {
00019   return sck->getPeerAddress();
00020 }
00021 
00022 //Receiving data-------------------------------------------
00023 
00024 /* Reads in the buffer type header */
00025 bool RemoteEvents::readType(char* &data, int &bytes) {
00026   //printf("Got pointer: %x, %d bytes\n", data, bytes);
00027     if ((unsigned)bytes < sizeof(BufferType))
00028         return false;
00029 
00030     bufType = *(BufferType *)data;
00031     data += sizeof(BufferType);
00032     bytes -= sizeof(BufferType);
00033     return true;
00034 }
00035 
00036 /* Reads in a size header from the data pointer. */
00037 bool  RemoteEvents::readSize(char* &data, int &bytes) {
00038     //Return an error if there's not enough data there
00039     if ((unsigned)bytes < sizeof(int))
00040         return false;
00041 
00042     //Reset the buffer
00043     vecbuf.clear();
00044 
00045     //Read the size and increment/decrement things as appropriate
00046     sizeLeft = *(int *)data;
00047     data += sizeof(int);
00048     bytes -= sizeof(int);
00049     return true;
00050 }
00051 
00052 /* Reads in data from the given pointer until the target size is
00053  * reached, or bytes becomes zero. Return true if the whole desired
00054  * chunk was read, false otherwise. */
00055 bool RemoteEvents::readData(char* &data, int &bytes) {
00056     while (bytes) {
00057         //If sizeLeft is zero it's done reading the data
00058         if (!sizeLeft)
00059             return true;
00060         
00061         //Read a byte
00062         vecbuf.push_back(*data++);
00063         bytes--;
00064         sizeLeft--;
00065     }
00066     //Return whether or not the whole chunk was read
00067     return !sizeLeft;
00068 }
00069 //------------------------------------------------------
00070 
00071 bool RemoteEvents::isConnected() {
00072   return sck==NULL ? false : wireless->isConnected(sck->sock);
00073 }
00074 
00075 std::ostream& operator<<(std::ostream &os, const RemoteRequest &req) {
00076   os << "RemoteRequest[type=" << req.type
00077      << ",elts=" << req.numElements
00078      << ",egid=" << req.egid
00079      << ",sid=" << req.sid
00080      << ",etid=" << req.etid
00081      << ",sType=" << req.sType
00082      << ",interval=" << req.interval
00083      << "]";
00084   return os;
00085 }

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:50 2016 by Doxygen 1.6.3