Exemplo n.º 1
0
func (cli *Client) ReadOne() bool {
	generic, _, err := Unpack(cli.conn, cli.Framed)
	host := cli.Host
	ret := true

	if err == io.EOF {
		log.Printf("%s: EOF", cli)
		ret = false
	} else if err != nil {
		log.Printf("%s: unpack error: %s", host, err)

		if err.Error() == "use of closed network connection" {
			ret = false
		}
	} else if response := jsonw.NewWrapper(generic); response == nil {
		log.Printf("%s: unexpected JSON failure")
	} else if p, e := response.AtIndex(0).GetInt(); e != nil {
		log.Printf("%s: error decoding prefix: %s", host, e)
	} else if byte(p) != rpc_response {
		log.Printf("%s: didn't get rpc_response", host)
	} else if id, e := response.AtIndex(1).GetInt64(); e != nil {
		log.Printf("%s: no msgid found: %s", host, e)
	} else if output, present := cli.outputChannels[id]; !present {
		log.Printf("%s: no output channel found for msgid %d", host, id)
	} else {
		var rp ReplyPair
		if !response.AtIndex(2).IsNil() {
			s, e := response.AtIndex(2).GetString()
			if e != nil {
				rp.err = e
				s = e.Error()
			} else {
				rp.err = Error{s}
			}
			log.Printf("%s: error: %s", host, s)
		} else if reply, e := response.AtIndex(3).GetData(); e != nil {
			rp.err = e
			log.Printf("%s: invalid reply sent: %s", host, e)
		} else {
			rp.data = jsonw.NewWrapper(reply)
		}
		output <- rp
		delete(cli.outputChannels, id)
	}
	return ret
}
Exemplo n.º 2
0
func (sc *ServerConn) serve() {

	sc.srv.Logger.Printf(LOG_CONNECTIONS,
		"connection established: remote=%s\n", sc.conn.RemoteAddr())

	go sc.sendResults()

	gogo := true
	for gogo {
		rpc, _, err := Unpack(sc.conn, sc.srv.framed)
		if err != nil {
			gogo = false
		} else {
			go sc.srv.processRpc(jsonw.NewWrapper(rpc), sc)
		}
	}
	sc.quit <- true
}
Exemplo n.º 3
0
func (pr *PackReader) unpack() (interface{}, int, error) {

	frame := 0
	frame_len := 0
	var b uint8
	var err error = nil

	var iRes interface{} = nil

	var framed bool = pr.expectFraming()

	// Threaded implementation won't need to worry, so we can
	// just throw the framing away....
	if framed {
		pr.first = false
		var tmp interface{}
		tmp, frame_len, err = pr.unpack()
		if err == nil {
			w := jsonw.NewWrapper(tmp)
			frame, err = w.GetInt()
			if err == nil {
				// Reset the offset so that we start counting from the
				// inside of the frame
				pr.offset = 0
			}
		}
	}

	if err == nil {
		b, err = pr.ReadUint8()
	}

	doswitch := false

	if err == nil {

		if b <= positive_fix_max {
			iRes = b
		} else if b >= negative_fix_min && b <= negative_fix_max {
			iRes = int8(b)

		} else if b >= type_fix_raw && b <= type_fix_raw_max {
			iRes, err = pr.unpackRaw(uint32(b & fix_raw_count_mask))

		} else if b >= type_fix_array_min && b <= type_fix_array_max {
			iRes, err = pr.unpackArray(uint32(b & fix_array_count_mask))

		} else if b >= type_fix_map_min && b <= type_fix_map_max {
			iRes, err = pr.unpackMap(uint32(b & fix_map_count_mask))

		} else {
			doswitch = true
		}
	}

	if doswitch && err == nil {

		switch b {
		case type_nil:
		case type_false:
			iRes = false
		case type_true:
			iRes = true
		case type_uint8:
			iRes, err = pr.ReadUint8()
		case type_uint16:
			iRes, err = pr.ReadUint16()
		case type_uint32:
			iRes, err = pr.ReadUint32()
		case type_uint64:
			iRes, err = pr.ReadUint64()
		case type_int16:
			iRes, err = pr.ReadInt16()
		case type_int32:
			iRes, err = pr.ReadInt32()
		case type_int64:
			iRes, err = pr.ReadInt64()
		case type_float:
			iRes, err = pr.ReadFloat32()
		case type_double:
			iRes, err = pr.ReadFloat64()
		case type_raw16:
			var length uint16
			length, err = pr.ReadUint16()
			if err == nil {
				iRes, err = pr.unpackRaw(uint32(length))
			}
		case type_raw32:
			var length uint32
			length, err = pr.ReadUint32()
			if err == nil {
				iRes, err = pr.unpackRaw(length)
			}
		case type_array16:
			var length uint16
			length, err := pr.ReadUint16()
			if err == nil {
				iRes, err = pr.unpackArray(uint32(length))
			}
		case type_array32:
			var length uint32
			length, err = pr.ReadUint32()
			if err == nil {
				iRes, err = pr.unpackArray(length)
			}
		case type_map16:
			var length uint16
			length, err = pr.ReadUint16()
			if err == nil {
				iRes, err = pr.unpackMap(uint32(length))
			}
		case type_map32:
			var length uint32
			length, err = pr.ReadUint32()
			if err == nil {
				iRes, err = pr.unpackMap(length)
			}
		default:
			log.Printf("unhandled type prefix: %x\n", b)
		}
	}

	numRead := pr.offset

	if framed && err == nil && numRead != frame {
		log.Printf("bad frame value: %d v %d\n", numRead, frame)
	}

	return iRes, numRead + frame_len, err
}