• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef SOFTBUS_SOCKET_H
17 #define SOFTBUS_SOCKET_H
18 
19 #include <sys/types.h>
20 #include <sys/uio.h>
21 
22 #include "softbus_conn_interface.h"
23 #include "softbus_protocol_def.h"
24 
25 #ifdef __cplusplus
26 #if __cplusplus
27 extern "C" {
28 #endif
29 #endif
30 
31 #ifndef SOFTBUS_TEMP_FAILURE_RETRY
32 #define SOFTBUS_TEMP_FAILURE_RETRY(expression)              \
33     (__extension__({                                        \
34         long int __result;                                  \
35         do {                                                \
36             __result = (long int)(expression);              \
37         } while (__result == SOFTBUS_ADAPTER_SOCKET_EINTR); \
38         __result;                                           \
39     }))
40 #endif
41 
42 enum SocketEvent {
43     SOFTBUS_SOCKET_OUT,       // writable
44     SOFTBUS_SOCKET_IN,        // readable
45     SOFTBUS_SOCKET_EXCEPTION, // exception
46 };
47 
48 typedef struct {
49     char addr[MAX_SOCKET_ADDR_LEN];
50     int32_t port;
51 } SocketAddr;
52 
53 typedef struct SocketInterface {
54     const char *name;
55     const ProtocolType type;
56     int32_t (*GetSockPort)(int32_t fd);
57     int32_t (*OpenServerSocket)(const LocalListenerInfo *option);
58     int32_t (*OpenClientSocket)(const ConnectOption *option, const char *bindAddr, bool isNonBlock);
59     int32_t (*AcceptClient)(int32_t fd, ConnectOption *clientAddr, int32_t *cfd);
60 } SocketInterface;
61 
62 int32_t ConnInitSockets(void);
63 void ConnDeinitSockets(void);
64 
65 const SocketInterface *GetSocketInterface(ProtocolType protocolType);
66 
67 int32_t RegistSocketProtocol(const SocketInterface *interface);
68 
69 int32_t ConnOpenClientSocket(const ConnectOption *option, const char *bindAddr, bool isNonBlock);
70 
71 ssize_t ConnSendSocketData(int32_t fd, const char *buf, size_t len, int32_t timeout);
72 ssize_t ConnRecvSocketData(int32_t fd, char *buf, size_t len, int32_t timeout);
73 void ConnCloseSocket(int32_t fd);
74 void ConnShutdownSocket(int32_t fd);
75 int32_t ConnSetTcpKeepAlive(int32_t fd, int32_t seconds);
76 int32_t ConnSetTcpUserTimeOut(int32_t fd, uint32_t millSec);
77 
78 int32_t ConnToggleNonBlockMode(int32_t fd, bool isNonBlock);
79 int32_t ConnGetSocketError(int32_t fd);
80 int32_t ConnGetLocalSocketPort(int32_t fd);
81 int32_t ConnGetPeerSocketAddr(int32_t fd, SocketAddr *socketAddr);
82 
83 #ifdef __cplusplus
84 #if __cplusplus
85 }
86 #endif /* __cplusplus */
87 #endif /* __cplusplus */
88 
89 #endif
90