xref: /f-stack/app/micro_thread/mt_net_api.h (revision 45438af0)
1 
2 /**
3  * Tencent is pleased to support the open source community by making MSEC available.
4  *
5  * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
6  *
7  * Licensed under the GNU General Public License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License. You may
9  * obtain a copy of the License at
10  *
11  *     https://opensource.org/licenses/GPL-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software distributed under the
14  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
15  * either express or implied. See the License for the specific language governing permissions
16  * and limitations under the License.
17  */
18 
19 
20 /**
21  *  @file mt_net_api.h
22  **/
23 
24 #ifndef __MT_NET_API_H__
25 #define __MT_NET_API_H__
26 
27 #include <netinet/in.h>
28 
29 namespace NS_MICRO_THREAD {
30 
31 enum MT_PROTO_TYPE
32 {
33     NET_PROTO_UNDEF      = 0,
34     NET_PROTO_UDP        = 0x1,
35     NET_PROTO_TCP        = 0x2
36 };
37 
38 enum MT_RC_TYPE
39 {
40     RC_SUCCESS          = 0,
41     RC_ERR_SOCKET       = -1,
42     RC_SEND_FAIL        = -2,
43     RC_RECV_FAIL        = -3,
44     RC_CONNECT_FAIL     = -4,
45     RC_CHECK_PKG_FAIL   = -5,
46     RC_NO_MORE_BUFF     = -6,
47     RC_REMOTE_CLOSED    = -7,
48 
49     RC_INVALID_PARAM    = -10,
50     RC_INVALID_HANDLER  = -11,
51     RC_MEM_ERROR        = -12,
52     RC_CONFLICT_SID     = -13,
53     RC_KQUEUE_ERROR     = -14,
54 };
55 
56 typedef int32_t (*CHECK_SESSION_CALLBACK)(const void* data, uint32_t len,
57                                         uint64_t* session_id, uint32_t* need_len);
58 
59 class CNetHelper
60 {
61 public:
62 
63     static char* GetErrMsg(int32_t result);
64 
65     int32_t SendRecv(void* data, uint32_t len, uint32_t timeout);
66 
67     void* GetRspBuff();
68 
69     uint32_t GetRspLen();
70 
71     void SetProtoType(MT_PROTO_TYPE type);
72 
73     void SetDestAddress(struct sockaddr_in* dst);
74 
75     void SetSessionId(uint64_t sid);
76 
77     void SetSessionCallback(CHECK_SESSION_CALLBACK function);
78 
79     CNetHelper();
80     ~CNetHelper();
81 
82 private:
83 
84     void*    handler;
85 };
86 
87 
88 }
89 
90 #endif
91 
92 
93