ReadFirstBuffer reads the first buffer from either clientConn or destConn
(ctx context.Context, logger *zap.Logger, clientConn, destConn net.Conn)
| 18 | |
| 19 | // ReadFirstBuffer reads the first buffer from either clientConn or destConn |
| 20 | func ReadFirstBuffer(ctx context.Context, logger *zap.Logger, clientConn, destConn net.Conn) ([]byte, string, error) { |
| 21 | // Attempt to read from destConn first |
| 22 | buf, err := util.ReadBytes(ctx, logger, destConn) |
| 23 | // If there is data from destConn, return it |
| 24 | if err == nil { |
| 25 | return buf, "destination", nil |
| 26 | } |
| 27 | // If the error is a timeout, try to read from clientConn |
| 28 | if netErr, ok := err.(net.Error); ok && netErr.Timeout() { |
| 29 | buf, err = util.ReadBytes(ctx, logger, clientConn) |
| 30 | // If there is data from clientConn, return it |
| 31 | if err == nil { |
| 32 | return buf, "client", nil |
| 33 | } |
| 34 | // Return any error from reading clientConn |
| 35 | return nil, "", err |
| 36 | } |
| 37 | // Return any other error from reading destConn |
| 38 | return nil, "", err |
| 39 | } |
| 40 | |
| 41 | // ReadPacketStream reads packets from the connection and sends them to the bufferChannel |
| 42 | func ReadPacketStream(ctx context.Context, logger *zap.Logger, conn net.Conn, bufferChannel chan []byte, errChannel chan error) { |