SHARE
TWEET

Untitled

a guest Jan 14th, 2020 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. func (ctx *ClientContext) startHandler() {
  2.     log, _ := logger.New("handler-" + strconv.Itoa(lastClient), 1)
  3.     log.SetLogLevel(defaultLogLevel)
  4.  
  5.     defer func() {
  6.         if r := recover(); r != nil {
  7.             log.Criticalf("PANIC! %v", r)
  8.         }
  9.     }()
  10.  
  11. outerLoop:
  12.     for {
  13.         var size uint16
  14.         err := binary.Read(ctx.Conn, binary.BigEndian, &size)
  15.         if err != nil {
  16.             log.Errorf("Error happened: %v", err)
  17.             break
  18.         }
  19.         // TODO: Size limiting
  20.         buf := make([]byte, size)
  21.         read := 0
  22.         for read != int(size) {
  23.             _read, err := ctx.Conn.Read(buf[read:])
  24.             read += _read
  25.             if err != nil {
  26.                 log.Errorf("Error occurred while reading from client:", err)
  27.                 break outerLoop
  28.             }
  29.         }
  30.  
  31.         header := protocol.MessageHeader{}
  32.         if ctx.Encoding == "" {
  33.             log.Debug("No encoding set, trying to guess")
  34.             err = json.Unmarshal(buf, header)
  35.             if err != nil {
  36.                 log.Debug("JSON decode failed, moving on")
  37.                 _, err = header.UnmarshalMsg(buf)
  38.                 if err != nil {
  39.                     log.Debug("MessagePack decode failed, closing the connection")
  40.                     break
  41.                 } else {
  42.                     log.Debug("Determined that encoding is msgpack")
  43.                     ctx.Encoding = "msgpack"
  44.                 }
  45.             } else {
  46.                 log.Debug("Determined that encoding is json")
  47.                 ctx.Encoding = "json"
  48.             }
  49.         } else {
  50.             header, err = ctx.ReadHeader(buf)
  51.             if err != nil {
  52.                 log.Debugf("Message decode failed: %v", err)
  53.                 header, errBody := protocol.NewError(protocol.ErrDeserializationFailure, protocol.ErrDeserializationFailureDesc)
  54.                 err = ctx.Reply(header, errBody)
  55.                 if err != nil {
  56.                     log.Warningf("Failed writing reply to client: %v", err)
  57.                 }
  58.                 // On deserialization failure we cannot be sure that connection is clear
  59.                 // TODO: Is closing the connection a valid strategy?
  60.                 break
  61.             }
  62.         }
  63.         result, clientErr, err := ctx.ProcessMessage(&header)
  64.         if err != nil {
  65.             log.Criticalf("Message processing failed: %v", err)
  66.             header, errBody := protocol.NewError(protocol.ErrInternalServerError, protocol.ErrInternalServerErrorDesc)
  67.             err = ctx.Reply(header, errBody)
  68.             if err != nil {
  69.                 log.Warningf("Failed writing reply to client: %v", err)
  70.                 break
  71.             }
  72.             continue
  73.         }
  74.         if clientErr != nil {
  75.             header.Type = protocol.TypeError
  76.             err = ctx.Reply(header, clientErr)
  77.             if err != nil {
  78.                 log.Warningf("Failed writing reply to client: %v", err)
  79.                 break
  80.             }
  81.             continue
  82.         }
  83.         err = ctx.Reply(header, result)
  84.         if err != nil {
  85.             log.Warningf("Failed writing reply to client: %v", err)
  86.             break
  87.         }
  88.     }
  89.     log.Debug("Connection is closed")
  90.     _ = ctx.Conn.Close()
  91. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top