func (c *client) Call(ctx Context, serviceName, methodName string, req, resp thrift.TStruct) (bool, error) { call, err := c.sc.BeginCall(ctx, serviceName+"::"+methodName, &tchannel.CallOptions{Format: tchannel.Thrift}) if err != nil { return false, err } writer, err := call.Arg2Writer() if err != nil { return false, err } if err := writeHeaders(writer, ctx.Headers()); err != nil { return false, err } if err := writer.Close(); err != nil { return false, err } writer, err = call.Arg3Writer() if err != nil { return false, err } protocol := thrift.NewTBinaryProtocolTransport(&readWriterTransport{Writer: writer}) if err := req.Write(protocol); err != nil { return false, err } if err := writer.Close(); err != nil { return false, err } reader, err := call.Response().Arg2Reader() if err != nil { return false, err } headers, err := readHeaders(reader) if err != nil { return false, err } ctx.SetResponseHeaders(headers) if err := reader.Close(); err != nil { return false, err } success := !call.Response().ApplicationError() reader, err = call.Response().Arg3Reader() if err != nil { return success, err } protocol = thrift.NewTBinaryProtocolTransport(&readWriterTransport{Reader: reader}) if err := resp.Read(protocol); err != nil { return success, err } if err := reader.Close(); err != nil { return success, err } return success, nil }
func (s *Server) handle(origCtx context.Context, handler handler, method string, call *tchannel.InboundCall) error { reader, err := call.Arg2Reader() if err != nil { return err } headers, err := readHeaders(reader) if err != nil { return err } if err := reader.Close(); err != nil { return err } reader, err = call.Arg3Reader() if err != nil { return err } ctx := WithHeaders(origCtx, headers) protocol := thrift.NewTBinaryProtocolTransport(&readWriterTransport{Reader: reader}) success, resp, err := handler.server.Handle(ctx, method, protocol) if err != nil { reader.Close() call.Response().SendSystemError(err) return nil } if err := reader.Close(); err != nil { return err } if !success { call.Response().SetApplicationError() } writer, err := call.Response().Arg2Writer() if err != nil { return err } if err := writeHeaders(writer, ctx.ResponseHeaders()); err != nil { return err } if err := writer.Close(); err != nil { return err } writer, err = call.Response().Arg3Writer() protocol = thrift.NewTBinaryProtocolTransport(&readWriterTransport{Writer: writer}) resp.Write(protocol) err = writer.Close() if handler.postResponseCB != nil { handler.postResponseCB(method, resp) } return err }
func (h *scribeHandler) spans() []*zipkincore.Span { h.RLock() defer h.RUnlock() spans := []*zipkincore.Span{} for _, m := range h.entries { decoded, err := base64.StdEncoding.DecodeString(m.GetMessage()) if err != nil { h.t.Error(err) continue } buffer := thrift.NewTMemoryBuffer() if _, err := buffer.Write(decoded); err != nil { h.t.Error(err) continue } transport := thrift.NewTBinaryProtocolTransport(buffer) zs := &zipkincore.Span{} if err := zs.Read(transport); err != nil { h.t.Error(err) continue } spans = append(spans, zs) } return spans }
func kafkaSerialize(s *Span) []byte { t := thrift.NewTMemoryBuffer() p := thrift.NewTBinaryProtocolTransport(t) if err := s.Encode().Write(p); err != nil { panic(err) } return t.Buffer.Bytes() }
func scribeSerialize(s *Span) string { t := thrift.NewTMemoryBuffer() p := thrift.NewTBinaryProtocolTransport(t) if err := s.Encode().Write(p); err != nil { panic(err) } return base64.StdEncoding.EncodeToString(t.Buffer.Bytes()) }
func spanToBytes(span *zipkin.Span) ([]byte, error) { t := thrift.NewTMemoryBuffer() p := thrift.NewTBinaryProtocolTransport(t) err := span.Write(p) if err != nil { return nil, err } return t.Buffer.Bytes(), nil }
func (z *zipkin) pub(s *zipkincore.Span, pr sarama.SyncProducer) { t := thrift.NewTMemoryBufferLen(1024) p := thrift.NewTBinaryProtocolTransport(t) if err := s.Write(p); err != nil { return } m := &sarama.ProducerMessage{ Topic: z.opts.Topic, Value: sarama.ByteEncoder(t.Buffer.Bytes()), } pr.SendMessage(m) }
// SerializeThrift takes a thrift struct and returns the serialized bytes // of that struct using the thrift binary protocol. This is a temporary // measure before frames can be forwarded directly past the endpoint to the proper // destinaiton. func SerializeThrift(s athrift.TStruct) ([]byte, error) { var b []byte var buffer = bytes.NewBuffer(b) transport := athrift.NewStreamTransportW(buffer) if err := s.Write(athrift.NewTBinaryProtocolTransport(transport)); err != nil { return nil, err } if err := transport.Flush(); err != nil { return nil, err } return buffer.Bytes(), nil }
func (c *ScribeCollector) newConnection() (scribe.Scribe, error) { addr, err := net.ResolveTCPAddr("tcp", c.addr) if err != nil { return nil, err } socket := thrift.NewTSocketFromAddrTimeout(addr, timeout) transport := thrift.NewTFramedTransport(socket) if err := transport.Open(); err != nil { return nil, err } proto := thrift.NewTBinaryProtocolTransport(transport) s := scribe.NewScribeClientProtocol(transport, proto, proto) return s, nil }
func deserializeSpan(t *testing.T, e sarama.Encoder) *zipkincore.Span { bytes, err := e.Encode() if err != nil { t.Errorf("error in encoding: %v", err) } s := zipkincore.NewSpan() mb := thrift.NewTMemoryBufferLen(len(bytes)) mb.Write(bytes) mb.Flush() pt := thrift.NewTBinaryProtocolTransport(mb) err = s.Read(pt) if err != nil { t.Errorf("error in decoding: %v", err) } return s }
func scribeClientFactory(addr string, timeout time.Duration) func() (scribe.Scribe, error) { return func() (scribe.Scribe, error) { a, err := net.ResolveTCPAddr("tcp", addr) if err != nil { return nil, err } socket := thrift.NewTSocketFromAddrTimeout(a, timeout) transport := thrift.NewTFramedTransport(socket) if err := transport.Open(); err != nil { socket.Close() return nil, err } proto := thrift.NewTBinaryProtocolTransport(transport) client := scribe.NewScribeClientProtocol(transport, proto, proto) return client, nil } }
func newConnection(n *node, keyspace string, timeout time.Duration, authentication map[string]string) (*connection, error) { addr, err := net.ResolveTCPAddr("tcp", n.node) if err != nil { return nil, err } c := &connection{node: n} c.socket = thrift.NewTSocketFromAddrTimeout(addr, timeout) c.transport = thrift.NewTFramedTransport(c.socket) protocol := thrift.NewTBinaryProtocolTransport(c.transport) c.client = cassandra.NewCassandraClientProtocol(c.transport, protocol, protocol) if err = c.transport.Open(); err != nil { return nil, err } version, err := c.client.DescribeVersion() if err != nil { c.close() return nil, err } versionComponents := strings.Split(version, ".") if len(versionComponents) < 1 { return nil, errors.New(fmt.Sprint("Cannot parse the Thrift API version number: ", version)) } majorVersion, err := strconv.Atoi(versionComponents[0]) if err != nil { return nil, errors.New(fmt.Sprint("Cannot parse the Thrift API version number: ", version)) } if majorVersion < LOWEST_COMPATIBLE_VERSION { return nil, errors.New(fmt.Sprint("Unsupported Thrift API version, lowest supported is ", LOWEST_COMPATIBLE_VERSION, ", server reports ", majorVersion)) } if len(authentication) > 0 { ar := cassandra.NewAuthenticationRequest() ar.Credentials = authentication err := c.client.Login(ar) if err != nil { c.close() switch err.(type) { case *cassandra.AuthenticationException: return nil, errors.New("Login error: cannot authenticate with the given credentials") case *cassandra.AuthorizationException: return nil, errors.New("Login error: the given credentials are not authorized to access the server") default: return nil, err } } } err = c.client.SetKeyspace(keyspace) if err != nil { c.close() switch err.(type) { case *cassandra.InvalidRequestException: err = errors.New("Cannot set the keyspace " + keyspace) } return nil, err } return c, nil }
// DeserializeThrift takes a byte slice and attempts to write it into the // given thrift struct using the thrift binary protocol. This is a temporary // measure before frames can be forwarded directly past the endpoint to the proper // destinaiton. func DeserializeThrift(b []byte, s athrift.TStruct) error { reader := bytes.NewReader(b) transport := athrift.NewStreamTransportR(reader) return s.Read(athrift.NewTBinaryProtocolTransport(transport)) }
func (t *readWriterTransport) RemainingBytes() uint64 { const maxSize = ^uint64(0) return maxSize } var _ thrift.TRichTransport = &readWriterTransport{} type thriftProtocol struct { transport *readWriterTransport protocol *thrift.TBinaryProtocol } var thriftProtocolPool = sync.Pool{ New: func() interface{} { transport := &readWriterTransport{} protocol := thrift.NewTBinaryProtocolTransport(transport) return &thriftProtocol{transport, protocol} }, } func getProtocolWriter(writer io.Writer) *thriftProtocol { wp := thriftProtocolPool.Get().(*thriftProtocol) wp.transport.Reader = nil wp.transport.Writer = writer return wp } func getProtocolReader(reader io.Reader) *thriftProtocol { wp := thriftProtocolPool.Get().(*thriftProtocol) wp.transport.Reader = reader wp.transport.Writer = nil