add websocket client interface sdk c

Signed-off-by: liuxiyao223 <liuxiyao223@huawei.com>
This commit is contained in:
liuxiyao223 2023-11-24 19:42:01 +08:00
parent f3d5062834
commit 92f488af5f
4 changed files with 468 additions and 0 deletions

View File

@ -0,0 +1,34 @@
# Copyright (c) 2023 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//build/ohos.gni")
ohos_ndk_library("libnet_websocket") {
output_name = "net_websocket"
output_extension = "so"
ndk_description_file = "./libwebsocket.ndk.json"
min_compact_version = "1"
system_capability = "SystemCapability.Communication.NetStack"
system_capability_headers = [
"network/netstack/net_websocket/net_websocket.h",
"network/netstack/net_websocket/net_websocket_type.h",
]
}
ohos_ndk_headers("websocket_header") {
dest_dir = "$ndk_headers_out_dir/network/netstack/net_websocket"
sources = [
"net_websocket.h",
"net_websocket_type.h",
]
}

View File

@ -0,0 +1,9 @@
[
{"name": "OH_NetStack_WebsocketClient_Construct"},
{"name": "OH_NetStack_WebSocketClient_AddHeader"},
{"name": "OH_NetStack_WebSocketClient_Connect"},
{"name": "OH_NetStack_WebSocketClient_Send"},
{"name": "OH_NetStack_WebSocketClient_Close"},
{"name": "OH_NetStack_WebsocketClient_Destroy"},
{"name": "OH_NetStack_WebSocketClient_FreeAll"}
]

View File

@ -0,0 +1,139 @@
/*
* Copyright (C) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef NET_WEBSOCKET_H
#define NET_WEBSOCKET_H
#include <signal.h>
#include <stdint.h>
#include <string.h>
/**
* @addtogroup netstack
* @{
*
* @brief Provides C APIs for the WebSocket client module.
* @since 11
* @version 1.0
*/
/**
* @file net_websocket.h
*
* @brief Defines the APIs for the WebSocket client module.
*
* @library libnet_websocket.so
* @syscap SystemCapability.Communication.NetStack
* @since 11
* @version 1.0
*/
#include "net_websocket_type.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Constructor of OH_NetStack_WebsocketClient.
*
* @param onMessage Callback function invoked when a message is received.
* @param onClose Callback function invoked when a connection closing message is closed.
* @param onError Callback function invoked when a connection error message is received.
* @param onOpen Callback function invoked when a connection setup message is received.
* @return Pointer to the WebSocket client if success; NULL otherwise.
* @syscap SystemCapability.Communication.NetStack
* @since 11
* @version 1.0
*/
struct OH_NetStack_WebsocketClient *OH_NetStack_WebsocketClient_Construct(
OH_NetStack_WebsocketClient_OnOpenCallback onOpen, OH_NetStack_WebsocketClient_OnMessageCallback onMessage,
OH_NetStack_WebsocketClient_OnErrorCallback onError, OH_NetStack_WebsocketClient_OnCloseCallback onclose);
/**
* @brief Adds the header information to the client request.
*
* @param client Pointer to the WebSocket client.
* @param header Header information
* @return 0 if success; non-0 otherwise. For details about error codes, see {@link OH_Websocket_ErrCode}.
* @syscap SystemCapability.Communication.NetStack
* @since 11
* @version 1.0
*/
int OH_NetStack_WebSocketClient_AddHeader(struct OH_NetStack_WebsocketClient *client,
struct OH_NetStack_WebsocketClient_Slist header);
/**
* @brief Connects the client to the server.
*
* @param client Pointer to the WebSocket client.
* @param url URL for the client to connect to the server.
* @param options Optional parameters.
* @return 0 if success; non-0 otherwise. For details about error codes, see {@link OH_Websocket_ErrCode}.
* @permission ohos.permission.INTERNET
* @syscap SystemCapability.Communication.NetStack
* @since 11
* @version 1.0
*/
int OH_NetStack_WebSocketClient_Connect(struct OH_NetStack_WebsocketClient *client, const char *url,
struct OH_NetStack_WebsocketClient_RequestOptions options);
/**
* @brief Sends data from the client to the server.
*
* @param client Pointer to the WebSocket client.
* @param data Data sent by the client.
* @param length Length of the data sent by the client.
* @return 0 if success; non-0 otherwise. For details about error codes, see {@link OH_Websocket_ErrCode}.
* @permission ohos.permission.INTERNET
* @syscap SystemCapability.Communication.NetStack
* @since 11
* @version 1.0
*/
int OH_NetStack_WebSocketClient_Send(struct OH_NetStack_WebsocketClient *client, char *data, size_t length);
/**
* @brief Closes a WebSocket connection.
*
* @param client Pointer to the WebSocket client.
* @param url URL for the client to connect to the server.
* @param options Optional parameters.
* @return 0 if success; non-0 otherwise. For details about error codes, see {@link OH_Websocket_ErrCode}.
* @permission ohos.permission.INTERNET
* @syscap SystemCapability.Communication.NetStack
* @since 11
* @version 1.0
*/
int OH_NetStack_WebSocketClient_Close(struct OH_NetStack_WebsocketClient *client,
struct OH_NetStack_WebsocketClient_CloseOption options);
/**
* @brief Releases the context and resources of the WebSocket connection.
*
* @param client Pointer to the WebSocket client.
* @return 0 if success; non-0 otherwise. For details about error codes, see {@link OH_Websocket_ErrCode}.
* @permission ohos.permission.INTERNET
* @syscap SystemCapability.Communication.NetStack
* @since 11
* @version 1.0
*/
int OH_NetStack_WebsocketClient_Destroy(struct OH_NetStack_WebsocketClient *client);
#ifdef __cplusplus
}
#endif
#endif // NET_WEBSOCKET_H

View File

@ -0,0 +1,286 @@
/*
* Copyright (C) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef NET_WEBSOCKET_TYPE_H
#define NET_WEBSOCKET_TYPE_H
/**
* @addtogroup netstack
* @{
*
* @brief Provides C APIs for the WebSocket client module.
*
* @since 11
* @version 1.0
*/
/**
* @file net_websocket_type.h
* @brief Defines the data structure for the C APIs of the WebSocket client module.
*
* @library libnet_websocket.so
* @syscap SystemCapability.Communication.NetStack
* @since 11
* @version 1.0
*/
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Defines the parameters for connection closing by the server.
*
* @since 11
* @version 1.0
*/
struct OH_NetStack_WebsocketClient_CloseResult {
/** Error code */
uint32_t code;
/** Error cause */
const char *reason;
};
/**
* @brief Defines the parameters for proactive connection closing by the client.
*
* @since 11
* @version 1.0
*/
struct OH_NetStack_WebsocketClient_CloseOption {
/** Error code */
uint32_t code;
/** Error cause */
const char *reason;
};
/**
* @brief Defines the parameters for the connection error reported by the server.
*
* @since 11
* @version 1.0
*/
struct OH_NetStack_WebsocketClient_ErrorResult {
/** Error code */
uint32_t errorCode;
/** Error message */
const char *errorMessage;
};
/**
* @brief Defines the parameters for the connection success reported by the server.
*
* @since 11
* @version 1.0
*/
struct OH_NetStack_WebsocketClient_OpenResult {
/** Connection success code */
uint32_t code;
/** Connection success reason */
const char *reason;
};
/**
* @brief Defines the callback function invoked when an <b>open</b> message is received.
*
* @param client WebSocket client.
* @param openResult Content of the <b>open</b> message received by the WebSocket client.
* @since 11
* @version 1.0
*/
typedef void (*OH_NetStack_WebsocketClient_OnOpenCallback)(struct OH_NetStack_WebsocketClient *client,
OH_NetStack_WebsocketClient_OpenResult openResult);
/**
* @brief Defines the callback function invoked when data is received.
*
* @param client WebSocket client.
* @param data Data received by the WebSocket client.
* @param length Length of the data received by the WebSocket client.
* @since 11
* @version 1.0
*/
typedef void (*OH_NetStack_WebsocketClient_OnMessageCallback)(struct OH_NetStack_WebsocketClient *client, char *data,
uint32_t length);
/**
* @brief Defines the callback function invoked when an error message is received.
*
* @param client WebSocket client.
* @param errorResult Content of the connection error message received by the WebSocket client.
* @since 11
* @version 1.0
*/
typedef void (*OH_NetStack_WebsocketClient_OnErrorCallback)(struct OH_NetStack_WebsocketClient *client,
OH_NetStack_WebsocketClient_ErrorResult errorResult);
/**
* @brief Defines the callback function invoked when a <b>close</b> message is received.
*
* @param client WebSocket client.
* @param closeResult Content of the <b>close</b> message received by the WebSocket client.
* @since 11
* @version 1.0
*/
typedef void (*OH_NetStack_WebsocketClient_OnCloseCallback)(struct OH_NetStack_WebsocketClient *client,
OH_NetStack_WebsocketClient_CloseResult closeResult);
/**
* @brief Adds the header linked list to the WebSocket client.
*
* @since 11
* @version 1.0
*/
struct OH_NetStack_WebsocketClient_Slist {
/** Header field name */
const char *fieldName;
/** Header field content */
const char *fieldValue;
/** Next pointer of the header linked list */
struct OH_NetStack_WebsocketClient_Slist *next;
};
/**
* @brief Defines the parameters for the connection between the WebSocket client and server.
*
* @param headers Header information.
* @since 11
* @version 1.0
*/
struct OH_NetStack_WebsocketClient_RequestOptions {
struct OH_NetStack_WebsocketClient_Slist *headers;
};
/**
* @brief Defines the WebSocket client structure.
*
* @since 11
* @version 1.0
*/
struct OH_NetStack_WebsocketClient {
/** Pointer to the callback invoked when a connection message is received */
OH_NetStack_WebsocketClient_OnOpenCallback onOpen;
/** Pointer to the callback invoked when a message is received */
OH_NetStack_WebsocketClient_OnMessageCallback onMessage;
/** Pointer to the callback invoked when an error message is received */
OH_NetStack_WebsocketClient_OnErrorCallback onError;
/** Pointer to the callback invoked when a close message is received */
OH_NetStack_WebsocketClient_OnCloseCallback onClose;
/** Content of the request for establishing a connection on the client */
OH_NetStack_WebsocketClient_RequestOptions requestOptions;
};
typedef enum OH_Websocket_ErrCode {
/**
* Operation success.
*/
WEBSOCKET_OK = 0,
/**
* @brief Error code base.
*/
E_BASE = 1000,
/**
* @brief The WebSocket client is null.
*/
WEBSOCKET_CLIENT_IS_NULL = (E_BASE + 1),
/**
* @brief A WebSocket client is not created.
*/
WEBSOCKET_CLIENT_IS_NOT_CREAT = (E_BASE + 2),
/**
* @brief An error occurs while setting up a WebSocket connection.
*/
WEBSOCKET_CONNECTION_ERROR = (E_BASE + 3),
/**
* @brief An error occurs while parsing WebSocket connection parameters.
*/
WEBSOCKET_CONNECTION_PARSEURL_ERROR = (E_BASE + 5),
/**
* @brief The memory is insufficient for creating a context during WebSocket connection setup.
*/
WEBSOCKET_CONNECTION_NO_MEMOERY = (E_BASE + 6),
/**
* @brief The WebSocket connection is closed by the peer.
*/
WEBSOCKET_PEER_INITIATED_CLOSE = (E_BASE + 7),
/**
* @brief The WebSocket connection is destroyed.
*/
WEBSOCKET_DESTROY = (E_BASE + 8),
/**
* @brief An incorrect protocol is used for WebSocket connection.
*/
WEBSOCKET_PROTOCOL_ERROR = (E_BASE + 9),
/**
* @brief The memory for the WebSocket client to send data is insufficient.
*/
WEBSOCKET_SEND_NO_MEMOERY_ERROR = (E_BASE + 10),
/**
* @brief The data sent by the WebSocket client is null.
*/
WEBSOCKET_SEND_DATA_NULL = (E_BASE + 11),
/**
* @brief The length of the data sent by the WebSocket client exceeds the limit.
*/
WEBSOCKET_DATA_LENGTH_EXCEEDS = (E_BASE + 12),
/**
* @brief The queue length of the data sent by the WebSocket client exceeds the limit.
*/
WEBSOCKET_QUEUE_LENGTH_EXCEEDS = (E_BASE + 13),
/**
* @brief The context of the WebSocket client is null.
*/
WEBSOCKET_ERROR_NO_CLIENTCONTEX = (E_BASE + 14),
/**
* @brief The header of the WebSocket client is null.
*/
WEBSOCKET_ERROR_NO_HEADR_CONTEXT = (E_BASE + 15),
/**
* @brief The header of the WebSocket client exceeds the limit.
*/
WEBSOCKET_ERROR_NO_HEADR_EXCEEDS = (E_BASE + 16),
/**
* @brief The WebSocket client is not connected.
*/
WEBSOCKET_ERROR_HAVE_NO_CONNECT = (E_BASE + 17),
/**
* @brief The WebSocket client does not have the connection context.
*/
WEBSOCKET_ERROR_HAVE_NO_CONNECT_CONTEXT = (E_BASE + 18),
} OH_Websocket_ErrCode;
#ifdef __cplusplus
}
#endif
#endif // NET_WEBSOCKET_TYPE_H