Ejemplo n.º 1
0
func (self *serverConn) SendMessage(msg *rpc.Message, id string, extra map[string]string, tryDigest bool) error {
	if msg == nil {
		cmd := &proto.Command{
			Type: proto.CMD_EMPTY,
		}
		if len(id) > 0 {
			cmd.Params = []string{id}
		}
		return self.cmdio.WriteCommand(cmd, false)
	}
	sz := msg.Size()
	if tryDigest && self.shouldDigest(sz) {
		container := &rpc.MessageContainer{
			Id:      id,
			Message: msg,
		}
		return self.writeDigest(container, extra, sz)
	}
	cmd := &proto.Command{
		Type:    proto.CMD_DATA,
		Message: msg,
	}
	cmd.Params = []string{id}
	return self.cmdio.WriteCommand(cmd, self.shouldCompress(sz))
}
Ejemplo n.º 2
0
func randomMessage() *rpc.Message {
	msg := new(rpc.Message)
	msg.Body = make([]byte, 10)
	io.ReadFull(rand.Reader, msg.Body)
	msg.Header = make(map[string]string, 2)
	msg.Header["aaa"] = "hello"
	msg.Header["aa"] = "hell"
	return msg
}
Ejemplo n.º 3
0
func (self *clientConn) SendMessageToServer(msg *rpc.Message) error {
	compress := self.shouldCompress(msg.Size())

	cmd := &proto.Command{}
	cmd.Message = msg
	cmd.Type = proto.CMD_DATA
	err := self.cmdio.WriteCommand(cmd, compress)
	return err
}
Ejemplo n.º 4
0
func UnmarshalCommand(data []byte) (cmd *Command, err error) {
	if len(data) < 4 {
		return
	}
	cmd = new(Command)
	cmd.Type = data[0]
	nrParams := int(data[1] >> 4)
	nrHeaders := int((uint16(data[2]) << 8) | (uint16(data[3])))

	data = data[4:]
	if nrParams > 0 {
		cmd.Params = make([]string, nrParams)
		var str []byte
		for i := 0; i < nrParams; i++ {
			str, data, err = cutString(data)
			if err != nil {
				return
			}
			cmd.Params[i] = string(str)
		}
	}
	var msg *rpc.Message
	msg = nil
	if nrHeaders > 0 {
		msg = new(rpc.Message)
		msg.Header = make(map[string]string, nrHeaders)
		var key []byte
		var value []byte
		for i := 0; i < nrHeaders; i++ {
			key, data, err = cutString(data)
			if err != nil {
				return
			}
			value, data, err = cutString(data)
			if err != nil {
				return
			}
			msg.Header[string(key)] = string(value)
		}
	}
	if len(data) > 0 {
		if msg == nil {
			msg = new(rpc.Message)
		}
		msg.Body = data
	}
	if msg != nil {
		cmd.Message = msg
	}
	return
}
Ejemplo n.º 5
0
func (self *clientConn) SendMessageToUsers(msg *rpc.Message, ttl time.Duration, service string, receiver ...string) error {
	if len(receiver) == 0 {
		return nil
	}
	cmd := &proto.Command{}
	cmd.Type = proto.CMD_FWD_REQ
	cmd.Params = make([]string, 2, 3)
	cmd.Params[0] = fmt.Sprintf("%v", ttl)
	cmd.Params[1] = strings.Join(receiver, ",")
	if len(service) > 0 && service != self.Service() {
		cmd.Params = append(cmd.Params, service)
	}
	cmd.Message = msg
	compress := self.shouldCompress(msg.Size())
	return self.cmdio.WriteCommand(cmd, compress)
}
Ejemplo n.º 6
0
func (self *serverConn) ForwardMessage(sender, senderService string, msg *rpc.Message, id string, tryDigest bool) error {
	sz := msg.Size()
	if sz == 0 {
		return nil
	}
	if tryDigest && self.shouldDigest(sz) {
		container := &rpc.MessageContainer{
			Id:            id,
			Sender:        sender,
			SenderService: senderService,
			Message:       msg,
		}
		return self.writeDigest(container, nil, sz)
	}
	cmd := &proto.Command{
		Type:    proto.CMD_FWD,
		Message: msg,
	}
	cmd.Params = []string{sender, senderService, id}
	return self.cmdio.WriteCommand(cmd, self.shouldCompress(sz))
}
Ejemplo n.º 7
0
func randomMessage(maxNrHeader, maxBodyLen, maxStrLen int) *rpc.Message {
	nrHeaders := weakrand.Intn(maxNrHeaders)
	bodyLen := weakrand.Intn(maxBodyLen)
	if nrHeaders <= 0 && bodyLen <= 0 {
		return nil
	}

	msg := new(rpc.Message)
	if bodyLen > 0 {
		msg.Body = randomBytes(maxBodyLen)
	}
	if nrHeaders <= 0 {
		return msg
	}

	msg.Header = make(map[string]string, nrHeaders)

	for i := 0; i < nrHeaders; i++ {
		msg.Header[string(randomBytes(maxStrLen))] = string(randomBytes(maxStrLen))
	}
	return msg
}