context.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package cli
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "math"
  7. )
  8. type Context struct {
  9. Id int64
  10. seq uint16
  11. ctx context.Context
  12. wc io.WriteCloser
  13. params map[string]string
  14. args []string
  15. }
  16. func (ctx *Context) reset(id int64, wc io.WriteCloser) {
  17. ctx.Id = id
  18. ctx.wc = wc
  19. ctx.seq = 0
  20. ctx.ctx = context.Background()
  21. ctx.args = make([]string, 0)
  22. ctx.params = make(map[string]string)
  23. }
  24. func (ctx *Context) setArgs(args []string) {
  25. ctx.args = args
  26. }
  27. func (ctx *Context) setParam(ps map[string]string) {
  28. ctx.params = ps
  29. }
  30. func (ctx *Context) Bind(v any) (err error) {
  31. return
  32. }
  33. func (ctx *Context) setContext(c context.Context) {
  34. ctx.ctx = c
  35. }
  36. func (ctx *Context) Context() context.Context {
  37. return ctx.Context()
  38. }
  39. func (ctx *Context) Argument(index int) string {
  40. if index >= len(ctx.args) || index < 0 {
  41. return ""
  42. }
  43. return ctx.args[index]
  44. }
  45. func (ctx *Context) Param(s string) string {
  46. if v, ok := ctx.params[s]; ok {
  47. return v
  48. }
  49. return ""
  50. }
  51. func (ctx *Context) Success(v any) (err error) {
  52. return ctx.send(responsePayload{Type: PacketTypeCommand, Data: v})
  53. }
  54. func (ctx *Context) Error(code int, reason string) (err error) {
  55. return ctx.send(responsePayload{Type: PacketTypeCommand, Code: code, Reason: reason})
  56. }
  57. func (ctx *Context) Close() (err error) {
  58. return ctx.wc.Close()
  59. }
  60. func (ctx *Context) send(res responsePayload) (err error) {
  61. var (
  62. ok bool
  63. buf []byte
  64. marshal encoder
  65. )
  66. if res.Code > 0 {
  67. err = writeFrame(ctx.wc, &Frame{
  68. Feature: Feature,
  69. Type: res.Type,
  70. Seq: ctx.seq,
  71. Flag: FlagComplete,
  72. Error: fmt.Sprintf("ERROR(%d): %s", res.Code, res.Reason),
  73. })
  74. return
  75. }
  76. if res.Data == nil {
  77. buf = OK
  78. goto __END
  79. }
  80. if marshal, ok = res.Data.(encoder); ok {
  81. buf, err = marshal.Marshal()
  82. goto __END
  83. }
  84. buf, err = serialize(res.Data)
  85. __END:
  86. if err != nil {
  87. return
  88. }
  89. offset := 0
  90. chunkSize := math.MaxInt16 - 1
  91. n := len(buf) / chunkSize
  92. for i := 0; i < n; i++ {
  93. if err = writeFrame(ctx.wc, newFrame(res.Type, FlagPortion, ctx.seq, 0, buf[offset:chunkSize+offset])); err != nil {
  94. return
  95. }
  96. offset += chunkSize
  97. }
  98. err = writeFrame(ctx.wc, newFrame(res.Type, FlagComplete, ctx.seq, 0, buf[offset:]))
  99. return
  100. }
  101. func newContext(id int64, wc io.WriteCloser) *Context {
  102. return &Context{
  103. Id: id,
  104. wc: wc,
  105. }
  106. }