| 21 | } |
| 22 | |
| 23 | void RpcConnection::Open() |
| 24 | { |
| 25 | if (state == State::Connected) { |
| 26 | return; |
| 27 | } |
| 28 | |
| 29 | if (state == State::Disconnected && !connection->Open()) { |
| 30 | return; |
| 31 | } |
| 32 | |
| 33 | if (state == State::SentHandshake) { |
| 34 | JsonDocument message; |
| 35 | if (Read(message)) { |
| 36 | auto cmd = GetStrMember(&message, "cmd"); |
| 37 | auto evt = GetStrMember(&message, "evt"); |
| 38 | if (cmd && evt && !strcmp(cmd, "DISPATCH") && !strcmp(evt, "READY")) { |
| 39 | state = State::Connected; |
| 40 | if (onConnect) { |
| 41 | onConnect(message); |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | else { |
| 47 | sendFrame.opcode = Opcode::Handshake; |
| 48 | sendFrame.length = (uint32_t)JsonWriteHandshakeObj( |
| 49 | sendFrame.message, sizeof(sendFrame.message), RpcVersion, appId); |
| 50 | |
| 51 | if (connection->Write(&sendFrame, sizeof(MessageFrameHeader) + sendFrame.length)) { |
| 52 | state = State::SentHandshake; |
| 53 | } |
| 54 | else { |
| 55 | Close(); |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void RpcConnection::Close() |
| 61 | { |
no test coverage detected