//
|
// NetStream.hpp
|
// GoldRich
|
//
|
// Created by WindShan on 2017/3/8.
|
// Copyright © 2017年 WindShan. All rights reserved.
|
//
|
|
#ifndef NetStream_hpp
|
#define NetStream_hpp
|
|
#include <stdio.h>
|
#include <string.h>
|
|
|
#include <sys/socket.h>
|
#include <fcntl.h>
|
#include <errno.h>
|
#include <netinet/in.h>
|
#include <arpa/inet.h>
|
|
#include "hton_ntoh.h"
|
#import "TypeConv.h"
|
|
#include <iostream.h>
|
using namespace std;
|
|
|
class TypeConv;
|
|
const static int net_stream_mod_xx = 1;
|
|
class CNetStream
|
{
|
public:
|
CNetStream(void)
|
{
|
pConv = new TypeConv(net_stream_mod_xx);
|
}
|
~CNetStream(void)
|
{
|
if (pConv)
|
{
|
delete pConv;
|
}
|
}
|
|
private:
|
TypeConv* pConv;
|
string tmp_st;
|
public:
|
int writeInt(int val)
|
{
|
int n = htonl(val);
|
return pConv->toBinary(&n,sizeof(int));
|
}
|
int writeShort(short val)
|
{
|
int n = htons(val);
|
return pConv->toBinary(&n,sizeof(short));
|
}
|
int writeDouble(double val)
|
{
|
long long d = htond(val);
|
return pConv->toBinary(&d,sizeof(double));
|
}
|
|
int writeFloat(float val)
|
{
|
unsigned int f = htonf(val);
|
return pConv->toBinary(&f,sizeof(unsigned int));
|
}
|
|
int writeByte(char bit)
|
{
|
return pConv->toBinary(&bit,sizeof(char));
|
}
|
int writeString(const char* val)
|
{
|
string str;
|
str.assign(val);
|
return pConv->strToBinary(str);
|
}
|
|
|
int readInt()
|
{
|
int nRet = 0xffffffff;
|
pConv->toType(&nRet,sizeof(int));
|
nRet = ntohl(nRet);
|
|
return nRet;
|
}
|
short readShort()
|
{
|
short sret = 0xffff;
|
pConv->toType(&sret,sizeof(short));
|
|
sret = ntohs(sret);
|
|
return sret;
|
}
|
double readDouble()
|
{
|
long long dRet = 0xffffffffffffffff;
|
pConv->toType(&dRet,sizeof(dRet));
|
double ret = ntohd(dRet);
|
|
return ret;
|
}
|
|
float readFloat()
|
{
|
unsigned int nRet = 0xffffffff;
|
pConv->toType(&nRet,sizeof(unsigned int));
|
float f = ntohf(nRet);
|
|
return f;
|
}
|
char readByte()
|
{
|
char c = 0xff;
|
pConv->toType(&c,sizeof(char));
|
|
return c;
|
}
|
const char* readString()
|
{
|
pConv->toString(tmp_st);
|
return tmp_st.data();
|
}
|
char* getData()
|
{
|
return (char*)pConv->getBuffer();
|
}
|
unsigned int getLenth()
|
{
|
return pConv->getDataSize();
|
}
|
unsigned int getBufSize()
|
{
|
return pConv->getBufferSize();
|
}
|
void attach(void* buf,int size,int dataSize = 0)
|
{
|
pConv->attach(buf,size,data_size);
|
}
|
char* detach()
|
{
|
return (char*)pConv->detach();
|
}
|
int reset(int size)
|
{
|
return pConv->resetData(size);
|
}
|
};
|
|
#endif /* NetStream_hpp */
|