Send a connect protocol message to the server, issue user/password if applicable. Will wait for a flush to return from the server for error processing.
()
| 3017 | // applicable. Will wait for a flush to return from the server for error |
| 3018 | // processing. |
| 3019 | func (nc *Conn) sendConnect() error { |
| 3020 | // Construct the CONNECT protocol string |
| 3021 | cProto, err := nc.connectProto() |
| 3022 | if err != nil { |
| 3023 | if !nc.initc { |
| 3024 | if asyncErrorCB := nc.Opts.AsyncErrorCB; asyncErrorCB != nil { |
| 3025 | nc.ach.push(func() { asyncErrorCB(nc, nil, err) }) |
| 3026 | } |
| 3027 | } |
| 3028 | return err |
| 3029 | } |
| 3030 | |
| 3031 | // Write the protocol and PING directly to the underlying writer. |
| 3032 | if err := nc.bw.writeDirect(cProto, pingProto); err != nil { |
| 3033 | return err |
| 3034 | } |
| 3035 | |
| 3036 | // We don't want to read more than we need here, otherwise |
| 3037 | // we would need to transfer the excess read data to the readLoop. |
| 3038 | // Since in normal situations we just are looking for a PONG\r\n, |
| 3039 | // reading byte-by-byte here is ok. |
| 3040 | proto, err := nc.readProto() |
| 3041 | if err != nil { |
| 3042 | if !nc.initc { |
| 3043 | if asyncErrorCB := nc.Opts.AsyncErrorCB; asyncErrorCB != nil { |
| 3044 | nc.ach.push(func() { asyncErrorCB(nc, nil, err) }) |
| 3045 | } |
| 3046 | } |
| 3047 | return err |
| 3048 | } |
| 3049 | |
| 3050 | // If opts.Verbose is set, handle +OK |
| 3051 | if nc.Opts.Verbose && proto == okProto { |
| 3052 | // Read the rest now... |
| 3053 | proto, err = nc.readProto() |
| 3054 | if err != nil { |
| 3055 | if !nc.initc { |
| 3056 | if asyncErrorCB := nc.Opts.AsyncErrorCB; asyncErrorCB != nil { |
| 3057 | nc.ach.push(func() { asyncErrorCB(nc, nil, err) }) |
| 3058 | } |
| 3059 | } |
| 3060 | return err |
| 3061 | } |
| 3062 | } |
| 3063 | |
| 3064 | // We expect a PONG |
| 3065 | if proto != pongProto { |
| 3066 | // But it could be something else, like -ERR |
| 3067 | |
| 3068 | // Since we no longer use ReadLine(), trim the trailing "\r\n" |
| 3069 | proto = strings.TrimRight(proto, "\r\n") |
| 3070 | |
| 3071 | // If it's a server error... |
| 3072 | if strings.HasPrefix(proto, _ERR_OP_) { |
| 3073 | // Remove -ERR, trim spaces and quotes, and convert to lower case. |
| 3074 | proto = normalizeErr(proto) |
| 3075 | |
| 3076 | // Check if this is an auth error |
no test coverage detected