Changeset 622

Show
Ignore:
Timestamp:
03/09/07 22:17:36 (18 months ago)
Author:
hyriand
Message:

Documented messageprocessor.h/cpp

Location:
branches/newnet/museekd/museekd
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • branches/newnet/museekd/museekd/messageprocessor.cpp

    r611 r622  
    2424Museek::MessageProcessor::parseMessage(NewNet::ClientSocket * socket) 
    2525{ 
     26  /* How many bytes do we have in store? */ 
    2627  size_t count = socket->receiveBuffer().count(); 
     28  /* Set up an easy-access pointer to the buffer. */ 
    2729  uchar * inbuf = socket->receiveBuffer().data(); 
    2830 
     31  /* If we have less than 4 + m_CodeSize bytes, bail out: 
     32     4 bytes for the message length 
     33     m_CodeSize bytes for the message type. */ 
    2934  if(count < (4 + m_CodeSize)) 
    3035    return false; 
    3136 
     37  /* Unpack the message length. 32bit little endian, that's the slsk way. */ 
    3238  uint32 len = inbuf[0] + (inbuf[1] << 8) + (inbuf[2] << 16) + (inbuf[3] << 24); 
     39 
     40  /* Unpack the message type. 8 or 32bit little endian depending on m_CodeSize */ 
    3341  uint32 mtype = 0; 
    3442  for(uint j = 0; j < m_CodeSize; ++j) 
    3543    mtype += inbuf[4 + j] << (j * 8); 
    3644 
     45  /* 'len' includes the message type, so if we have less than len + 4 bytes in 
     46     the buffer, bail out. */ 
    3747  if(count < (len + 4)) 
    3848    return false; 
    3949 
     50  /* A complete message is here. Set up the message data structure and emit 
     51     messageReceivedEvent. */ 
    4052  struct MessageData messageData; 
    4153  messageData.socket = socket; 
     
    4557  messageReceivedEvent(&messageData); 
    4658 
     59  /* This happens if the socket descriptor is transferred to another socket 
     60     instance. Bail out and terminate all processing, somebody else owns 
     61     this buffer now. */ 
    4762  if(socket->receiveBuffer().empty()) 
    4863    return false; 
    4964 
     65  /* Seek to the end of the message. */ 
    5066  socket->receiveBuffer().seek(len + 4); 
    5167 
     68  /* Is the buffer empty? Stop processing. If it's not, keep processing. */ 
    5269  return ! socket->receiveBuffer().empty(); 
    5370} 
  • branches/newnet/museekd/museekd/messageprocessor.h

    r611 r622  
    2424#include <NewNet/nnevent.h> 
    2525 
     26/* Forward declarations. */ 
    2627namespace NewNet 
    2728{ 
     
    3132namespace Museek 
    3233{ 
     34  /* MessageProcessor is a mix-in class that provides the means to process 
     35     soulseek message packets coming in on a socket. */ 
    3336  class MessageProcessor 
    3437  { 
    3538  public: 
     39    /* Constructor. codeSize defines how wide the messageCode parameter will 
     40       be. 1 is used for handshake socket, other types use 4. */ 
    3641    MessageProcessor(uint codeSize) : m_CodeSize(codeSize) 
    3742    { 
    3843    } 
    3944 
     45    /* Data that's provided when messageReceivedEvent is emitted. */ 
    4046    struct MessageData 
    4147    { 
     48      /* Pointer to the socket that received the message. */ 
    4249      NewNet::ClientSocket * socket; 
     50      /* Message type / message code. Identifies what kind of message this 
     51         is. */ 
    4352      uint32 type; 
     53      /* Length of the data body. */ 
    4454      uint32 length; 
     55      /* Pointer to the data that's part of the message. */ 
    4556      const unsigned char * data; 
    4657    }; 
    4758 
     59    /* Emitted when a complete message has been received and parsed. */ 
    4860    NewNet::Event<const MessageData *> messageReceivedEvent; 
    4961 
     62    /* Data received event handler. Connect a client socket's 
     63       dataReceivedEvent to this. */ 
    5064    void onDataReceived(NewNet::ClientSocket * socket) 
    5165    { 
     66      /* Keep parsing messages until we run out of data. */ 
    5267      while(parseMessage(socket)) 
    5368      { 
     
    5671 
    5772  private: 
     73    /* Size of the message type, see constructor comment for more info. */ 
    5874    uint m_CodeSize; 
     75    /* Try to parse a complete message from the socket's receive buffer. */ 
    5976    bool parseMessage(NewNet::ClientSocket * socket); 
    6077  };