|
47 | 47 |
|
48 | 48 | #include "clientrpc.h" |
49 | 49 |
|
| 50 | +static QString ConnectionStateToString ( const EConnectionState eState ) |
| 51 | +{ |
| 52 | + switch ( eState ) |
| 53 | + { |
| 54 | + case CS_CONNECTING: |
| 55 | + return "connecting"; |
| 56 | + |
| 57 | + case CS_CONNECTED: |
| 58 | + return "connected"; |
| 59 | + |
| 60 | + default: |
| 61 | + return "disconnected"; |
| 62 | + } |
| 63 | +} |
| 64 | + |
50 | 65 | CClientRpc::CClientRpc ( CClient* pClient, CClientSettings* pSettings, CRpcServer* pRpcServer, QObject* parent ) : |
51 | 66 | QObject ( parent ), |
52 | 67 | m_pSettings ( pSettings ) |
@@ -168,6 +183,30 @@ CClientRpc::CClientRpc ( CClient* pClient, CClientSettings* pSettings, CRpcServe |
168 | 183 | /// @param {object} params - No parameters (empty object). |
169 | 184 | connect ( pClient, &CClient::Disconnected, [=]() { pRpcServer->BroadcastNotification ( "jamulusclient/disconnected", QJsonObject{} ); } ); |
170 | 185 |
|
| 186 | + // A failed attempt surfaces through connectionStateChanged with the error attached. |
| 187 | + // serverName is omitted here: an invalid address fails before the name is set. |
| 188 | + connect ( pClient, &CClient::ConnectingFailed, [=] ( QString strError ) { |
| 189 | + pRpcServer->BroadcastNotification ( "jamulusclient/connectionStateChanged", |
| 190 | + QJsonObject{ |
| 191 | + { "state", ConnectionStateToString ( CS_DISCONNECTED ) }, |
| 192 | + { "error", strError }, |
| 193 | + } ); |
| 194 | + } ); |
| 195 | + |
| 196 | + /// @rpc_notification jamulusclient/connectionStateChanged |
| 197 | + /// @brief Emitted whenever the connection state changes. On a failed connection attempt it is |
| 198 | + /// emitted with state "disconnected" and an additional error field. |
| 199 | + /// @param {string} params.state - The new connection state (disconnected, connecting, or connected). |
| 200 | + /// @param {string} params.serverName - The human readable server name (empty/absent when disconnected). |
| 201 | + /// @param {string} params.error - Only present on a failed connection attempt (with state "disconnected"); serverName is omitted in that case. |
| 202 | + connect ( pClient, &CClient::ConnectionStateChanged, [=] ( EConnectionState eState ) { |
| 203 | + pRpcServer->BroadcastNotification ( "jamulusclient/connectionStateChanged", |
| 204 | + QJsonObject{ |
| 205 | + { "state", ConnectionStateToString ( eState ) }, |
| 206 | + { "serverName", eState == CS_DISCONNECTED ? QString() : pClient->GetConnectedServerName() }, |
| 207 | + } ); |
| 208 | + } ); |
| 209 | + |
171 | 210 | /// @rpc_notification jamulusclient/recorderState |
172 | 211 | /// @brief Emitted when the client is connected to a server whose recorder state changes. |
173 | 212 | /// @param {number} params.state - The recorder state. |
@@ -212,6 +251,76 @@ CClientRpc::CClientRpc ( CClient* pClient, CClientSettings* pSettings, CRpcServe |
212 | 251 | Q_UNUSED ( params ); |
213 | 252 | } ); |
214 | 253 |
|
| 254 | + /// @rpc_method jamulusclient/requestConnection |
| 255 | + /// @brief Connects the client to a server. Any current connection is terminated first. |
| 256 | + /// The connection is established asynchronously: subscribe to the jamulusclient/connected |
| 257 | + /// and jamulusclient/connectionStateChanged notifications to follow its progress (a failed |
| 258 | + /// attempt arrives as connectionStateChanged with state "disconnected" and an error field). |
| 259 | + /// An address that cannot be resolved is rejected with an error and leaves the current |
| 260 | + /// connection untouched. |
| 261 | + /// @param {string} params.address - Socket address of the server (host:port). |
| 262 | + /// @param {string} params.serverName - Optional human readable server name used for display purposes; if given it must be a string |
| 263 | + /// (null counts as omitted). Defaults to the address. |
| 264 | + /// @result {string} result - "ok" once the connection attempt has been initiated. |
| 265 | + pRpcServer->HandleMethod ( "jamulusclient/requestConnection", [=] ( const QJsonObject& params, QJsonObject& response ) { |
| 266 | + auto jsonAddress = params["address"]; |
| 267 | + if ( !jsonAddress.isString() ) |
| 268 | + { |
| 269 | + response["error"] = CRpcServer::CreateJsonRpcError ( CRpcServer::iErrInvalidParams, "Invalid params: address is not a string" ); |
| 270 | + return; |
| 271 | + } |
| 272 | + |
| 273 | + auto jsonServerName = params["serverName"]; |
| 274 | + if ( !jsonServerName.isUndefined() && !jsonServerName.isNull() && !jsonServerName.isString() ) |
| 275 | + { |
| 276 | + response["error"] = CRpcServer::CreateJsonRpcError ( CRpcServer::iErrInvalidParams, "Invalid params: serverName is not a string" ); |
| 277 | + return; |
| 278 | + } |
| 279 | + |
| 280 | + const QString strAddress = NetworkUtil::FixAddress ( jsonAddress.toString() ); |
| 281 | + const QString strServerName = jsonServerName.isString() ? jsonServerName.toString() : strAddress; |
| 282 | + |
| 283 | + // resolve here so that the caller gets an error result for an invalid address |
| 284 | + CHostAddress haServer; |
| 285 | + if ( !NetworkUtil::ParseNetworkAddress ( strAddress, haServer, pClient->IsIPv6Available() ) ) |
| 286 | + { |
| 287 | + response["error"] = |
| 288 | + CRpcServer::CreateJsonRpcError ( CRpcServer::iErrInvalidParams, "Invalid params: address is not a valid socket address" ); |
| 289 | + return; |
| 290 | + } |
| 291 | + |
| 292 | + pClient->Connect ( haServer, strServerName ); |
| 293 | + |
| 294 | + response["result"] = "ok"; |
| 295 | + } ); |
| 296 | + |
| 297 | + /// @rpc_method jamulusclient/disconnect |
| 298 | + /// @brief Disconnects the client from the current server. Does nothing if the client is not connected. |
| 299 | + /// @param {object} params - No parameters (empty object). |
| 300 | + /// @result {string} result - Always "ok". |
| 301 | + pRpcServer->HandleMethod ( "jamulusclient/disconnect", [=] ( const QJsonObject& params, QJsonObject& response ) { |
| 302 | + pClient->Disconnect(); |
| 303 | + |
| 304 | + response["result"] = "ok"; |
| 305 | + Q_UNUSED ( params ); |
| 306 | + } ); |
| 307 | + |
| 308 | + /// @rpc_method jamulusclient/getConnectionState |
| 309 | + /// @brief Returns the current connection state. |
| 310 | + /// @param {object} params - No parameters (empty object). |
| 311 | + /// @result {string} result.state - The connection state (disconnected, connecting, or connected). |
| 312 | + /// @result {string} result.serverName - The human readable name of the current server (empty if disconnected). |
| 313 | + pRpcServer->HandleMethod ( "jamulusclient/getConnectionState", [=] ( const QJsonObject& params, QJsonObject& response ) { |
| 314 | + const EConnectionState eState = pClient->GetConnectionState(); |
| 315 | + |
| 316 | + QJsonObject result{ |
| 317 | + { "state", ConnectionStateToString ( eState ) }, |
| 318 | + { "serverName", eState == CS_DISCONNECTED ? QString() : pClient->GetConnectedServerName() }, |
| 319 | + }; |
| 320 | + response["result"] = result; |
| 321 | + Q_UNUSED ( params ); |
| 322 | + } ); |
| 323 | + |
215 | 324 | /// @rpc_method jamulus/getMode |
216 | 325 | /// @brief Returns the current mode, i.e. whether Jamulus is running as a server or client. |
217 | 326 | /// @param {object} params - No parameters (empty object). |
|
0 commit comments