单军华
2017-07-12 20d1260d26b028897f3c0935c12fc35aa37b2e93
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//
//  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 */