예제 #1
0
파일: pool.go 프로젝트: skycoin/skycoin
// Converts a client's connection buffer to byte messages
// Keep extracting message events until we dont have enough bytes to read in
func (self *ConnectionPool) processConnectionBuffer(c *Connection) {
	for c.Buffer.Len() >= messageLengthSize {
		//logger.Debug("There is data in the buffer, extracting")
		prefix := c.Buffer.Bytes()[:messageLengthSize]
		// decode message length
		tmpLength := uint32(0)
		encoder.DeserializeAtomic(prefix[0:4], &tmpLength)
		length := int(tmpLength)

		channel := uint16(0)
		encoder.DeserializeAtomic(prefix[4:6], &channel)

		logger.Debug("Extracting message: addr= %s, len(msg)=%d bytes", c.Addr(), length)
		// Disconnect if we received an invalid length.
		if length < messagePrefixLength ||
			length > self.Config.MaxMessageLength {
			logger.Debug("Invalid message length %d received from %s; message size mismatch",
				length, c.Addr())
			self.Disconnect(c, DisconnectInvalidMessageLength)
			break
		}

		if c.Buffer.Len()-messageLengthSize < length {
			logger.Debug("Skipping, not enough data to read this")
			break
		}

		c.Buffer.Next(messageLengthSize) // strip the length prefix
		data := c.Buffer.Next(length)    // read the message contents

		//logger.Debug("Telling the message unpacker about this message")
		c.LastReceived = Now()
		//err, dc := self.receiveMessage(c, channel, data)

		err := self.Config.MessageCallback(c, channel, data)

		if err != nil {
			logger.Debug("Error with the event: %v", err)
			self.Disconnect(c, DisconnectMalformedMessage)
			break
		}

	}
}
예제 #2
0
파일: pool.go 프로젝트: skycoin/skycoin
// Converts a client's connection buffer to byte messages
func (self *ConnectionPool) processConnectionBuffer(c *Connection) {
	for c.Buffer.Len() > messageLengthSize {
		//logger.Debug("There is data in the buffer, extracting")
		prefix := c.Buffer.Bytes()[:messageLengthSize]
		// decode message length
		tmpLength := uint32(0)
		encoder.DeserializeAtomic(prefix, &tmpLength)
		length := int(tmpLength)
		//logger.Debug("Length is %d", length)
		// Disconnect if we received an invalid length.
		if length < messagePrefixLength ||
			length > self.Config.MaxMessageLength {
			logger.Debug("Invalid message length %d received from %s",
				length, c.Addr())
			self.Disconnect(c, DisconnectInvalidMessageLength)
			break
		}

		if c.Buffer.Len()-messageLengthSize < length {
			//logger.Debug("Skipping, not enough data to read this")
			break
		}

		c.Buffer.Next(messageLengthSize) // strip the length prefix
		data := c.Buffer.Next(length)    // read the message contents

		//logger.Debug("Telling the message unpacker about this message")
		err, dc := self.receiveMessage(c, data)
		if err != nil {
			logger.Debug("Error with the event: %v", err)
			self.Disconnect(c, DisconnectMalformedMessage)
			break
		}
		if dc != nil {
			// The handler disconnected the connection, stop processing
			break
		}
	}
}