本文整理匯總了Golang中github.com/hashicorp/go-msgpack/codec.NewDecoder函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewDecoder函數的具體用法?Golang NewDecoder怎麽用?Golang NewDecoder使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewDecoder函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: readTCP
// readTCP is used to read the start of a TCP stream.
// it decrypts and decompresses the stream if necessary
func (m *Memberlist) readTCP(conn net.Conn) (messageType, io.Reader, *codec.Decoder, error) {
// Created a buffered reader
var bufConn io.Reader = bufio.NewReader(conn)
// Read the message type
buf := [1]byte{0}
if _, err := bufConn.Read(buf[:]); err != nil {
return 0, nil, nil, err
}
msgType := messageType(buf[0])
// Check if the message is encrypted
if msgType == encryptMsg {
if !m.config.EncryptionEnabled() {
return 0, nil, nil,
fmt.Errorf("Remote state is encrypted and encryption is not configured")
}
plain, err := m.decryptRemoteState(bufConn)
if err != nil {
return 0, nil, nil, err
}
// Reset message type and bufConn
msgType = messageType(plain[0])
bufConn = bytes.NewReader(plain[1:])
} else if m.config.EncryptionEnabled() {
return 0, nil, nil,
fmt.Errorf("Encryption is configured but remote state is not encrypted")
}
// Get the msgPack decoders
hd := codec.MsgpackHandle{}
dec := codec.NewDecoder(bufConn, &hd)
// Check if we have a compressed message
if msgType == compressMsg {
var c compress
if err := dec.Decode(&c); err != nil {
return 0, nil, nil, err
}
decomp, err := decompressBuffer(&c)
if err != nil {
return 0, nil, nil, err
}
// Reset the message type
msgType = messageType(decomp[0])
// Create a new bufConn
bufConn = bytes.NewReader(decomp[1:])
// Create a new decoder
dec = codec.NewDecoder(bufConn, &hd)
}
return msgType, bufConn, dec, nil
}
示例2: decodePeerMsg
func decodePeerMsg(buf []byte) []string {
var data []string
if err := codec.NewDecoder(bytes.NewReader(buf), &mh).Decode(&data); err != nil {
kingpin.Errorf("Error while decoding (generic msgpack) message: %s\n", err.Error())
}
return data
}
示例3: printMsgPackData
func printMsgPackData(log raft.Log, msgtype string, buf []byte) {
var data interface{}
if err := codec.NewDecoder(bytes.NewReader(buf), &mh).Decode(&data); err != nil {
kingpin.Errorf("Error while decoding (generic msgpack) message: %s\n", err.Error())
}
printJson(log, msgtype, data)
}
示例4: newConnToLeader
// joins the raft leader and sets up infrastructure for
// processing commands
// can return ErrNotLeader
func newConnToLeader(conn net.Conn, advertiseAddr string, lg *log.Logger) (*connToLeader, error) {
// send join command
h := &codec.MsgpackHandle{}
ret := &connToLeader{
c: conn,
e: codec.NewEncoder(conn, h),
d: codec.NewDecoder(conn, h),
l: new(sync.Mutex),
lg: lg,
pending: make(chan *commandCallback, 64),
}
join := &joinReq{
PeerAddr: advertiseAddr,
}
err := ret.e.Encode(join)
if err != nil {
ret.c.Close()
return nil, err
}
joinResp := &joinResp{}
err = ret.d.Decode(joinResp)
if err != nil {
ret.lg.Printf("Error connecting to leader at %s : %s", conn.RemoteAddr().String(), err)
ret.c.Close()
return nil, err
}
go ret.readResponses()
return ret, nil
}
示例5: handleSnapshotRequest
// handleSnapshotRequest reads the request from the conn and dispatches it. This
// will be called from a goroutine after an incoming stream is determined to be
// a snapshot request.
func (s *Server) handleSnapshotRequest(conn net.Conn) error {
var args structs.SnapshotRequest
dec := codec.NewDecoder(conn, &codec.MsgpackHandle{})
if err := dec.Decode(&args); err != nil {
return fmt.Errorf("failed to decode request: %v", err)
}
var reply structs.SnapshotResponse
snap, err := s.dispatchSnapshotRequest(&args, conn, &reply)
if err != nil {
reply.Error = err.Error()
goto RESPOND
}
defer func() {
if err := snap.Close(); err != nil {
s.logger.Printf("[ERR] consul: Failed to close snapshot: %v", err)
}
}()
RESPOND:
enc := codec.NewEncoder(conn, &codec.MsgpackHandle{})
if err := enc.Encode(&reply); err != nil {
return fmt.Errorf("failed to encode response: %v", err)
}
if snap != nil {
if _, err := io.Copy(conn, snap); err != nil {
return fmt.Errorf("failed to stream snapshot: %v", err)
}
}
return nil
}
示例6: getConn
// getConn is used to get a connection from the pool.
func (n *NetworkTransport) getConn(target string) (*netConn, error) {
// Check for a pooled conn
if conn := n.getPooledConn(target); conn != nil {
return conn, nil
}
// Dial a new connection
conn, err := n.stream.Dial(target, n.timeout)
if err != nil {
return nil, err
}
// Wrap the conn
netConn := &netConn{
target: target,
conn: conn,
r: bufio.NewReader(conn),
w: bufio.NewWriter(conn),
}
// Setup encoder/decoders
netConn.dec = codec.NewDecoder(netConn.r, &codec.MsgpackHandle{})
netConn.enc = codec.NewEncoder(netConn.w, &codec.MsgpackHandle{})
// Done
return netConn, nil
}
示例7: listen
// listen is a long running routine that listens for new clients
func (i *AgentRPC) listen() {
for {
conn, err := i.listener.Accept()
if err != nil {
if i.stop {
return
}
i.logger.Printf("[ERR] agent.rpc: Failed to accept client: %v", err)
continue
}
i.logger.Printf("[INFO] agent.rpc: Accepted client: %v", conn.RemoteAddr())
// Wrap the connection in a client
client := &rpcClient{
name: conn.RemoteAddr().String(),
conn: conn,
reader: bufio.NewReader(conn),
writer: bufio.NewWriter(conn),
}
client.dec = codec.NewDecoder(client.reader, msgpackHandle)
client.enc = codec.NewEncoder(client.writer, msgpackHandle)
// Register the client
i.Lock()
if !i.stop {
i.clients[client.name] = client
go i.handleClient(client)
} else {
conn.Close()
}
i.Unlock()
}
}
示例8: MockDecoder
func MockDecoder(buf []byte) interface{} {
out := new(MockData)
err := codec.NewDecoder(bytes.NewReader(buf), msgpackHandle).Decode(out)
if err != nil {
panic(err)
}
return out
}
示例9: NewCodec
// NewCodec returns a MsgpackCodec that can be used as either
// a Client or Server rpc Codec. It also provides controls for
// enabling and disabling buffering for both reads and writes.
func NewCodec(bufReads, bufWrites bool, conn io.ReadWriteCloser) *MsgpackCodec {
cc := &MsgpackCodec{
conn: conn,
}
if bufReads {
cc.bufR = bufio.NewReader(conn)
cc.dec = codec.NewDecoder(cc.bufR, msgpackHandle)
} else {
cc.dec = codec.NewDecoder(cc.conn, msgpackHandle)
}
if bufWrites {
cc.bufW = bufio.NewWriter(conn)
cc.enc = codec.NewEncoder(cc.bufW, msgpackHandle)
} else {
cc.enc = codec.NewEncoder(cc.conn, msgpackHandle)
}
return cc
}
示例10: SnapshotRPC
// SnapshotRPC is a streaming client function for performing a snapshot RPC
// request to a remote server. It will create a fresh connection for each
// request, send the request header, and then stream in any data from the
// reader (for a restore). It will then parse the received response header, and
// if there's no error will return an io.ReadCloser (that you must close) with
// the streaming output (for a snapshot). If the reply contains an error, this
// will always return an error as well, so you don't need to check the error
// inside the filled-in reply.
func SnapshotRPC(pool *ConnPool, dc string, addr net.Addr,
args *structs.SnapshotRequest, in io.Reader, reply *structs.SnapshotResponse) (io.ReadCloser, error) {
conn, hc, err := pool.Dial(dc, addr)
if err != nil {
return nil, err
}
// keep will disarm the defer on success if we are returning the caller
// our connection to stream the output.
var keep bool
defer func() {
if !keep {
conn.Close()
}
}()
// Write the snapshot RPC byte to set the mode, then perform the
// request.
if _, err := conn.Write([]byte{byte(rpcSnapshot)}); err != nil {
return nil, fmt.Errorf("failed to write stream type: %v", err)
}
// Push the header encoded as msgpack, then stream the input.
enc := codec.NewEncoder(conn, &codec.MsgpackHandle{})
if err := enc.Encode(&args); err != nil {
return nil, fmt.Errorf("failed to encode request: %v", err)
}
if _, err := io.Copy(conn, in); err != nil {
return nil, fmt.Errorf("failed to copy snapshot in: %v", err)
}
// Our RPC protocol requires support for a half-close in order to signal
// the other side that they are done reading the stream, since we don't
// know the size in advance. This saves us from having to buffer just to
// calculate the size.
if hc != nil {
if err := hc.CloseWrite(); err != nil {
return nil, fmt.Errorf("failed to half close snapshot connection: %v", err)
}
} else {
return nil, fmt.Errorf("snapshot connection requires half-close support")
}
// Pull the header decoded as msgpack. The caller can continue to read
// the conn to stream the remaining data.
dec := codec.NewDecoder(conn, &codec.MsgpackHandle{})
if err := dec.Decode(reply); err != nil {
return nil, fmt.Errorf("failed to decode response: %v", err)
}
if reply.Error != "" {
return nil, errors.New(reply.Error)
}
keep = true
return conn, nil
}
示例11: decodeQueryEventRecord
func decodeQueryEventRecord(buf []byte) (EventRecord, error) {
var qr QueryEventRecord
handle := codec.MsgpackHandle{RawToString: true, WriteExt: true}
if err := codec.NewDecoder(bytes.NewReader(buf), &handle).Decode(&qr); err != nil {
log.Printf("decoding failed")
return nil, err
}
return EventRecord(qr), nil
}
示例12: Restore
// See raft.FSM.
func (m *MockFSM) Restore(in io.ReadCloser) error {
m.Lock()
defer m.Unlock()
defer in.Close()
hd := codec.MsgpackHandle{}
dec := codec.NewDecoder(in, &hd)
m.logs = nil
return dec.Decode(&m.logs)
}
示例13: decodeTags
// decodeTags is used to decode a tag map
func (t *tribe) decodeTags(buf []byte) map[string]string {
tags := make(map[string]string)
r := bytes.NewReader(buf)
dec := codec.NewDecoder(r, &codec.MsgpackHandle{})
if err := dec.Decode(&tags); err != nil {
t.logger.WithFields(log.Fields{
"_block": "decode-tags",
"error": err,
}).Error("Failed to decode tags")
}
return tags
}
示例14: decodeTags
// decodeTags is used to decode a tag map
func (s *Serf) decodeTags(buf []byte) map[string]string {
tags := make(map[string]string)
// Backwards compatibility mode
if len(buf) == 0 || buf[0] != tagMagicByte {
tags["role"] = string(buf)
return tags
}
// Decode the tags
r := bytes.NewReader(buf[1:])
dec := codec.NewDecoder(r, &codec.MsgpackHandle{})
if err := dec.Decode(&tags); err != nil {
s.logger.Printf("[ERR] serf: Failed to decode tags: %v", err)
}
return tags
}
示例15: ClientFromConfig
// ClientFromConfig is used to create a new RPC client given the
// configuration object. This will return a client, or an error if
// the connection could not be established.
func ClientFromConfig(c *Config) (*RPCClient, error) {
// Setup the defaults
if c.Timeout == 0 {
c.Timeout = DefaultTimeout
}
// Try to dial to serf
conn, err := net.DialTimeout("tcp", c.Addr, c.Timeout)
if err != nil {
return nil, err
}
// Create the client
client := &RPCClient{
seq: 0,
timeout: c.Timeout,
conn: conn.(*net.TCPConn),
reader: bufio.NewReader(conn),
writer: bufio.NewWriter(conn),
dispatch: make(map[uint64]seqHandler),
shutdownCh: make(chan struct{}),
}
client.dec = codec.NewDecoder(client.reader,
&codec.MsgpackHandle{RawToString: true, WriteExt: true})
client.enc = codec.NewEncoder(client.writer,
&codec.MsgpackHandle{RawToString: true, WriteExt: true})
go client.listen()
// Do the initial handshake
if err := client.handshake(); err != nil {
client.Close()
return nil, err
}
// Do the initial authentication if needed
if c.AuthKey != "" {
if err := client.auth(c.AuthKey); err != nil {
client.Close()
return nil, err
}
}
return client, err
}