예제 #1
0
파일: chat.go 프로젝트: Yawning/ricochet
func (ch *chatChan) validatePacket(chatPkt *packet.ChatPacket) error {
	if ch.isOutgoing && chatPkt.GetChatMessage() != nil {
		return fmt.Errorf("received chat message on outgoing channel")
	}
	if !ch.isOutgoing && chatPkt.GetChatAcknowledge() != nil {
		return fmt.Errorf("received chat ack message on inbound channel")
	}
	return nil
}
예제 #2
0
파일: chat.go 프로젝트: Yawning/ricochet
func (ch *chatChan) onPacket(rawPkt []byte) (err error) {
	log := ch.conn.endpoint.log
	var chatPkt packet.ChatPacket
	if err = proto.Unmarshal(rawPkt, &chatPkt); err != nil {
		return
	}
	if err = ch.validatePacket(&chatPkt); err != nil {
		return
	}

	if chatMsg := chatPkt.GetChatMessage(); chatMsg != nil {
		accepted := true
		msgText := chatMsg.GetMessageText()
		deltaT := chatMsg.GetTimeDelta()
		msgID := chatMsg.GetMessageId()

		if len(msgText) > MessageMaxCharacters {
			log.Printf("[%v]: Oversized chat message, rejecting")
			accepted = false
		}

		// Deliver the chat message to the caller.
		if accepted {
			log.Printf("[%v]: %v:%v '%v'", ch.conn.hostname, msgID, deltaT, msgText)
			ch.conn.endpoint.onMessageReceived(ch.conn.hostname, msgText)
		}

		// Ack the packet.
		if chatMsg.MessageId != nil {
			err = ch.sendChatAck(chatMsg.GetMessageId(), accepted)
		}
	}
	if chatAck := chatPkt.GetChatAcknowledge(); chatAck != nil {
		// XXX: Handle the ACK somehow.
		log.Printf("[%v]: ACK: %d Accepted: %v", ch.conn.hostname, chatAck.GetMessageId(), chatAck.GetAccepted())
	}

	return nil
}