修改文件编码
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
#include <string.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <errno.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "tfe_types.h"
|
||||
#include "tfe_utils.h"
|
||||
@@ -23,6 +24,7 @@ struct tfe_cmsg_tlv
|
||||
|
||||
struct tfe_cmsg
|
||||
{
|
||||
pthread_rwlock_t rwlock;
|
||||
uint16_t nr_tlvs;
|
||||
struct tfe_cmsg_tlv* tlvs[TFE_CMSG_TLV_NR_MAX];
|
||||
uint16_t size;
|
||||
@@ -39,6 +41,8 @@ struct tfe_cmsg* tfe_cmsg_init()
|
||||
{
|
||||
struct tfe_cmsg *cmsg = ALLOC(struct tfe_cmsg, 1);
|
||||
cmsg->size = sizeof(struct tfe_cmsg_serialize_header);
|
||||
|
||||
pthread_rwlock_init(&(cmsg->rwlock), NULL);
|
||||
return cmsg;
|
||||
}
|
||||
|
||||
@@ -46,11 +50,15 @@ void tfe_cmsg_destroy(struct tfe_cmsg *cmsg)
|
||||
{
|
||||
if(cmsg != NULL)
|
||||
{
|
||||
pthread_rwlock_wrlock(&cmsg->rwlock);
|
||||
for(int i = 0; i < TFE_CMSG_TLV_NR_MAX; i++)
|
||||
{
|
||||
FREE(&(cmsg->tlvs[i]));
|
||||
}
|
||||
pthread_rwlock_unlock(&cmsg->rwlock);
|
||||
pthread_rwlock_destroy(&cmsg->rwlock);
|
||||
}
|
||||
|
||||
FREE(&cmsg);
|
||||
}
|
||||
|
||||
@@ -60,6 +68,7 @@ int tfe_cmsg_set(struct tfe_cmsg * cmsg, enum tfe_cmsg_tlv_type type, const unsi
|
||||
{
|
||||
return TFE_CMSG_INVALID_TYPE;
|
||||
}
|
||||
pthread_rwlock_wrlock(&cmsg->rwlock);
|
||||
struct tfe_cmsg_tlv *tlv = cmsg->tlvs[type];
|
||||
uint16_t length = sizeof(struct tfe_cmsg_tlv) + size;
|
||||
|
||||
@@ -83,6 +92,7 @@ int tfe_cmsg_set(struct tfe_cmsg * cmsg, enum tfe_cmsg_tlv_type type, const unsi
|
||||
tlv->length = length;
|
||||
memcpy(tlv->value_as_string, value, size);
|
||||
cmsg->tlvs[type] = tlv;
|
||||
pthread_rwlock_unlock(&cmsg->rwlock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -99,6 +109,7 @@ int tfe_cmsg_get_value(struct tfe_cmsg * cmsg, enum tfe_cmsg_tlv_type type, unsi
|
||||
goto errout;
|
||||
}
|
||||
|
||||
pthread_rwlock_rdlock(&cmsg->rwlock);
|
||||
tlv = cmsg->tlvs[type];
|
||||
if (unlikely(tlv == NULL))
|
||||
{
|
||||
@@ -115,31 +126,38 @@ int tfe_cmsg_get_value(struct tfe_cmsg * cmsg, enum tfe_cmsg_tlv_type type, unsi
|
||||
|
||||
memcpy(out_value, tlv->value_as_string, value_length);
|
||||
*out_size = value_length;
|
||||
|
||||
pthread_rwlock_unlock(&cmsg->rwlock);
|
||||
return 0;
|
||||
|
||||
errout:
|
||||
pthread_rwlock_unlock(&cmsg->rwlock);
|
||||
return result;
|
||||
}
|
||||
|
||||
uint16_t tfe_cmsg_serialize_size_get(struct tfe_cmsg *cmsg)
|
||||
{
|
||||
return cmsg->size;
|
||||
pthread_rwlock_rdlock(&cmsg->rwlock);
|
||||
uint16_t size = cmsg->size;
|
||||
pthread_rwlock_unlock(&cmsg->rwlock);
|
||||
return size;
|
||||
}
|
||||
|
||||
int tfe_cmsg_serialize(struct tfe_cmsg *cmsg, unsigned char *buff, uint16_t bufflen, uint16_t *serialize_len)
|
||||
{
|
||||
//size是serialize之后的实际长度
|
||||
pthread_rwlock_rdlock(&cmsg->rwlock);
|
||||
uint16_t size = cmsg->size;
|
||||
//传入buff是否够长
|
||||
if(bufflen < size)
|
||||
{
|
||||
pthread_rwlock_unlock(&cmsg->rwlock);
|
||||
return TFE_CMSG_BUFF_NOT_ENOUGH;
|
||||
}
|
||||
//size是否正确
|
||||
if(size < sizeof(struct tfe_cmsg_serialize_header))
|
||||
{
|
||||
return TFE_CMSG_INVALID_FORMAT;
|
||||
pthread_rwlock_unlock(&cmsg->rwlock);
|
||||
return TFE_CMSG_BUFF_NOT_ENOUGH;
|
||||
}
|
||||
struct tfe_cmsg_serialize_header *header = (struct tfe_cmsg_serialize_header*)buff;
|
||||
header->__magic__[0] = 0x4d;
|
||||
@@ -156,6 +174,7 @@ int tfe_cmsg_serialize(struct tfe_cmsg *cmsg, unsigned char *buff, uint16_t buff
|
||||
}
|
||||
if(count != cmsg->nr_tlvs)
|
||||
{
|
||||
pthread_rwlock_unlock(&cmsg->rwlock);
|
||||
return TFE_CMSG_INVALID_FORMAT;
|
||||
}
|
||||
//序列化
|
||||
@@ -168,11 +187,13 @@ int tfe_cmsg_serialize(struct tfe_cmsg *cmsg, unsigned char *buff, uint16_t buff
|
||||
}
|
||||
if(i != tlv->type)
|
||||
{
|
||||
pthread_rwlock_unlock(&cmsg->rwlock);
|
||||
return TFE_CMSG_INVALID_FORMAT;
|
||||
}
|
||||
uint16_t length = tlv->length;
|
||||
if(length < sizeof(struct tfe_cmsg_tlv) || offset + length > size)
|
||||
{
|
||||
pthread_rwlock_unlock(&cmsg->rwlock);
|
||||
return TFE_CMSG_INVALID_FORMAT;
|
||||
}
|
||||
memcpy((char*)header + offset, (void*)tlv, length);
|
||||
@@ -184,9 +205,11 @@ int tfe_cmsg_serialize(struct tfe_cmsg *cmsg, unsigned char *buff, uint16_t buff
|
||||
//检查size是否正确
|
||||
if(offset != size)
|
||||
{
|
||||
pthread_rwlock_unlock(&cmsg->rwlock);
|
||||
return TFE_CMSG_INVALID_FORMAT;
|
||||
}
|
||||
*serialize_len = size;
|
||||
pthread_rwlock_unlock(&cmsg->rwlock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -235,7 +258,9 @@ int tfe_cmsg_deserialize(const unsigned char *data, uint16_t len, struct tfe_cms
|
||||
offset += length;
|
||||
}
|
||||
cmsg->size = offset;
|
||||
pthread_rwlock_wrlock(&((*pcmsg)->rwlock));
|
||||
*pcmsg = cmsg;
|
||||
pthread_rwlock_unlock(&((*pcmsg)->rwlock));
|
||||
return 0;
|
||||
|
||||
error_out:
|
||||
|
||||
Reference in New Issue
Block a user