コード例 #1
0
ファイル: session.go プロジェクト: thlac/stove
func (s *Session) HandlePacket(header *rpc.Header, body []byte) {
	if s.state == StateDisconnected {
		panic("cannot handle packets from disconnected clients")
	}
	serviceId := int(header.GetServiceId())
	methodId := int(header.GetMethodId())
	s.receivedToken = header.GetToken()

	if serviceId == 254 {
		s.HandleResponse(header.GetToken(), body)
	} else {
		resp := s.HandleRequest(serviceId, methodId, body)
		if resp != nil {
			respHead := &rpc.Header{
				ServiceId: proto.Uint32(254),
				Token:     header.Token,
				Size:      proto.Uint32(uint32(len(resp))),
			}
			err := s.QueuePacket(respHead, resp)
			if err != nil {
				log.Panicf("error: Session.HandlePacket: respond: %v", err)
			}
		}
	}
}
コード例 #2
0
ファイル: server.go プロジェクト: synap5e/stove
func (s *Server) handleClient(c net.Conn) {
	c.SetDeadline(time.Time{})

	sess := NewSession(s, c)
	defer sess.DisconnectOnPanic()
	buf := make([]byte, 0x1000)
	for {
		_, err := sess.conn.Read(buf[:2])
		if err != nil {
			log.Panicf("error: Server.handleClient: length read: %v", err)
		}
		headerLen := int(buf[0])<<8 | int(buf[1])
		if headerLen > len(buf) {
			buf = append(buf, make([]byte, headerLen-len(buf))...)
		}
		_, err = sess.conn.Read(buf[:headerLen])
		if err != nil {
			log.Panicf("error: Server.handleClient: header read: %v", err)
		}
		header := rpc.Header{}
		err = proto.Unmarshal(buf[:headerLen], &header)
		if err != nil {
			log.Panicf("error: Server.handleClient: header decode: %v", err)
		}
		bodyLen := int(header.GetSize())
		var body []byte
		if bodyLen > 0 {
			if bodyLen > len(buf) {
				buf = append(buf, make([]byte, bodyLen-len(buf))...)
			}
			body = buf[:bodyLen]
			_, err = sess.conn.Read(body)
			if err != nil {
				log.Panicf("error: Server.handleClient: body read: %v", err)
			}
		}
		log.Printf("handling packet %s %x", header.String(), body)
		sess.HandlePacket(&header, body)
	}
}