ReceiveStartupMessage receives the initial connection message. This method is used of the normal Receive method because the initial connection message is "special" and does not include the message type as the first byte. This will return either a StartupMessage, SSLRequest, GSSEncRequest, or CancelR
()
| 119 | // because the initial connection message is "special" and does not include the message type as the first byte. This |
| 120 | // will return either a StartupMessage, SSLRequest, GSSEncRequest, or CancelRequest. |
| 121 | func (b *Backend) ReceiveStartupMessage() (FrontendMessage, error) { |
| 122 | buf, err := b.cr.Next(4) |
| 123 | if err != nil { |
| 124 | return nil, err |
| 125 | } |
| 126 | msgSize := int(int32(binary.BigEndian.Uint32(buf)) - 4) |
| 127 | |
| 128 | if msgSize < minStartupPacketLen || msgSize > maxStartupPacketLen { |
| 129 | return nil, fmt.Errorf("invalid length of startup packet: %d", msgSize) |
| 130 | } |
| 131 | |
| 132 | buf, err = b.cr.Next(msgSize) |
| 133 | if err != nil { |
| 134 | return nil, translateEOFtoErrUnexpectedEOF(err) |
| 135 | } |
| 136 | |
| 137 | code := binary.BigEndian.Uint32(buf) |
| 138 | |
| 139 | switch code { |
| 140 | case ProtocolVersion30, ProtocolVersion32: |
| 141 | err = b.startupMessage.Decode(buf) |
| 142 | if err != nil { |
| 143 | return nil, err |
| 144 | } |
| 145 | return &b.startupMessage, nil |
| 146 | case sslRequestNumber: |
| 147 | err = b.sslRequest.Decode(buf) |
| 148 | if err != nil { |
| 149 | return nil, err |
| 150 | } |
| 151 | return &b.sslRequest, nil |
| 152 | case cancelRequestCode: |
| 153 | err = b.cancelRequest.Decode(buf) |
| 154 | if err != nil { |
| 155 | return nil, err |
| 156 | } |
| 157 | return &b.cancelRequest, nil |
| 158 | case gssEncReqNumber: |
| 159 | err = b.gssEncRequest.Decode(buf) |
| 160 | if err != nil { |
| 161 | return nil, err |
| 162 | } |
| 163 | return &b.gssEncRequest, nil |
| 164 | default: |
| 165 | return nil, fmt.Errorf("unknown startup message code: %d", code) |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | // Receive receives a message from the frontend. The returned message is only valid until the next call to Receive. |
| 170 | func (b *Backend) Receive() (FrontendMessage, error) { |