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 
00004 RemoteEvents::RemoteEvents() : sck(NULL),
00005                                sizeLeft(0), vecbuf(), bufType(Invalid) {
00006 
00007 }
00008 
00009 RemoteEvents::~RemoteEvents() {
00010 
00011 }
00012 
00013 std::string RemoteEvents::remoteIPString() {
00014   return EventRouter::intToStringIP(remoteIPInt());
00015 }
00016 
00017 int RemoteEvents::remoteIPInt() {
00018   return sck->getPeerAddress();
00019 }
00020 
00021 //Receiving data-------------------------------------------
00022 
00023 /* Reads in the buffer type header */
00024 bool RemoteEvents::readType(char* &data, int &bytes) {
00025   //printf("Got pointer: %x, %d bytes\n", data, bytes);
00026     if ((unsigned)bytes < sizeof(BufferType))
00027         return false;
00028 
00029     bufType = *(BufferType *)data;
00030     data += sizeof(BufferType);
00031     bytes -= sizeof(BufferType);
00032     return true;
00033 }
00034 
00035 /* Reads in a size header from the data pointer. */
00036 bool  RemoteEvents::readSize(char* &data, int &bytes) {
00037     //Return an error if there's not enough data there
00038     if ((unsigned)bytes < sizeof(int))
00039         return false;
00040 
00041     //Reset the buffer
00042     vecbuf.clear();
00043 
00044     //Read the size and increment/decrement things as appropriate
00045     sizeLeft = *(int *)data;
00046     data += sizeof(int);
00047     bytes -= sizeof(int);
00048     return true;
00049 }
00050 
00051 /* Reads in data from the given pointer until the target size is
00052  * reached, or bytes becomes zero. Return true if the whole desired
00053  * chunk was read, false otherwise. */
00054 bool RemoteEvents::readData(char* &data, int &bytes) {
00055     while (bytes) {
00056         //If sizeLeft is zero it's done reading the data
00057         if (!sizeLeft)
00058             return true;
00059         
00060         //Read a byte
00061         vecbuf.push_back(*data++);
00062         bytes--;
00063         sizeLeft--;
00064     }
00065     //Return whether or not the whole chunk was read
00066     return !sizeLeft;
00067 }
00068 //------------------------------------------------------
00069 
00070 bool RemoteEvents::isConnected() {
00071     return wireless->isConnected(sck->sock);
00072 }

Tekkotsu v4.0
Generated Thu Nov 22 00:54:55 2007 by Doxygen 1.5.4