Logo
~Sockets~
~Examples~
~Contact~


AjpBaseSocket Class Reference

#include <AjpBaseSocket.h>

Inheritance diagram for AjpBaseSocket:
Collaboration diagram for AjpBaseSocket:

List of all members.


Public Member Functions

 AjpBaseSocket (ISocketHandler &h)
void OnRawData (const char *buf, size_t sz)
 This callback is executed after a successful read from the socket.
virtual void OnHeader (short id, short len)=0
virtual void OnPacket (const char *buf, size_t sz)=0

Protected Member Functions

unsigned char get_byte (const char *buf, int &ptr)
bool get_boolean (const char *buf, int &ptr)
short get_integer (const char *buf, int &ptr)
std::string get_string (const char *buf, int &ptr)
void put_byte (char *buf, int &ptr, unsigned char zz)
void put_boolean (char *buf, int &ptr, bool zz)
void put_integer (char *buf, int &ptr, short zz)
void put_string (char *buf, int &ptr, const std::string &zz)
void reset ()
 Reset to original state.

Static Protected Attributes

static Initializer Init

Private Attributes

int m_state
int m_length
int m_ptr
char m_message [8192]

Classes

class  Initializer

Detailed Description

Definition at line 45 of file AjpBaseSocket.h.


Constructor & Destructor Documentation

AjpBaseSocket::AjpBaseSocket ( ISocketHandler h  ) 

Definition at line 129 of file AjpBaseSocket.cpp.

00129                                               : TcpSocket(h)
00130 , m_state(0)
00131 , m_length(4)
00132 , m_ptr(0)
00133 {
00134 }


Member Function Documentation

void AjpBaseSocket::OnRawData ( const char *  buf,
size_t  len 
) [virtual]

This callback is executed after a successful read from the socket.

Parameters:
buf Pointer to the data
len Length of the data

Reimplemented from TcpSocket.

Definition at line 138 of file AjpBaseSocket.cpp.

References DEB, get_integer(), m_length, m_message, m_ptr, m_state, OnHeader(), and OnPacket().

00139 {
00140 DEB(fprintf(stderr, "OnRawData: %d bytes\n", sz);)
00141         size_t ptr = 0;
00142         while (true)
00143         {
00144                 size_t left = sz - ptr;
00145 DEB(fprintf(stderr, " left: %d bytes\n", left);
00146 fprintf(stderr, " state: %d\n", m_state);)
00147                 switch (m_state)
00148                 {
00149                 case 0:
00150                         {
00151                                 size_t missing = m_length - m_ptr;
00152                                 short len = (short)(missing < left ? missing : left);
00153                                 memcpy(m_message + m_ptr, buf + ptr, len);
00154                                 m_ptr += len;
00155                                 ptr += len;
00156                                 if (m_ptr < m_length)
00157                                 {
00158                                         return; // read more
00159                                 }
00160                                 int p = 0;
00161                                 short id = get_integer(m_message, p);
00162                                 short length = get_integer(m_message, p);
00163                                 OnHeader(id, length);
00164                                 m_state = 1;
00165                                 m_length = length;
00166                                 m_ptr = 0; // bytes in m_message
00167                         }
00168                         break;
00169                 case 1:
00170                         {
00171                                 size_t missing = m_length - m_ptr;
00172                                 short len = (short)(missing < left ? missing : left);
00173                                 memcpy(m_message + m_ptr, buf + ptr, len);
00174                                 m_ptr += len;
00175                                 ptr += len;
00176                                 if (m_ptr < m_length)
00177                                 {
00178                                         return; // read more
00179                                 }
00180                                 OnPacket(m_message, m_ptr);
00181                                 m_state = 0;
00182                                 m_length = 4;
00183                                 m_ptr = 0;
00184                         }
00185                         break;
00186                 }
00187         }
00188 }

virtual void AjpBaseSocket::OnHeader ( short  id,
short  len 
) [pure virtual]

Implemented in Ajp13Socket.

Referenced by OnRawData().

virtual void AjpBaseSocket::OnPacket ( const char *  buf,
size_t  sz 
) [pure virtual]

Implemented in Ajp13Socket.

Referenced by OnRawData().

unsigned char AjpBaseSocket::get_byte ( const char *  buf,
int &  ptr 
) [protected]

Definition at line 192 of file AjpBaseSocket.cpp.

Referenced by Ajp13Socket::ReceiveForwardRequest().

00193 {
00194         return (unsigned char)buf[ptr++];
00195 }

bool AjpBaseSocket::get_boolean ( const char *  buf,
int &  ptr 
) [protected]

Definition at line 199 of file AjpBaseSocket.cpp.

Referenced by Ajp13Socket::ReceiveForwardRequest().

00200 {
00201         return ( (unsigned char)buf[ptr++] & 1) == 1 ? true : false;
00202 }

short AjpBaseSocket::get_integer ( const char *  buf,
int &  ptr 
) [protected]

Definition at line 206 of file AjpBaseSocket.cpp.

Referenced by get_string(), OnRawData(), and Ajp13Socket::ReceiveForwardRequest().

00207 {
00208         short n;
00209         memcpy(&n, buf + ptr, 2);
00210         ptr += 2;
00211         return ntohs(n);
00212 }

std::string AjpBaseSocket::get_string ( const char *  buf,
int &  ptr 
) [protected]

Definition at line 216 of file AjpBaseSocket.cpp.

References get_integer().

Referenced by Ajp13Socket::ReceiveForwardRequest().

00217 {
00218         short len = get_integer(buf, ptr);
00219         if (len != -1)
00220         {
00221                 std::string tmp = buf + ptr;
00222                 ptr += len;
00223                 ptr++; // skip trailing 0x0
00224                 tmp.resize(len);
00225                 return tmp;
00226         }
00227         return "";
00228 }

void AjpBaseSocket::put_byte ( char *  buf,
int &  ptr,
unsigned char  zz 
) [protected]

void AjpBaseSocket::put_boolean ( char *  buf,
int &  ptr,
bool  zz 
) [protected]

Definition at line 239 of file AjpBaseSocket.cpp.

Referenced by Ajp13Socket::OnTransferLimit().

00240 {
00241         buf[ptr++] = zz ? 1 : 0;
00242 }

void AjpBaseSocket::put_integer ( char *  buf,
int &  ptr,
short  zz 
) [protected]

Definition at line 246 of file AjpBaseSocket.cpp.

Referenced by Ajp13Socket::IHttpServer_Respond(), Ajp13Socket::OnTransferLimit(), put_string(), and Ajp13Socket::ReceiveBody().

00247 {
00248         short tmp = htons(zz);
00249         memcpy(buf + ptr, &tmp, 2);
00250         ptr += 2;
00251 }

void AjpBaseSocket::put_string ( char *  buf,
int &  ptr,
const std::string &  zz 
) [protected]

Definition at line 255 of file AjpBaseSocket.cpp.

References put_byte(), and put_integer().

Referenced by Ajp13Socket::IHttpServer_Respond().

00256 {
00257         put_integer(buf, ptr, (short)zz.size() );
00258         memcpy(buf + ptr, zz.c_str(), zz.size());
00259         ptr += (int)zz.size();
00260         put_byte(buf, ptr, 0);
00261 }

void AjpBaseSocket::reset (  )  [protected]

Reset to original state.

Definition at line 265 of file AjpBaseSocket.cpp.

References m_length, m_ptr, and m_state.

Referenced by Ajp13Socket::ReceiveCPing(), and Ajp13Socket::Reset().

00266 {
00267         m_state = m_ptr = 0;
00268         m_length = 4;
00269 }


Member Data Documentation

Definition at line 83 of file AjpBaseSocket.h.

int AjpBaseSocket::m_state [private]

Definition at line 86 of file AjpBaseSocket.h.

Referenced by OnRawData(), and reset().

int AjpBaseSocket::m_length [private]

Definition at line 87 of file AjpBaseSocket.h.

Referenced by OnRawData(), and reset().

int AjpBaseSocket::m_ptr [private]

Definition at line 88 of file AjpBaseSocket.h.

Referenced by OnRawData(), and reset().

char AjpBaseSocket::m_message[8192] [private]

Definition at line 89 of file AjpBaseSocket.h.

Referenced by OnRawData().


The documentation for this class was generated from the following files:
Page, code, and content Copyright (C) 2007 by Anders Hedström
Generated for C++ Sockets by  doxygen 1.4.4