Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
慢慢CG
TDengine
提交
9fdf3455
T
TDengine
项目概览
慢慢CG
/
TDengine
与 Fork 源项目一致
Fork自
taosdata / TDengine
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
9fdf3455
编写于
4月 27, 2020
作者:
weixin_48148422
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
split SBuffer to Reader & Writer
上级
058c8912
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
317 addition
and
414 deletion
+317
-414
src/util/inc/tbuffer.h
src/util/inc/tbuffer.h
+91
-149
src/util/src/tbuffer.c
src/util/src/tbuffer.c
+226
-265
未找到文件。
src/util/inc/tbuffer.h
浏览文件 @
9fdf3455
...
@@ -23,160 +23,102 @@
...
@@ -23,160 +23,102 @@
extern
"C"
{
extern
"C"
{
#endif
#endif
/*
typedef
struct
{
// SBuffer can be used to read or write a buffer, but cannot be used for both
bool
endian
;
// read & write at a same time. Below is an example:
const
char
*
data
;
#include <stdio.h>
size_t
pos
;
#include <stdlib.h>
size_t
size
;
#include "exception.h"
}
SBufferReader
;
#include "tbuffer.h"
int foo() {
SBuffer wbuf, rbuf;
tbufSetup(&wbuf, NULL, false);
tbufSetup(&rbuf, NULL, false);
TRY {
//--------------------- write ------------------------
tbufBeginWrite(&wbuf);
// reserve 1024 bytes for the buffer to improve performance
tbufEnsureCapacity(&wbuf, 1024);
// write 5 integers to the buffer
for (int i = 0; i < 5; i++) {
tbufWriteInt32(&wbuf, i);
}
// write a string to the buffer
tbufWriteString(&wbuf, "this is a string.\n");
// acquire the result and close the write buffer
size_t size = tbufTell(&wbuf);
char* data = tbufGetData(&wbuf, true);
//------------------------ read -----------------------
tbufBeginRead(&rbuf, data, size);
// read & print out 5 integers
for (int i = 0; i < 5; i++) {
printf("%d\n", tbufReadInt32(&rbuf));
}
// read & print out a string
puts(tbufReadString(&rbuf, NULL));
// try read another integer, this result in an error as there no this integer
tbufReadInt32(&rbuf);
printf("you should not see this message.\n");
} CATCH( code ) {
printf("exception code is: %d, you will see this message after print out 5 integers and a string.\n", code);
THROW( code );
} END_CATCH
tbufClose(&wbuf, true);
tbufClose(&rbuf, false);
return 0;
}
int main(int argc, char** argv) {
TRY {
printf("in main: you will see this line\n");
foo();
printf("in main: you will not see this line\n");
} CATCH( code ) {
printf("foo raise an exception with code %d\n", code);
} END_CATCH
return 0;
}
*/
typedef
struct
{
typedef
struct
{
void
*
(
*
allocator
)(
void
*
,
size_t
);
bool
endian
;
bool
endian
;
char
*
data
;
char
*
data
;
size_t
pos
;
size_t
pos
;
size_t
size
;
size_t
size
;
}
SBuffer
;
void
*
(
*
allocator
)(
void
*
,
size_t
);
}
SBufferWriter
;
// common functions can be used in both read & write
////////////////////////////////////////////////////////////////////////////////
// tbufSetup setup the buffer, should be called before tbufBeginRead / tbufBeginWrite
// common functions & macros for both reader & writer
// *allocator*, function to allocate memory, will use 'realloc' if NULL
#define tbufTell( buf ) ((buf)->pos)
// *endian*, if true, read/write functions of primitive types will do 'ntoh' or 'hton' automatically
void
tbufSetup
(
SBuffer
*
buf
,
void
*
(
*
allocator
)(
void
*
,
size_t
),
bool
endian
);
size_t
tbufTell
(
SBuffer
*
buf
);
////////////////////////////////////////////////////////////////////////////////
size_t
tbufSeekTo
(
SBuffer
*
buf
,
size_t
pos
);
// reader functions & macros
void
tbufClose
(
SBuffer
*
buf
,
bool
keepData
);
// *Endian*, if true, reader functions of primitive types will do 'ntoh' automatically
// basic read functions
#define tbufInitReader( Data, Size, Endian ) {.endian = (Endian), .data = (Data), .pos = 0, .size = ((Data) == NULL ? 0 :(Size))}
void
tbufBeginRead
(
SBuffer
*
buf
,
void
*
data
,
size_t
len
);
size_t
tbufSkip
(
SBuffer
*
buf
,
size_t
size
);
size_t
tbufSkip
(
SBufferReader
*
buf
,
size_t
size
);
char
*
tbufRead
(
SBuffer
*
buf
,
size_t
size
);
void
tbufReadToBuffer
(
SBuffer
*
buf
,
void
*
dst
,
size_t
size
);
char
*
tbufRead
(
SBufferReader
*
buf
,
size_t
size
);
const
char
*
tbufReadString
(
SBuffer
*
buf
,
size_t
*
len
);
void
tbufReadToBuffer
(
SBufferReader
*
buf
,
void
*
dst
,
size_t
size
);
size_t
tbufReadToString
(
SBuffer
*
buf
,
char
*
dst
,
size_t
size
);
const
char
*
tbufReadString
(
SBufferReader
*
buf
,
size_t
*
len
);
const
char
*
tbufReadBinary
(
SBuffer
*
buf
,
size_t
*
len
);
size_t
tbufReadToString
(
SBufferReader
*
buf
,
char
*
dst
,
size_t
size
);
size_t
tbufReadToBinary
(
SBuffer
*
buf
,
void
*
dst
,
size_t
size
);
const
char
*
tbufReadBinary
(
SBufferReader
*
buf
,
size_t
*
len
);
size_t
tbufReadToBinary
(
SBufferReader
*
buf
,
void
*
dst
,
size_t
size
);
// basic write functions
void
tbufBeginWrite
(
SBuffer
*
buf
);
bool
tbufReadBool
(
SBufferReader
*
buf
);
void
tbufEnsureCapacity
(
SBuffer
*
buf
,
size_t
size
);
char
tbufReadChar
(
SBufferReader
*
buf
);
size_t
tbufReserve
(
SBuffer
*
buf
,
size_t
size
);
int8_t
tbufReadInt8
(
SBufferReader
*
buf
);
char
*
tbufGetData
(
SBuffer
*
buf
,
bool
takeOver
);
uint8_t
tbufReadUint8
(
SBufferReader
*
buf
);
void
tbufWrite
(
SBuffer
*
buf
,
const
void
*
data
,
size_t
size
);
int16_t
tbufReadInt16
(
SBufferReader
*
buf
);
void
tbufWriteAt
(
SBuffer
*
buf
,
size_t
pos
,
const
void
*
data
,
size_t
size
);
uint16_t
tbufReadUint16
(
SBufferReader
*
buf
);
void
tbufWriteStringLen
(
SBuffer
*
buf
,
const
char
*
str
,
size_t
len
);
int32_t
tbufReadInt32
(
SBufferReader
*
buf
);
void
tbufWriteString
(
SBuffer
*
buf
,
const
char
*
str
);
uint32_t
tbufReadUint32
(
SBufferReader
*
buf
);
// the prototype of WriteBinary and Write is identical
int64_t
tbufReadInt64
(
SBufferReader
*
buf
);
// the difference is: WriteBinary writes the length of the data to the buffer
uint64_t
tbufReadUint64
(
SBufferReader
*
buf
);
float
tbufReadFloat
(
SBufferReader
*
buf
);
double
tbufReadDouble
(
SBufferReader
*
buf
);
////////////////////////////////////////////////////////////////////////////////
// writer functions & macros
// *Allocator*, function to allocate memory, will use 'realloc' if NULL
// *Endian*, if true, writer functions of primitive types will do 'hton' automatically
#define tbufInitWriter( Allocator, Endian ) {.endian = (Endian), .data = NULL, .pos = 0, .size = 0, .allocator = ((Allocator) == NULL ? realloc : (Allocator))}
void
tbufCloseWriter
(
SBufferWriter
*
buf
);
void
tbufEnsureCapacity
(
SBufferWriter
*
buf
,
size_t
size
);
size_t
tbufReserve
(
SBufferWriter
*
buf
,
size_t
size
);
char
*
tbufGetData
(
SBufferWriter
*
buf
,
bool
takeOver
);
void
tbufWrite
(
SBufferWriter
*
buf
,
const
void
*
data
,
size_t
size
);
void
tbufWriteAt
(
SBufferWriter
*
buf
,
size_t
pos
,
const
void
*
data
,
size_t
size
);
void
tbufWriteStringLen
(
SBufferWriter
*
buf
,
const
char
*
str
,
size_t
len
);
void
tbufWriteString
(
SBufferWriter
*
buf
,
const
char
*
str
);
// the prototype of tbufWriteBinary and tbufWrite are identical
// the difference is: tbufWriteBinary writes the length of the data to the buffer
// first, then the actual data, which means the reader don't need to know data
// first, then the actual data, which means the reader don't need to know data
// size before read. Write only write the data itself, which means the reader
// size before read. Write only write the data itself, which means the reader
// need to know data size before read.
// need to know data size before read.
void
tbufWriteBinary
(
SBuffer
*
buf
,
const
void
*
data
,
size_t
len
);
void
tbufWriteBinary
(
SBufferWriter
*
buf
,
const
void
*
data
,
size_t
len
);
// read / write functions for primitive types
void
tbufWriteBool
(
SBufferWriter
*
buf
,
bool
data
);
bool
tbufReadBool
(
SBuffer
*
buf
);
void
tbufWriteBoolAt
(
SBufferWriter
*
buf
,
size_t
pos
,
bool
data
);
void
tbufWriteBool
(
SBuffer
*
buf
,
bool
data
);
void
tbufWriteChar
(
SBufferWriter
*
buf
,
char
data
);
void
tbufWriteBoolAt
(
SBuffer
*
buf
,
size_t
pos
,
bool
data
);
void
tbufWriteCharAt
(
SBufferWriter
*
buf
,
size_t
pos
,
char
data
);
void
tbufWriteInt8
(
SBufferWriter
*
buf
,
int8_t
data
);
char
tbufReadChar
(
SBuffer
*
buf
);
void
tbufWriteInt8At
(
SBufferWriter
*
buf
,
size_t
pos
,
int8_t
data
);
void
tbufWriteChar
(
SBuffer
*
buf
,
char
data
);
void
tbufWriteUint8
(
SBufferWriter
*
buf
,
uint8_t
data
);
void
tbufWriteCharAt
(
SBuffer
*
buf
,
size_t
pos
,
char
data
);
void
tbufWriteUint8At
(
SBufferWriter
*
buf
,
size_t
pos
,
uint8_t
data
);
void
tbufWriteInt16
(
SBufferWriter
*
buf
,
int16_t
data
);
int8_t
tbufReadInt8
(
SBuffer
*
buf
);
void
tbufWriteInt16At
(
SBufferWriter
*
buf
,
size_t
pos
,
int16_t
data
);
void
tbufWriteInt8
(
SBuffer
*
buf
,
int8_t
data
);
void
tbufWriteUint16
(
SBufferWriter
*
buf
,
uint16_t
data
);
void
tbufWriteInt8At
(
SBuffer
*
buf
,
size_t
pos
,
int8_t
data
);
void
tbufWriteUint16At
(
SBufferWriter
*
buf
,
size_t
pos
,
uint16_t
data
);
void
tbufWriteInt32
(
SBufferWriter
*
buf
,
int32_t
data
);
uint8_t
tbufReadUint8
(
SBuffer
*
buf
);
void
tbufWriteInt32At
(
SBufferWriter
*
buf
,
size_t
pos
,
int32_t
data
);
void
tbufWriteUint8
(
SBuffer
*
buf
,
uint8_t
data
);
void
tbufWriteUint32
(
SBufferWriter
*
buf
,
uint32_t
data
);
void
tbufWriteUint8At
(
SBuffer
*
buf
,
size_t
pos
,
uint8_t
data
);
void
tbufWriteUint32At
(
SBufferWriter
*
buf
,
size_t
pos
,
uint32_t
data
);
void
tbufWriteInt64
(
SBufferWriter
*
buf
,
int64_t
data
);
int16_t
tbufReadInt16
(
SBuffer
*
buf
);
void
tbufWriteInt64At
(
SBufferWriter
*
buf
,
size_t
pos
,
int64_t
data
);
void
tbufWriteInt16
(
SBuffer
*
buf
,
int16_t
data
);
void
tbufWriteUint64
(
SBufferWriter
*
buf
,
uint64_t
data
);
void
tbufWriteInt16At
(
SBuffer
*
buf
,
size_t
pos
,
int16_t
data
);
void
tbufWriteUint64At
(
SBufferWriter
*
buf
,
size_t
pos
,
uint64_t
data
);
void
tbufWriteFloat
(
SBufferWriter
*
buf
,
float
data
);
uint16_t
tbufReadUint16
(
SBuffer
*
buf
);
void
tbufWriteFloatAt
(
SBufferWriter
*
buf
,
size_t
pos
,
float
data
);
void
tbufWriteUint16
(
SBuffer
*
buf
,
uint16_t
data
);
void
tbufWriteDouble
(
SBufferWriter
*
buf
,
double
data
);
void
tbufWriteUint16At
(
SBuffer
*
buf
,
size_t
pos
,
uint16_t
data
);
void
tbufWriteDoubleAt
(
SBufferWriter
*
buf
,
size_t
pos
,
double
data
);
int32_t
tbufReadInt32
(
SBuffer
*
buf
);
void
tbufWriteInt32
(
SBuffer
*
buf
,
int32_t
data
);
void
tbufWriteInt32At
(
SBuffer
*
buf
,
size_t
pos
,
int32_t
data
);
uint32_t
tbufReadUint32
(
SBuffer
*
buf
);
void
tbufWriteUint32
(
SBuffer
*
buf
,
uint32_t
data
);
void
tbufWriteUint32At
(
SBuffer
*
buf
,
size_t
pos
,
uint32_t
data
);
int64_t
tbufReadInt64
(
SBuffer
*
buf
);
void
tbufWriteInt64
(
SBuffer
*
buf
,
int64_t
data
);
void
tbufWriteInt64At
(
SBuffer
*
buf
,
size_t
pos
,
int64_t
data
);
uint64_t
tbufReadUint64
(
SBuffer
*
buf
);
void
tbufWriteUint64
(
SBuffer
*
buf
,
uint64_t
data
);
void
tbufWriteUint64At
(
SBuffer
*
buf
,
size_t
pos
,
uint64_t
data
);
float
tbufReadFloat
(
SBuffer
*
buf
);
void
tbufWriteFloat
(
SBuffer
*
buf
,
float
data
);
void
tbufWriteFloatAt
(
SBuffer
*
buf
,
size_t
pos
,
float
data
);
double
tbufReadDouble
(
SBuffer
*
buf
);
void
tbufWriteDouble
(
SBuffer
*
buf
,
double
data
);
void
tbufWriteDoubleAt
(
SBuffer
*
buf
,
size_t
pos
,
double
data
);
#ifdef __cplusplus
#ifdef __cplusplus
}
}
...
...
src/util/src/tbuffer.c
浏览文件 @
9fdf3455
...
@@ -22,417 +22,378 @@
...
@@ -22,417 +22,378 @@
#include <taoserror.h>
#include <taoserror.h>
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// common functions
// reader functions
void
tbufSetup
(
SBuffer
*
buf
,
void
*
(
*
allocator
)(
void
*
,
size_t
),
bool
endian
)
{
if
(
allocator
!=
NULL
)
{
buf
->
allocator
=
allocator
;
}
else
{
buf
->
allocator
=
realloc
;
}
buf
->
endian
=
endian
;
}
size_t
tbufTell
(
SBuffer
*
buf
)
{
return
buf
->
pos
;
}
size_t
tbufS
eekTo
(
SBuffer
*
buf
,
size_t
pos
)
{
size_t
tbufS
kip
(
SBufferReader
*
buf
,
size_t
size
)
{
if
(
pos
>
buf
->
size
)
{
if
(
(
buf
->
pos
+
size
)
>
buf
->
size
)
{
THROW
(
TSDB_CODE_MEMORY_CORRUPTED
);
THROW
(
TSDB_CODE_MEMORY_CORRUPTED
);
}
}
size_t
old
=
buf
->
pos
;
size_t
old
=
buf
->
pos
;
buf
->
pos
=
pos
;
buf
->
pos
+=
size
;
return
old
;
return
old
;
}
}
void
tbufClose
(
SBuffer
*
buf
,
bool
keepData
)
{
char
*
tbufRead
(
SBufferReader
*
buf
,
size_t
size
)
{
if
(
!
keepData
)
{
(
*
buf
->
allocator
)(
buf
->
data
,
0
);
}
buf
->
data
=
NULL
;
buf
->
pos
=
0
;
buf
->
size
=
0
;
}
////////////////////////////////////////////////////////////////////////////////
// read functions
void
tbufBeginRead
(
SBuffer
*
buf
,
void
*
data
,
size_t
len
)
{
buf
->
data
=
data
;
buf
->
pos
=
0
;
buf
->
size
=
(
data
==
NULL
)
?
0
:
len
;
}
size_t
tbufSkip
(
SBuffer
*
buf
,
size_t
size
)
{
return
tbufSeekTo
(
buf
,
buf
->
pos
+
size
);
}
char
*
tbufRead
(
SBuffer
*
buf
,
size_t
size
)
{
char
*
ret
=
buf
->
data
+
buf
->
pos
;
char
*
ret
=
buf
->
data
+
buf
->
pos
;
tbufSkip
(
buf
,
size
);
tbufSkip
(
buf
,
size
);
return
ret
;
return
ret
;
}
}
void
tbufReadToBuffer
(
SBuffer
*
buf
,
void
*
dst
,
size_t
size
)
{
void
tbufReadToBuffer
(
SBufferReader
*
buf
,
void
*
dst
,
size_t
size
)
{
assert
(
dst
!=
NULL
);
assert
(
dst
!=
NULL
);
// always using memcpy, leave optimization to compiler
// always using memcpy, leave optimization to compiler
memcpy
(
dst
,
tbufRead
(
buf
,
size
),
size
);
memcpy
(
dst
,
tbufRead
(
buf
,
size
),
size
);
}
}
static
size_t
tbufReadLength
(
SBuffer
*
buf
)
{
static
size_t
tbufReadLength
(
SBufferReader
*
buf
)
{
// maximum length is 65535, if larger length is required
// maximum length is 65535, if larger length is required
// this function and the corresponding write function need to be
// this function and the corresponding write function need to be
// revised.
// revised.
uint16_t
l
=
tbufReadUint16
(
buf
);
uint16_t
l
=
tbufReadUint16
(
buf
);
return
l
;
return
l
;
}
}
const
char
*
tbufReadString
(
SBuffer
*
buf
,
size_t
*
len
)
{
const
char
*
tbufReadString
(
SBufferReader
*
buf
,
size_t
*
len
)
{
size_t
l
=
tbufReadLength
(
buf
);
size_t
l
=
tbufReadLength
(
buf
);
char
*
ret
=
buf
->
data
+
buf
->
pos
;
char
*
ret
=
buf
->
data
+
buf
->
pos
;
tbufSkip
(
buf
,
l
+
1
);
tbufSkip
(
buf
,
l
+
1
);
ret
[
l
]
=
0
;
// ensure the string end with '\0'
if
(
ret
[
l
]
!=
0
)
{
if
(
len
!=
NULL
)
{
THROW
(
TSDB_CODE_MEMORY_CORRUPTED
);
}
if
(
len
!=
NULL
)
{
*
len
=
l
;
*
len
=
l
;
}
}
return
ret
;
return
ret
;
}
}
size_t
tbufReadToString
(
SBuffer
*
buf
,
char
*
dst
,
size_t
size
)
{
size_t
tbufReadToString
(
SBufferReader
*
buf
,
char
*
dst
,
size_t
size
)
{
assert
(
dst
!=
NULL
);
assert
(
dst
!=
NULL
);
size_t
len
;
size_t
len
;
const
char
*
str
=
tbufReadString
(
buf
,
&
len
);
const
char
*
str
=
tbufReadString
(
buf
,
&
len
);
if
(
len
>=
size
)
{
if
(
len
>=
size
)
{
len
=
size
-
1
;
len
=
size
-
1
;
}
}
memcpy
(
dst
,
str
,
len
);
memcpy
(
dst
,
str
,
len
);
dst
[
len
]
=
0
;
dst
[
len
]
=
0
;
return
len
;
return
len
;
}
}
const
char
*
tbufReadBinary
(
SBuffer
*
buf
,
size_t
*
len
)
{
const
char
*
tbufReadBinary
(
SBufferReader
*
buf
,
size_t
*
len
)
{
size_t
l
=
tbufReadLength
(
buf
);
size_t
l
=
tbufReadLength
(
buf
);
char
*
ret
=
buf
->
data
+
buf
->
pos
;
char
*
ret
=
buf
->
data
+
buf
->
pos
;
tbufSkip
(
buf
,
l
);
tbufSkip
(
buf
,
l
);
if
(
len
!=
NULL
)
{
if
(
len
!=
NULL
)
{
*
len
=
l
;
*
len
=
l
;
}
}
return
ret
;
return
ret
;
}
}
size_t
tbufReadToBinary
(
SBuffer
*
buf
,
void
*
dst
,
size_t
size
)
{
size_t
tbufReadToBinary
(
SBufferReader
*
buf
,
void
*
dst
,
size_t
size
)
{
assert
(
dst
!=
NULL
);
assert
(
dst
!=
NULL
);
size_t
len
;
size_t
len
;
const
char
*
data
=
tbufReadBinary
(
buf
,
&
len
);
const
char
*
data
=
tbufReadBinary
(
buf
,
&
len
);
if
(
len
>=
size
)
{
if
(
len
>=
size
)
{
len
=
size
;
len
=
size
;
}
}
memcpy
(
dst
,
data
,
len
);
memcpy
(
dst
,
data
,
len
);
return
len
;
return
len
;
}
}
////////////////////////////////////////////////////////////////////////////////
bool
tbufReadBool
(
SBufferReader
*
buf
)
{
// write functions
bool
ret
;
tbufReadToBuffer
(
buf
,
&
ret
,
sizeof
(
ret
)
);
return
ret
;
}
void
tbufBeginWrite
(
SBuffer
*
buf
)
{
char
tbufReadChar
(
SBufferReader
*
buf
)
{
buf
->
data
=
NULL
;
char
ret
;
buf
->
pos
=
0
;
tbufReadToBuffer
(
buf
,
&
ret
,
sizeof
(
ret
)
)
;
buf
->
size
=
0
;
return
ret
;
}
}
void
tbufEnsureCapacity
(
SBuffer
*
buf
,
size_t
size
)
{
int8_t
tbufReadInt8
(
SBufferReader
*
buf
)
{
size
+=
buf
->
pos
;
int8_t
ret
;
if
(
size
>
buf
->
size
)
{
tbufReadToBuffer
(
buf
,
&
ret
,
sizeof
(
ret
)
);
size_t
nsize
=
size
+
buf
->
size
;
return
ret
;
char
*
data
=
(
*
buf
->
allocator
)(
buf
->
data
,
nsize
);
if
(
data
==
NULL
)
{
// TODO: handle client out of memory
THROW
(
TSDB_CODE_SERV_OUT_OF_MEMORY
);
}
buf
->
data
=
data
;
buf
->
size
=
nsize
;
}
}
}
size_t
tbufReserve
(
SBuffer
*
buf
,
size_t
size
)
{
uint8_t
tbufReadUint8
(
SBufferReader
*
buf
)
{
tbufEnsureCapacity
(
buf
,
size
);
uint8_t
ret
;
return
tbufSeekTo
(
buf
,
buf
->
pos
+
size
);
tbufReadToBuffer
(
buf
,
&
ret
,
sizeof
(
ret
)
);
return
ret
;
}
}
char
*
tbufGetData
(
SBuffer
*
buf
,
bool
takeOver
)
{
int16_t
tbufReadInt16
(
SBufferReader
*
buf
)
{
char
*
ret
=
buf
->
data
;
int16_t
ret
;
if
(
takeOver
)
{
tbufReadToBuffer
(
buf
,
&
ret
,
sizeof
(
ret
)
);
buf
->
pos
=
0
;
if
(
buf
->
endian
)
{
buf
->
size
=
0
;
return
(
int16_t
)
ntohs
(
ret
);
buf
->
data
=
NULL
;
}
}
return
ret
;
}
uint16_t
tbufReadUint16
(
SBufferReader
*
buf
)
{
uint16_t
ret
;
tbufReadToBuffer
(
buf
,
&
ret
,
sizeof
(
ret
)
);
if
(
buf
->
endian
)
{
return
ntohs
(
ret
);
}
return
ret
;
return
ret
;
}
}
void
tbufWrite
(
SBuffer
*
buf
,
const
void
*
data
,
size_t
size
)
{
int32_t
tbufReadInt32
(
SBufferReader
*
buf
)
{
assert
(
data
!=
NULL
);
int32_t
ret
;
tbufEnsureCapacity
(
buf
,
size
);
tbufReadToBuffer
(
buf
,
&
ret
,
sizeof
(
ret
)
);
memcpy
(
buf
->
data
+
buf
->
pos
,
data
,
size
);
if
(
buf
->
endian
)
{
buf
->
pos
+=
size
;
return
(
int32_t
)
ntohl
(
ret
);
}
return
ret
;
}
}
void
tbufWriteAt
(
SBuffer
*
buf
,
size_t
pos
,
const
void
*
data
,
size_t
size
)
{
uint32_t
tbufReadUint32
(
SBufferReader
*
buf
)
{
assert
(
data
!=
NULL
);
uint32_t
ret
;
// this function can only be called to fill the gap on previous writes,
tbufReadToBuffer
(
buf
,
&
ret
,
sizeof
(
ret
)
);
// so 'pos + size <= buf->pos' must be true
if
(
buf
->
endian
)
{
assert
(
pos
+
size
<=
buf
->
pos
);
return
ntohl
(
ret
);
memcpy
(
buf
->
data
+
pos
,
data
,
size
);
}
return
ret
;
}
}
static
void
tbufWriteLength
(
SBuffer
*
buf
,
size_t
len
)
{
int64_t
tbufReadInt64
(
SBufferReader
*
buf
)
{
// maximum length is 65535, if larger length is required
int64_t
ret
;
// this function and the corresponding read function need to be
tbufReadToBuffer
(
buf
,
&
ret
,
sizeof
(
ret
)
);
// revised.
if
(
buf
->
endian
)
{
assert
(
len
<=
0xffff
);
return
(
int64_t
)
htobe64
(
ret
);
// TODO: ntohll
tbufWriteUint16
(
buf
,
(
uint16_t
)
len
);
}
return
ret
;
}
}
void
tbufWriteStringLen
(
SBuffer
*
buf
,
const
char
*
str
,
size_t
len
)
{
uint64_t
tbufReadUint64
(
SBufferReader
*
buf
)
{
tbufWriteLength
(
buf
,
len
);
uint64_t
ret
;
tbufWrite
(
buf
,
str
,
len
);
tbufReadToBuffer
(
buf
,
&
ret
,
sizeof
(
ret
)
);
tbufWriteChar
(
buf
,
'\0'
);
if
(
buf
->
endian
)
{
return
htobe64
(
ret
);
// TODO: ntohll
}
return
ret
;
}
}
void
tbufWriteString
(
SBuffer
*
buf
,
const
char
*
str
)
{
float
tbufReadFloat
(
SBufferReader
*
buf
)
{
tbufWriteStringLen
(
buf
,
str
,
strlen
(
str
));
uint32_t
ret
=
tbufReadUint32
(
buf
);
return
*
(
float
*
)(
&
ret
);
}
}
void
tbufWriteBinary
(
SBuffer
*
buf
,
const
void
*
data
,
size_t
len
)
{
double
tbufReadDouble
(
SBufferReader
*
buf
)
{
tbufWriteLength
(
buf
,
len
);
uint64_t
ret
=
tbufReadUint64
(
buf
);
tbufWrite
(
buf
,
data
,
len
);
return
*
(
double
*
)(
&
ret
);
}
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
read / write functions for primitive type
s
//
writer function
s
bool
tbufReadBool
(
SBuffer
*
buf
)
{
void
tbufCloseWriter
(
SBufferWriter
*
buf
)
{
bool
ret
;
(
*
buf
->
allocator
)(
buf
->
data
,
0
);
tbufReadToBuffer
(
buf
,
&
ret
,
sizeof
(
ret
));
buf
->
data
=
NULL
;
return
ret
;
buf
->
pos
=
0
;
buf
->
size
=
0
;
}
}
void
tbufWriteBool
(
SBuffer
*
buf
,
bool
data
)
{
void
tbufEnsureCapacity
(
SBufferWriter
*
buf
,
size_t
size
)
{
tbufWrite
(
buf
,
&
data
,
sizeof
(
data
));
size
+=
buf
->
pos
;
if
(
size
>
buf
->
size
)
{
size_t
nsize
=
size
+
buf
->
size
;
char
*
data
=
(
*
buf
->
allocator
)(
buf
->
data
,
nsize
);
// TODO: the exception should be thrown by the allocator function
if
(
data
==
NULL
)
{
THROW
(
TSDB_CODE_SERV_OUT_OF_MEMORY
);
}
buf
->
data
=
data
;
buf
->
size
=
nsize
;
}
}
}
void
tbufWriteBoolAt
(
SBuffer
*
buf
,
size_t
pos
,
bool
data
)
{
size_t
tbufReserve
(
SBufferWriter
*
buf
,
size_t
size
)
{
tbufWriteAt
(
buf
,
pos
,
&
data
,
sizeof
(
data
));
tbufEnsureCapacity
(
buf
,
size
);
size_t
old
=
buf
->
pos
;
buf
->
pos
+=
size
;
return
old
;
}
}
char
tbufReadChar
(
SBuffer
*
buf
)
{
char
*
tbufGetData
(
SBufferWriter
*
buf
,
bool
takeOver
)
{
char
ret
;
char
*
ret
=
buf
->
data
;
tbufReadToBuffer
(
buf
,
&
ret
,
sizeof
(
ret
));
if
(
takeOver
)
{
buf
->
pos
=
0
;
buf
->
size
=
0
;
buf
->
data
=
NULL
;
}
return
ret
;
return
ret
;
}
}
void
tbufWriteChar
(
SBuffer
*
buf
,
char
data
)
{
void
tbufWrite
(
SBufferWriter
*
buf
,
const
void
*
data
,
size_t
size
)
{
tbufWrite
(
buf
,
&
data
,
sizeof
(
data
));
assert
(
data
!=
NULL
);
tbufEnsureCapacity
(
buf
,
size
);
memcpy
(
buf
->
data
+
buf
->
pos
,
data
,
size
);
buf
->
pos
+=
size
;
}
}
void
tbufWriteCharAt
(
SBuffer
*
buf
,
size_t
pos
,
char
data
)
{
void
tbufWriteAt
(
SBufferWriter
*
buf
,
size_t
pos
,
const
void
*
data
,
size_t
size
)
{
tbufWriteAt
(
buf
,
pos
,
&
data
,
sizeof
(
data
));
assert
(
data
!=
NULL
);
// this function can only be called to fill the gap on previous writes,
// so 'pos + size <= buf->pos' must be true
assert
(
pos
+
size
<=
buf
->
pos
);
memcpy
(
buf
->
data
+
pos
,
data
,
size
);
}
}
int8_t
tbufReadInt8
(
SBuffer
*
buf
)
{
static
void
tbufWriteLength
(
SBufferWriter
*
buf
,
size_t
len
)
{
int8_t
ret
;
// maximum length is 65535, if larger length is required
tbufReadToBuffer
(
buf
,
&
ret
,
sizeof
(
ret
));
// this function and the corresponding read function need to be
return
ret
;
// revised.
assert
(
len
<=
0xffff
);
tbufWriteUint16
(
buf
,
(
uint16_t
)
len
);
}
}
void
tbufWriteInt8
(
SBuffer
*
buf
,
int8_t
data
)
{
void
tbufWriteStringLen
(
SBufferWriter
*
buf
,
const
char
*
str
,
size_t
len
)
{
tbufWrite
(
buf
,
&
data
,
sizeof
(
data
));
tbufWriteLength
(
buf
,
len
);
tbufWrite
(
buf
,
str
,
len
);
tbufWriteChar
(
buf
,
'\0'
);
}
}
void
tbufWrite
Int8At
(
SBuffer
*
buf
,
size_t
pos
,
int8_t
data
)
{
void
tbufWrite
String
(
SBufferWriter
*
buf
,
const
char
*
str
)
{
tbufWrite
At
(
buf
,
pos
,
&
data
,
sizeof
(
data
)
);
tbufWrite
StringLen
(
buf
,
str
,
strlen
(
str
)
);
}
}
uint8_t
tbufReadUint8
(
SBuffer
*
buf
)
{
void
tbufWriteBinary
(
SBufferWriter
*
buf
,
const
void
*
data
,
size_t
len
)
{
uint8_t
ret
;
tbufWriteLength
(
buf
,
len
);
tbufReadToBuffer
(
buf
,
&
ret
,
sizeof
(
ret
));
tbufWrite
(
buf
,
data
,
len
);
return
ret
;
}
}
void
tbufWrite
Uint8
(
SBuffer
*
buf
,
uint8_t
data
)
{
void
tbufWrite
Bool
(
SBufferWriter
*
buf
,
bool
data
)
{
tbufWrite
(
buf
,
&
data
,
sizeof
(
data
)
);
tbufWrite
(
buf
,
&
data
,
sizeof
(
data
)
);
}
}
void
tbufWrite
Uint8At
(
SBuffer
*
buf
,
size_t
pos
,
uint8_t
data
)
{
void
tbufWrite
BoolAt
(
SBufferWriter
*
buf
,
size_t
pos
,
bool
data
)
{
tbufWriteAt
(
buf
,
pos
,
&
data
,
sizeof
(
data
)
);
tbufWriteAt
(
buf
,
pos
,
&
data
,
sizeof
(
data
)
);
}
}
int16_t
tbufReadInt16
(
SBuffer
*
buf
)
{
void
tbufWriteChar
(
SBufferWriter
*
buf
,
char
data
)
{
int16_t
ret
;
tbufWrite
(
buf
,
&
data
,
sizeof
(
data
)
);
tbufReadToBuffer
(
buf
,
&
ret
,
sizeof
(
ret
));
if
(
buf
->
endian
)
{
return
(
int16_t
)
ntohs
(
ret
);
}
return
ret
;
}
}
void
tbufWriteInt16
(
SBuffer
*
buf
,
int16_t
data
)
{
void
tbufWriteCharAt
(
SBufferWriter
*
buf
,
size_t
pos
,
char
data
)
{
if
(
buf
->
endian
)
{
tbufWriteAt
(
buf
,
pos
,
&
data
,
sizeof
(
data
)
);
data
=
(
int16_t
)
htons
(
data
);
}
tbufWrite
(
buf
,
&
data
,
sizeof
(
data
));
}
}
void
tbufWriteInt16At
(
SBuffer
*
buf
,
size_t
pos
,
int16_t
data
)
{
void
tbufWriteInt8
(
SBufferWriter
*
buf
,
int8_t
data
)
{
if
(
buf
->
endian
)
{
tbufWrite
(
buf
,
&
data
,
sizeof
(
data
)
);
data
=
(
int16_t
)
htons
(
data
);
}
tbufWriteAt
(
buf
,
pos
,
&
data
,
sizeof
(
data
));
}
}
uint16_t
tbufReadUint16
(
SBuffer
*
buf
)
{
void
tbufWriteInt8At
(
SBufferWriter
*
buf
,
size_t
pos
,
int8_t
data
)
{
uint16_t
ret
;
tbufWriteAt
(
buf
,
pos
,
&
data
,
sizeof
(
data
)
);
tbufReadToBuffer
(
buf
,
&
ret
,
sizeof
(
ret
));
if
(
buf
->
endian
)
{
return
ntohs
(
ret
);
}
return
ret
;
}
}
void
tbufWriteUint16
(
SBuffer
*
buf
,
uint16_t
data
)
{
void
tbufWriteUint8
(
SBufferWriter
*
buf
,
uint8_t
data
)
{
if
(
buf
->
endian
)
{
tbufWrite
(
buf
,
&
data
,
sizeof
(
data
)
);
data
=
htons
(
data
);
}
tbufWrite
(
buf
,
&
data
,
sizeof
(
data
));
}
}
void
tbufWriteUint16At
(
SBuffer
*
buf
,
size_t
pos
,
uint16_t
data
)
{
void
tbufWriteUint8At
(
SBufferWriter
*
buf
,
size_t
pos
,
uint8_t
data
)
{
if
(
buf
->
endian
)
{
tbufWriteAt
(
buf
,
pos
,
&
data
,
sizeof
(
data
)
);
data
=
htons
(
data
);
}
tbufWriteAt
(
buf
,
pos
,
&
data
,
sizeof
(
data
));
}
}
int32_t
tbufReadInt32
(
SBuffer
*
buf
)
{
void
tbufWriteInt16
(
SBufferWriter
*
buf
,
int16_t
data
)
{
int32_t
ret
;
if
(
buf
->
endian
)
{
tbufReadToBuffer
(
buf
,
&
ret
,
sizeof
(
ret
));
data
=
(
int16_t
)
htons
(
data
);
if
(
buf
->
endian
)
{
return
(
int32_t
)
ntohl
(
ret
);
}
}
return
ret
;
tbufWrite
(
buf
,
&
data
,
sizeof
(
data
)
)
;
}
}
void
tbufWriteInt
32
(
SBuffer
*
buf
,
int32_t
data
)
{
void
tbufWriteInt
16At
(
SBufferWriter
*
buf
,
size_t
pos
,
int16_t
data
)
{
if
(
buf
->
endian
)
{
if
(
buf
->
endian
)
{
data
=
(
int
32_t
)
htonl
(
data
);
data
=
(
int
16_t
)
htons
(
data
);
}
}
tbufWrite
(
buf
,
&
data
,
sizeof
(
data
)
);
tbufWrite
At
(
buf
,
pos
,
&
data
,
sizeof
(
data
)
);
}
}
void
tbufWrite
Int32At
(
SBuffer
*
buf
,
size_t
pos
,
int32_t
data
)
{
void
tbufWrite
Uint16
(
SBufferWriter
*
buf
,
uint16_t
data
)
{
if
(
buf
->
endian
)
{
if
(
buf
->
endian
)
{
data
=
(
int32_t
)
htonl
(
data
);
data
=
htons
(
data
);
}
}
tbufWrite
At
(
buf
,
pos
,
&
data
,
sizeof
(
data
)
);
tbufWrite
(
buf
,
&
data
,
sizeof
(
data
)
);
}
}
uint32_t
tbufReadUint32
(
SBuffer
*
buf
)
{
void
tbufWriteUint16At
(
SBufferWriter
*
buf
,
size_t
pos
,
uint16_t
data
)
{
uint32_t
ret
;
if
(
buf
->
endian
)
{
tbufReadToBuffer
(
buf
,
&
ret
,
sizeof
(
ret
));
data
=
htons
(
data
);
if
(
buf
->
endian
)
{
return
ntohl
(
ret
);
}
}
return
ret
;
tbufWriteAt
(
buf
,
pos
,
&
data
,
sizeof
(
data
)
)
;
}
}
void
tbufWrite
Uint32
(
SBuffer
*
buf
,
uint32_t
data
)
{
void
tbufWrite
Int32
(
SBufferWriter
*
buf
,
int32_t
data
)
{
if
(
buf
->
endian
)
{
if
(
buf
->
endian
)
{
data
=
htonl
(
data
);
data
=
(
int32_t
)
htonl
(
data
);
}
}
tbufWrite
(
buf
,
&
data
,
sizeof
(
data
)
);
tbufWrite
(
buf
,
&
data
,
sizeof
(
data
)
);
}
}
void
tbufWrite
Uint32At
(
SBuffer
*
buf
,
size_t
pos
,
uint32_t
data
)
{
void
tbufWrite
Int32At
(
SBufferWriter
*
buf
,
size_t
pos
,
int32_t
data
)
{
if
(
buf
->
endian
)
{
if
(
buf
->
endian
)
{
data
=
htonl
(
data
);
data
=
(
int32_t
)
htonl
(
data
);
}
}
tbufWriteAt
(
buf
,
pos
,
&
data
,
sizeof
(
data
)
);
tbufWriteAt
(
buf
,
pos
,
&
data
,
sizeof
(
data
)
);
}
}
int64_t
tbufReadInt64
(
SBuffer
*
buf
)
{
void
tbufWriteUint32
(
SBufferWriter
*
buf
,
uint32_t
data
)
{
int64_t
ret
;
if
(
buf
->
endian
)
{
tbufReadToBuffer
(
buf
,
&
ret
,
sizeof
(
ret
));
data
=
htonl
(
data
);
if
(
buf
->
endian
)
{
return
(
int64_t
)
htobe64
(
ret
);
// TODO: ntohll
}
}
return
ret
;
tbufWrite
(
buf
,
&
data
,
sizeof
(
data
)
)
;
}
}
void
tbufWrite
Int64
(
SBuffer
*
buf
,
int64_t
data
)
{
void
tbufWrite
Uint32At
(
SBufferWriter
*
buf
,
size_t
pos
,
uint32_t
data
)
{
if
(
buf
->
endian
)
{
if
(
buf
->
endian
)
{
data
=
(
int64_t
)
htobe64
(
data
);
data
=
htonl
(
data
);
}
}
tbufWrite
(
buf
,
&
data
,
sizeof
(
data
)
);
tbufWrite
At
(
buf
,
pos
,
&
data
,
sizeof
(
data
)
);
}
}
void
tbufWriteInt64
At
(
SBuffer
*
buf
,
size_t
pos
,
int64_t
data
)
{
void
tbufWriteInt64
(
SBufferWriter
*
buf
,
int64_t
data
)
{
if
(
buf
->
endian
)
{
if
(
buf
->
endian
)
{
data
=
(
int64_t
)
htobe64
(
data
);
data
=
(
int64_t
)
htobe64
(
data
);
}
}
tbufWrite
At
(
buf
,
pos
,
&
data
,
sizeof
(
data
)
);
tbufWrite
(
buf
,
&
data
,
sizeof
(
data
)
);
}
}
uint64_t
tbufReadUint64
(
SBuffer
*
buf
)
{
void
tbufWriteInt64At
(
SBufferWriter
*
buf
,
size_t
pos
,
int64_t
data
)
{
uint64_t
ret
;
if
(
buf
->
endian
)
{
tbufReadToBuffer
(
buf
,
&
ret
,
sizeof
(
ret
));
data
=
(
int64_t
)
htobe64
(
data
);
if
(
buf
->
endian
)
{
return
htobe64
(
ret
);
// TODO: ntohll
}
}
return
ret
;
tbufWriteAt
(
buf
,
pos
,
&
data
,
sizeof
(
data
)
)
;
}
}
void
tbufWriteUint64
(
SBuffer
*
buf
,
uint64_t
data
)
{
void
tbufWriteUint64
(
SBufferWriter
*
buf
,
uint64_t
data
)
{
if
(
buf
->
endian
)
{
if
(
buf
->
endian
)
{
data
=
htobe64
(
data
);
data
=
htobe64
(
data
);
}
}
tbufWrite
(
buf
,
&
data
,
sizeof
(
data
)
);
tbufWrite
(
buf
,
&
data
,
sizeof
(
data
)
);
}
}
void
tbufWriteUint64At
(
SBuffer
*
buf
,
size_t
pos
,
uint64_t
data
)
{
void
tbufWriteUint64At
(
SBufferWriter
*
buf
,
size_t
pos
,
uint64_t
data
)
{
if
(
buf
->
endian
)
{
if
(
buf
->
endian
)
{
data
=
htobe64
(
data
);
data
=
htobe64
(
data
);
}
}
tbufWriteAt
(
buf
,
pos
,
&
data
,
sizeof
(
data
));
tbufWriteAt
(
buf
,
pos
,
&
data
,
sizeof
(
data
)
);
}
float
tbufReadFloat
(
SBuffer
*
buf
)
{
uint32_t
ret
=
tbufReadUint32
(
buf
);
return
*
(
float
*
)(
&
ret
);
}
void
tbufWriteFloat
(
SBuffer
*
buf
,
float
data
)
{
tbufWriteUint32
(
buf
,
*
(
uint32_t
*
)(
&
data
));
}
}
void
tbufWriteFloat
At
(
SBuffer
*
buf
,
size_t
pos
,
float
data
)
{
void
tbufWriteFloat
(
SBufferWriter
*
buf
,
float
data
)
{
tbufWriteUint32
At
(
buf
,
pos
,
*
(
uint32_t
*
)(
&
data
)
);
tbufWriteUint32
(
buf
,
*
(
uint32_t
*
)(
&
data
)
);
}
}
double
tbufReadDouble
(
SBuffer
*
buf
)
{
void
tbufWriteFloatAt
(
SBufferWriter
*
buf
,
size_t
pos
,
float
data
)
{
uint64_t
ret
=
tbufReadUint64
(
buf
);
tbufWriteUint32At
(
buf
,
pos
,
*
(
uint32_t
*
)(
&
data
)
);
return
*
(
double
*
)(
&
ret
);
}
}
void
tbufWriteDouble
(
SBuffer
*
buf
,
double
data
)
{
void
tbufWriteDouble
(
SBufferWriter
*
buf
,
double
data
)
{
tbufWriteUint64
(
buf
,
*
(
uint64_t
*
)(
&
data
)
);
tbufWriteUint64
(
buf
,
*
(
uint64_t
*
)(
&
data
)
);
}
}
void
tbufWriteDoubleAt
(
SBuffer
*
buf
,
size_t
pos
,
double
data
)
{
void
tbufWriteDoubleAt
(
SBufferWriter
*
buf
,
size_t
pos
,
double
data
)
{
tbufWriteUint64At
(
buf
,
pos
,
*
(
uint64_t
*
)(
&
data
)
);
tbufWriteUint64At
(
buf
,
pos
,
*
(
uint64_t
*
)(
&
data
)
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录