首先,HPSocket是一个很好的开源网络项目,支持跨平台,Windows,Linux,Android,MacOS等等。但是就Windows平台而言,它的开发环境是VisualStudio。由于c++编译器的差异,Mingw和Mingw64是无法调用HPSocket的c++动态链接库的,因此我们只能使用HPSocket的c语言动态库。但是对于习惯使用c++的人来说,使用c语言是一件感觉很不好的事,就像现代人,突然回到了原始社会一样,在c语言中,没有c++的各种武器,并且函数、变量分散,阅读代码困难。因此自己抛砖引玉,对HPSocket做了下简单的包装,代码如下。
//
// Created by Yoo on 2020/5/26.
//
#ifndef HP_WRAPPER_HPP
#define HP_WRAPPER_HPP
#include <cstdio>
#include <HPSocket/HPSocket4C.h>
#include <HPSocket/HPTypeDef.h>
#include <assert.h>
#include <memory.h>
#include <malloc.h>
#if !defined(MAX)
#define MAX(a, b) (((a) >= (b)) ? (a) : (b))
#endif
#if !defined(MIN)
#define MIN(a, b) (((a) <= (b)) ? (a) : (b))
#endif
template<class T, size_t MAX_CACHE_SIZE = 0>
class CBufferPtrT {
public:
explicit CBufferPtrT(size_t size = 0, bool zero = false) {
Reset();
Malloc(size, zero);
}
explicit CBufferPtrT(const T *pch, size_t size) {
Reset();
Copy(pch, size);
}
CBufferPtrT(const CBufferPtrT &other) {
Reset();
Copy(other);
}
template<size_t S>
CBufferPtrT(const CBufferPtrT<T, S> &other) {
Reset();
Copy(other);
}
~CBufferPtrT() { Free(); }
T *Malloc(size_t size = 1, bool zero = false) {
Free();
return Alloc(size, zero, false);
}
T *Realloc(size_t size, bool zero = false) {
return Alloc(size, zero, true);
}
void Free() {
if (m_pch) {
free(m_pch);
Reset();
}
}
template<size_t S>
CBufferPtrT &Copy(const CBufferPtrT<T, S> &other) {
if ((void *) &other != (void *) this)
Copy(other.Ptr(), other.Size());
return *this;
}
CBufferPtrT &Copy(const T *pch, size_t size) {
Malloc(size);
if (m_pch)
memcpy(m_pch, pch, size * sizeof(T));
return *this;
}
template<size_t S>
CBufferPtrT &Cat(const CBufferPtrT<T, S> &other) {
if ((void *) &other != (void *) this)
Cat(other.Ptr(), other.Size());
return *this;
}
CBufferPtrT &Cat(const T *pch, size_t size = 1) {
size_t pre_size = m_size;
Realloc(m_size + size);
if (m_pch)
memcpy(m_pch + pre_size, pch, size * sizeof(T));
return *this;
}
template<size_t S>
bool Equal(const CBufferPtrT<T, S> &other) const {
if ((void *) &other == (void *) this)
return true;
else if (m_size != other.Size())
return false;
else if (m_size == 0)
return true;
else
return (memcmp(m_pch, other.Ptr(), m_size * sizeof(T)) == 0);
}
bool Equal(T *pch) const {
if (m_pch == pch)
return true;
else if (!m_pch || !pch)
return false;
else
return (memcmp(m_pch, pch, m_size * sizeof(T)) == 0);
}
T *Ptr() { return m_pch; }
const T *Ptr() const { return m_pch; }
T &Get(int i) { return *(m_pch + i); }
const T &Get(int i) const { return *(m_pch + i); }
size_t Size() const { return m_size; }
bool IsValid() const { return m_pch != 0; }
operator T *() { return Ptr(); }
operator const T *() const { return Ptr(); }
T &operator[](int i) { return Get(i); }
const T &operator[](int i) const { return Get(i); }
bool operator==(T *pv) const { return Equal(pv); }
template<size_t S>
bool operator==(const CBufferPtrT<T, S> &other) { return Equal(other); }
CBufferPtrT &operator=(const CBufferPtrT &other) { return Copy(other); }
template<size_t S>
CBufferPtrT &operator=(const CBufferPtrT<T, S> &other) { return Copy(other); }
private:
void Reset() {
m_pch = 0;
m_size = 0;
m_capacity = 0;
}
size_t GetAllocSize(size_t size) { return MAX(size, MIN(size * 2, m_size + MAX_CACHE_SIZE)); }
T *Alloc(size_t size, bool zero = false, bool is_realloc = false) {
if (size >= 0 && size != m_size) {
size_t rsize = GetAllocSize(size);
if (size > m_capacity || rsize < m_size) {
m_pch = is_realloc ?
(T *) realloc(m_pch, rsize * sizeof(T)) :
(T *) malloc(rsize * sizeof(T));
if (m_pch || rsize == 0) {
m_size = size;


750

被折叠的 条评论
为什么被折叠?



