1 /*
2  * Copyright (C) 2021-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 CRPC_CONTEXT_H
17 #define CRPC_CONTEXT_H
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 #define CONTEXT_BUFFER_MIN_SIZE (1024)
24 #define CONTEXT_BUFFER_MAX_SIZE (1024000)
25 
26 typedef struct Context Context;
27 struct Context {
28     int fd;
29     char *szRead;        /* read buffer */
30     unsigned int rCapacity;       /* read buffer size */
31     unsigned int rBegin;          /* current read pos */
32     unsigned int rEnd;            /* current read end pos */
33     char *szWrite;       /* write buffer */
34     unsigned int wCapacity;       /* write buffer size */
35     unsigned int wBegin;          /* current write pos */
36     unsigned int wEnd;            /* current write end pos */
37     char *oneProcess;    /* when deal message, copy message into here */
38     unsigned int nPos;            /* deal message, read pos */
39     unsigned int nSize;           /* deal message's total size */
40     char cSplit;         /* message split character flag */
41     const char *cMsgEnd; /* message end characters flag */
42 };
43 
44 /**
45  * @Description Create a Context object
46  *
47  * @param capacity - Context buffer size
48  * @return Context* - context pointer or NULL if failed
49  */
50 Context *CreateContext(int capacity);
51 
52 /**
53  * @Description Release Context
54  *
55  * @param context - Context object's pointer
56  */
57 void ReleaseContext(Context *context);
58 
59 /**
60  * @Description Read message
61  *
62  * @param context - Context object's pointer
63  * @return int - >= 0 success; other read failed
64  */
65 int ContextReadNet(Context *context);
66 
67 /**
68  * @Description Write message
69  *
70  * @param context - Context object's pointer
71  * @return int - Number of records which is written.
72  */
73 int ContextWriteNet(Context *context);
74 
75 /**
76  * @Description Append buff into context's write cache
77  *
78  * @param context - Context object's pointer
79  * @param buf - input message buffer
80  * @param len - message size
81  * @return int - 0 success; -1 append failed
82  */
83 int ContextAppendWrite(Context *context, const char *buf, int len);
84 
85 /**
86  * @Description Get message from context read cache. When context read from network,
87  *              it first appends read message into it's read cache, and then judge if
88  *              has read a complete message. This function returns the message.
89  *
90  * @param context - Context object's pointer
91  * @return char* - a complete message or NULL if not exist
92  */
93 char *ContextGetReadRecord(Context *context);
94 
95 #ifdef __cplusplus
96 }
97 #endif
98 #endif
99