示例#1
0
func RunStdioSerializer(stdio_serializer *mesh.Serializer,
	cmd_stdoutQueue chan interface{},
	cmd_stdout io.Reader,
	cmd_stdinQueue chan interface{},
	cmd_stdin io.Writer) {

	go func() {
		for {
			// Output
			to_stdout := <-cmd_stdinQueue
			b := stdio_serializer.SerializeMessage(to_stdout)
			length := (uint32)(len(b))
			bl := encoder.SerializeAtomic(length)
			cmd_stdin.Write(bl)
			cmd_stdin.Write(b)
		}
	}()
	for {
		// Input
		var bl []byte = make([]byte, 4)
		n_l, err_l := cmd_stdout.Read(bl)
		if n_l != 4 {
			if err_l == io.EOF {
				break
			}
			if err_l != nil {
				if err_l == io.EOF {
				}
				fmt.Fprintf(os.Stderr, "Error reading message length: %v\n", err_l)
			}
			continue
		}
		var length uint32
		encoder.DecodeInt(bl, &length)
		var bb []byte = make([]byte, length)
		n_bb, err_bb := cmd_stdout.Read(bb)
		if uint32(n_bb) != length {
			if err_l == io.EOF {
				break
			}
			if err_bb != nil {
				fmt.Fprintf(os.Stderr, "Error reading message length: %v\n", os.Getpid(), err_bb)
			}
			continue
		}
		message, error := stdio_serializer.UnserializeMessage(bb)
		if error == nil {
			cmd_stdoutQueue <- message
		} else {
			panic(fmt.Sprintf("Error unserializing message from stdin: %v\n", error))
		}

		time.Sleep(time.Millisecond)
	}
}
示例#2
0
	n, e = encoder.DeserializeRawToValue(msg, v)
	return
}

// Packgs a Message into []byte containing length, id and data
var encodeMessage = func(msg Message) []byte {
	t := reflect.ValueOf(msg).Elem().Type()
	msgId, succ := MessageIdMap[t]
	if !succ {
		txt := "Attempted to serialize message struct not in MessageIdMap: %v"
		log.Panicf(txt, msg)
	}
	bMsg := encoder.Serialize(msg)

	// message length
	bLen := encoder.SerializeAtomic(uint32(len(bMsg) + len(msgId)))
	m := make([]byte, 0)
	m = append(m, bLen...)     // length prefix
	m = append(m, msgId[:]...) // message id
	m = append(m, bMsg...)     // message bytes
	return m
}

// Sends []byte over a net.Conn
var sendByteMessage = func(conn net.Conn, msg []byte,
	timeout time.Duration) error {
	deadline := time.Time{}
	if timeout != 0 {
		deadline = time.Now().Add(timeout)
	}
	if err := conn.SetWriteDeadline(deadline); err != nil {