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