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 * @filename mt_cache.h 22 */ 23 24 #ifndef ___MT_BUFFER_CACHE_H 25 #define ___MT_BUFFER_CACHE_H 26 27 #include <stdint.h> 28 #include <sys/queue.h> 29 30 31 namespace NS_MICRO_THREAD { 32 33 #define SK_DFLT_BUFF_SIZE 64*1024 34 #define SK_DFLT_ALIGN_SIZE 8 35 36 #define SK_ERR_NEED_CLOSE 10000 37 38 39 typedef struct _sk_buffer_tag 40 { 41 TAILQ_ENTRY(_sk_buffer_tag) entry; 42 uint32_t last_time; 43 uint32_t size; 44 uint8_t* head; 45 uint8_t* end; 46 uint8_t* data; 47 uint32_t data_len; 48 uint8_t buff[0]; 49 } TSkBuffer; 50 typedef TAILQ_HEAD(__sk_buff_list, _sk_buffer_tag) TSkBuffList; 51 52 53 TSkBuffer* new_sk_buffer(uint32_t size = SK_DFLT_BUFF_SIZE); 54 55 void delete_sk_buffer(TSkBuffer* buff); 56 57 TSkBuffer* reserve_sk_buffer(TSkBuffer* buff, uint32_t size); 58 59 typedef struct _sk_buff_mng_tag 60 { 61 TSkBuffList free_list; 62 uint32_t expired; 63 uint32_t size; 64 uint32_t count; 65 } TSkBuffMng; 66 67 void sk_buffer_mng_init(TSkBuffMng* mng, uint32_t expired, uint32_t size = SK_DFLT_BUFF_SIZE); 68 69 void sk_buffer_mng_destroy(TSkBuffMng * mng); 70 71 TSkBuffer* alloc_sk_buffer(TSkBuffMng* mng); 72 73 void free_sk_buffer(TSkBuffMng* mng, TSkBuffer* buff); 74 75 void recycle_sk_buffer(TSkBuffMng* mng, uint32_t now); 76 77 typedef struct _sk_rw_cache_tag 78 { 79 TSkBuffList list; 80 uint32_t len; 81 uint32_t count; 82 TSkBuffMng *pool; 83 } TRWCache; 84 85 void rw_cache_init(TRWCache* cache, TSkBuffMng* pool); 86 87 void rw_cache_destroy(TRWCache* cache); 88 89 void cache_skip_data(TRWCache* cache, uint32_t len); 90 91 TSkBuffer* cache_skip_first_buffer(TRWCache* cache); 92 93 int32_t cache_append_data(TRWCache* cache, const void* data, uint32_t len); 94 95 void cache_append_buffer(TRWCache* cache, TSkBuffer* buff); 96 97 uint32_t cache_copy_out(TRWCache* cache, void* buff, uint32_t len); 98 99 int32_t cache_udp_recv(TRWCache* cache, uint32_t fd, struct sockaddr_in* remote_addr); 100 101 int32_t cache_tcp_recv(TRWCache* cache, uint32_t fd); 102 103 int32_t cache_tcp_send(TRWCache* cache, uint32_t fd); 104 105 int32_t cache_tcp_send_buff(TRWCache* cache, uint32_t fd, const void* data, uint32_t len); 106 107 108 // interface 109 typedef void* TBuffVecPtr; 110 typedef void* TBuffBlockPtr; 111 112 uint32_t get_data_len(TBuffVecPtr multi); 113 114 uint32_t get_block_count(TBuffVecPtr multi); 115 116 TBuffBlockPtr get_first_block(TBuffVecPtr multi); 117 118 TBuffBlockPtr get_next_block(TBuffVecPtr multi, TBuffBlockPtr block); 119 120 void get_block_data(TBuffBlockPtr block, const void** data, int32_t* len); 121 122 uint32_t read_cache_data(TBuffVecPtr multi, void* data, uint32_t len); 123 124 uint32_t read_cache_begin(TBuffVecPtr multi, uint32_t begin, void* data, uint32_t len); 125 126 }; 127 128 #endif 129