tools.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package rest_websocket
  2. import (
  3. "io"
  4. "git.ali33.ru/fcg-xvii/go-tools/json"
  5. "git.ali33.ru/fcg-xvii/rest"
  6. )
  7. // конверторы
  8. // Int64ToBytes упаковывает int64 в срез байтов заданной длины
  9. func Int64ToBytes(num int64, byteCount int) []byte {
  10. bytes := make([]byte, byteCount)
  11. for i := 0; i < byteCount; i++ {
  12. shift := uint((byteCount - 1 - i) * 8)
  13. bytes[i] = byte(num >> shift)
  14. }
  15. return bytes
  16. }
  17. // BytesToInt64 конвертирует срез байтов в int64
  18. func BytesToInt64(bytes []byte) int64 {
  19. var num int64
  20. for _, b := range bytes {
  21. num = (num << 8) | int64(b)
  22. }
  23. return num
  24. }
  25. func ioError(field string, err error) rest.IErrorArgs {
  26. return rest.NewError(
  27. "ErrIO",
  28. json.Map{
  29. "field": field,
  30. "error": err.Error(),
  31. },
  32. )
  33. }
  34. // чтение
  35. func ReadBuf(r io.Reader, size int, field string) ([]byte, rest.IErrorArgs) {
  36. buf := make([]byte, size)
  37. if _, err := r.Read(buf); err != nil {
  38. return nil, ioError(field, err)
  39. }
  40. return buf, nil
  41. }
  42. func ReadBufSize(r io.Reader, lenSize int, field string) ([]byte, rest.IErrorArgs) {
  43. var size int64
  44. if err := ReadInt64(r, lenSize, field+"_size", &size); err != nil {
  45. return nil, err
  46. }
  47. buf, err := ReadBuf(r, int(size), field)
  48. return buf, err
  49. }
  50. func ReadInt64(r io.Reader, size int, field string, result *int64) rest.IErrorArgs {
  51. buf := make([]byte, size)
  52. if _, err := r.Read(buf); err != nil {
  53. return ioError(field, err)
  54. }
  55. *result = BytesToInt64(buf)
  56. return nil
  57. }
  58. func ReadString(r io.Reader, lenSize int, field string, result *string) rest.IErrorArgs {
  59. strBuf, err := ReadBufSize(r, lenSize, field)
  60. if err != nil {
  61. return err
  62. }
  63. *result = string(strBuf)
  64. return nil
  65. }
  66. func ReadByte(r io.Reader, field string, result *byte) rest.IErrorArgs {
  67. buf, err := ReadBuf(r, 1, field)
  68. if err != nil {
  69. return err
  70. }
  71. *result = buf[0]
  72. return nil
  73. }
  74. // запись
  75. func WriteBuf(w io.Writer, buf []byte, field string) rest.IErrorArgs {
  76. if _, err := w.Write(buf); err != nil {
  77. return ioError(field, err)
  78. }
  79. return nil
  80. }
  81. func WriteInt64(w io.Writer, val int64, size int, field string) rest.IErrorArgs {
  82. buf := Int64ToBytes(val, size)
  83. return WriteBuf(w, buf, field)
  84. }
  85. func WriteString(w io.Writer, val, field string, lenSize int) rest.IErrorArgs {
  86. // длина
  87. if err := WriteInt64(w, int64(len(val)), lenSize, field+"_size"); err != nil {
  88. return err
  89. }
  90. // строка
  91. if len(val) == 0 {
  92. return nil
  93. }
  94. return WriteBuf(w, []byte(val), field)
  95. }
  96. func WriteByte(w io.Writer, val byte, field string) rest.IErrorArgs {
  97. return WriteBuf(w, []byte{val}, field)
  98. }
  99. func WriteBufSize(w io.Writer, val []byte, lenSize int, field string) rest.IErrorArgs {
  100. // длина
  101. if err := WriteInt64(w, int64(len(val)), lenSize, field+"_size"); err != nil {
  102. return err
  103. }
  104. // буфер
  105. return WriteBuf(w, val, field)
  106. }