// serialize serializes a message. It returns an error if serialization // failed, or if the message was too big to serialize based on the current // msgSize. The config lock is held during serialize. func (fs *FileServer) serialize(m qp.Message) ([]byte, error) { var ( mt qp.MessageType err error ) if mt, err = fs.proto.MessageType(m); err != nil { return nil, err } l := m.EncodedSize() + qp.HeaderSize if l > int(fs.msgSize) { return nil, errMsgTooBig } b := make([]byte, l) if err = m.Marshal(b[qp.HeaderSize:]); err != nil { return nil, err } binary.LittleEndian.PutUint32(b[0:4], uint32(l)) b[4] = byte(mt) return b, nil }
// received is the callback for messages decoded from the io.ReadWriter. func (c *Transport) received(m qp.Message) error { c.queueLock.Lock() defer c.queueLock.Unlock() t := m.GetTag() if ch, ok := c.queue[t]; ok { ch <- m close(ch) delete(c.queue, t) return nil } return ErrNoSuchTag }
// Send sends a message and retrieves the response. func (c *Transport) Send(m qp.Message) (qp.Message, error) { t := m.GetTag() ch := c.getChannel(t) if ch == nil { return nil, ErrNoSuchTag } err := c.encoder.WriteMessage(m) if err != nil { return nil, c.die(err) } return <-ch, nil }
func (fs *FileServer) received(m qp.Message) { rs := &requestState{tag: m.GetTag()} fs.logreq(rs.tag, m) if err := fs.register(rs, true); err != nil { fs.sendError(rs, err.Error()) return } switch mx := m.(type) { // Basic messages case *qp.VersionRequest: fs.version(mx, rs) case *qp.AuthRequest: fs.auth(mx, rs) case *qp.AttachRequest: fs.attach(mx, rs) case *qp.FlushRequest: fs.flush(mx, rs) case *qp.WalkRequest: go fs.walk(mx, rs) case *qp.OpenRequest: go fs.open(mx, rs) case *qp.CreateRequest: go fs.create(mx, rs) case *qp.ReadRequest: go fs.read(mx, rs) case *qp.WriteRequest: go fs.write(mx, rs) case *qp.ClunkRequest: go fs.clunk(mx, rs) case *qp.RemoveRequest: go fs.remove(mx, rs) case *qp.StatRequest: go fs.stat(mx, rs) case *qp.WriteStatRequest: go fs.writeStat(mx, rs) default: fs.sendError(rs, UnsupportedMessage) } }