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)

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 43 of file AjpBaseSocket.h.


Constructor & Destructor Documentation

AjpBaseSocket::AjpBaseSocket ( ISocketHandler h  ) 

Definition at line 127 of file AjpBaseSocket.cpp.

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


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 136 of file AjpBaseSocket.cpp.

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

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

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 190 of file AjpBaseSocket.cpp.

Referenced by Ajp13Socket::ReceiveForwardRequest().

00191 {
00192         return (unsigned char)buf[ptr++];
00193 }

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

Definition at line 197 of file AjpBaseSocket.cpp.

Referenced by Ajp13Socket::ReceiveForwardRequest().

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

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

Definition at line 204 of file AjpBaseSocket.cpp.

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

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

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

Definition at line 214 of file AjpBaseSocket.cpp.

References get_integer().

Referenced by Ajp13Socket::ReceiveForwardRequest().

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

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

Definition at line 230 of file AjpBaseSocket.cpp.

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

00231 {
00232         buf[ptr++] = zz;
00233 }

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

Definition at line 237 of file AjpBaseSocket.cpp.

Referenced by Ajp13Socket::OnTransferLimit().

00238 {
00239         buf[ptr++] = zz ? 1 : 0;
00240 }

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

Definition at line 244 of file AjpBaseSocket.cpp.

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

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

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

Definition at line 253 of file AjpBaseSocket.cpp.

References put_byte(), and put_integer().

Referenced by Ajp13Socket::Respond().

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


Member Data Documentation

Definition at line 78 of file AjpBaseSocket.h.

int AjpBaseSocket::m_state [private]

Definition at line 81 of file AjpBaseSocket.h.

Referenced by OnRawData().

int AjpBaseSocket::m_length [private]

Definition at line 82 of file AjpBaseSocket.h.

Referenced by OnRawData().

int AjpBaseSocket::m_ptr [private]

Definition at line 83 of file AjpBaseSocket.h.

Referenced by OnRawData().

char AjpBaseSocket::m_message[8192] [private]

Definition at line 84 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