func ReplyErrorAndArgs(errcode int32, args ...interface{}) *rpc.Message { msg := rpc.NewMessage(rpc.MAX_BUF_LEN) if errcode > 0 { sr := util.NewStoreArchiver(msg.Header) sr.Write(int8(1)) sr.Write(errcode) msg.Header = msg.Header[:sr.Len()] } if len(args) > 0 { mw := NewMessageWriter(msg) for i := 0; i < len(args); i++ { err := mw.Write(args[i]) if err != nil { msg.Free() panic("write args failed") return nil } } mw.Flush() } return msg }
//错误消息 func ReplyErrorMessage(errcode int32) *rpc.Message { msg := rpc.NewMessage(1) if errcode == 0 { return msg } sr := util.NewStoreArchiver(msg.Header) sr.Write(int8(1)) sr.Write(errcode) msg.Header = msg.Header[:sr.Len()] return msg }
//向客户端发包,存进一个消息队列,异步发送 func (c *ClientNode) Send(data []byte) error { if c.quit { return fmt.Errorf("client is quit") } message := rpc.NewMessage(len(data)) message.Body = append(message.Body, data...) select { case c.Sendbuf <- message: return nil default: message.Free() //满了,直接扔掉 return fmt.Errorf("send buffer full") } }
func (c *ClientCodec) ReadRequest(maxrc uint16) (*rpc.Message, error) { for { id, data, err := util.ReadPkg(c.rwc, c.cachebuf) if err != nil { return nil, err } switch id { case share.C2S_PING: c.node.Ping() break case share.C2S_RPC: msg := rpc.NewMessage(len(data)) ar := NewHeadWriter(msg) ar.Write(uint64(0)) ar.Write(c.node.MailBox.Uid) ar.Write("C2SC2SHelper.Call") msg.Header = msg.Header[:ar.Len()] msg.Body = append(msg.Body, data...) return msg, nil } } }
func (s *S2CHelper) call(src rpc.Mailbox, session int64, method string, out []byte) error { c := core.clientList.FindNode(session) if c == nil { return ErrClientNotFound } cachedata, exist := s.cachedata[session] if exist && cachedata != nil { //优先写入缓存 if len(cachedata.Body)+len(out) <= packagesize { //可以写入 cachedata.Body = append(cachedata.Body, out...) return nil } //超出长度,则先发送当前消息,保证消息顺序 if err := c.SendMessage(cachedata); err != nil { log.LogError(session, err) } cachedata.Free() cachedata = nil s.cachedata[session] = nil } if len(out) > packagesize { //超出包体大小,直接发送 //直接发送 if err := c.Send(out); err != nil { log.LogError(session, err) } return nil } //写入缓冲区 cachedata = rpc.NewMessage(packagesize) s.cachedata[session] = cachedata cachedata.Body = append(cachedata.Body, out...) return nil }
func NewMessage() *BodyWriter { msg := rpc.NewMessage(rpc.MAX_BUF_LEN) w := &BodyWriter{util.NewStoreArchiver(msg.Body), msg} return w }