Changeset 622
- Timestamp:
- 03/09/07 22:17:36 (18 months ago)
- Location:
- branches/newnet/museekd/museekd
- Files:
-
- 2 modified
-
messageprocessor.cpp (modified) (2 diffs)
-
messageprocessor.h (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/newnet/museekd/museekd/messageprocessor.cpp
r611 r622 24 24 Museek::MessageProcessor::parseMessage(NewNet::ClientSocket * socket) 25 25 { 26 /* How many bytes do we have in store? */ 26 27 size_t count = socket->receiveBuffer().count(); 28 /* Set up an easy-access pointer to the buffer. */ 27 29 uchar * inbuf = socket->receiveBuffer().data(); 28 30 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. */ 29 34 if(count < (4 + m_CodeSize)) 30 35 return false; 31 36 37 /* Unpack the message length. 32bit little endian, that's the slsk way. */ 32 38 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 */ 33 41 uint32 mtype = 0; 34 42 for(uint j = 0; j < m_CodeSize; ++j) 35 43 mtype += inbuf[4 + j] << (j * 8); 36 44 45 /* 'len' includes the message type, so if we have less than len + 4 bytes in 46 the buffer, bail out. */ 37 47 if(count < (len + 4)) 38 48 return false; 39 49 50 /* A complete message is here. Set up the message data structure and emit 51 messageReceivedEvent. */ 40 52 struct MessageData messageData; 41 53 messageData.socket = socket; … … 45 57 messageReceivedEvent(&messageData); 46 58 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. */ 47 62 if(socket->receiveBuffer().empty()) 48 63 return false; 49 64 65 /* Seek to the end of the message. */ 50 66 socket->receiveBuffer().seek(len + 4); 51 67 68 /* Is the buffer empty? Stop processing. If it's not, keep processing. */ 52 69 return ! socket->receiveBuffer().empty(); 53 70 } -
branches/newnet/museekd/museekd/messageprocessor.h
r611 r622 24 24 #include <NewNet/nnevent.h> 25 25 26 /* Forward declarations. */ 26 27 namespace NewNet 27 28 { … … 31 32 namespace Museek 32 33 { 34 /* MessageProcessor is a mix-in class that provides the means to process 35 soulseek message packets coming in on a socket. */ 33 36 class MessageProcessor 34 37 { 35 38 public: 39 /* Constructor. codeSize defines how wide the messageCode parameter will 40 be. 1 is used for handshake socket, other types use 4. */ 36 41 MessageProcessor(uint codeSize) : m_CodeSize(codeSize) 37 42 { 38 43 } 39 44 45 /* Data that's provided when messageReceivedEvent is emitted. */ 40 46 struct MessageData 41 47 { 48 /* Pointer to the socket that received the message. */ 42 49 NewNet::ClientSocket * socket; 50 /* Message type / message code. Identifies what kind of message this 51 is. */ 43 52 uint32 type; 53 /* Length of the data body. */ 44 54 uint32 length; 55 /* Pointer to the data that's part of the message. */ 45 56 const unsigned char * data; 46 57 }; 47 58 59 /* Emitted when a complete message has been received and parsed. */ 48 60 NewNet::Event<const MessageData *> messageReceivedEvent; 49 61 62 /* Data received event handler. Connect a client socket's 63 dataReceivedEvent to this. */ 50 64 void onDataReceived(NewNet::ClientSocket * socket) 51 65 { 66 /* Keep parsing messages until we run out of data. */ 52 67 while(parseMessage(socket)) 53 68 { … … 56 71 57 72 private: 73 /* Size of the message type, see constructor comment for more info. */ 58 74 uint m_CodeSize; 75 /* Try to parse a complete message from the socket's receive buffer. */ 59 76 bool parseMessage(NewNet::ClientSocket * socket); 60 77 };
