gtcp_pool_pkg.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package gtcp
  7. import (
  8. "time"
  9. )
  10. // SendPkg sends a package containing <data> to the connection.
  11. // The optional parameter <option> specifies the package options for sending.
  12. func (c *PoolConn) SendPkg(data []byte, option ...PkgOption) (err error) {
  13. if err = c.Conn.SendPkg(data, option...); err != nil && c.status == gCONN_STATUS_UNKNOWN {
  14. if v, e := c.pool.NewFunc(); e == nil {
  15. c.Conn = v.(*PoolConn).Conn
  16. err = c.Conn.SendPkg(data, option...)
  17. } else {
  18. err = e
  19. }
  20. }
  21. if err != nil {
  22. c.status = gCONN_STATUS_ERROR
  23. } else {
  24. c.status = gCONN_STATUS_ACTIVE
  25. }
  26. return err
  27. }
  28. // RecvPkg receives package from connection using simple package protocol.
  29. // The optional parameter <option> specifies the package options for receiving.
  30. func (c *PoolConn) RecvPkg(option ...PkgOption) ([]byte, error) {
  31. data, err := c.Conn.RecvPkg(option...)
  32. if err != nil {
  33. c.status = gCONN_STATUS_ERROR
  34. } else {
  35. c.status = gCONN_STATUS_ACTIVE
  36. }
  37. return data, err
  38. }
  39. // RecvPkgWithTimeout reads data from connection with timeout using simple package protocol.
  40. func (c *PoolConn) RecvPkgWithTimeout(timeout time.Duration, option ...PkgOption) (data []byte, err error) {
  41. if err := c.SetRecvDeadline(time.Now().Add(timeout)); err != nil {
  42. return nil, err
  43. }
  44. defer c.SetRecvDeadline(time.Time{})
  45. data, err = c.RecvPkg(option...)
  46. return
  47. }
  48. // SendPkgWithTimeout writes data to connection with timeout using simple package protocol.
  49. func (c *PoolConn) SendPkgWithTimeout(data []byte, timeout time.Duration, option ...PkgOption) (err error) {
  50. if err := c.SetSendDeadline(time.Now().Add(timeout)); err != nil {
  51. return err
  52. }
  53. defer c.SetSendDeadline(time.Time{})
  54. err = c.SendPkg(data, option...)
  55. return
  56. }
  57. // SendRecvPkg writes data to connection and blocks reading response using simple package protocol.
  58. func (c *PoolConn) SendRecvPkg(data []byte, option ...PkgOption) ([]byte, error) {
  59. if err := c.SendPkg(data, option...); err == nil {
  60. return c.RecvPkg(option...)
  61. } else {
  62. return nil, err
  63. }
  64. }
  65. // RecvPkgWithTimeout reads data from connection with timeout using simple package protocol.
  66. func (c *PoolConn) SendRecvPkgWithTimeout(data []byte, timeout time.Duration, option ...PkgOption) ([]byte, error) {
  67. if err := c.SendPkg(data, option...); err == nil {
  68. return c.RecvPkgWithTimeout(timeout, option...)
  69. } else {
  70. return nil, err
  71. }
  72. }