zhangzengfei
2022-07-20 c90c3e794bdd95127d0c34ff1d9e8759d18a0445
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/******************************************************************************
* FILE:    TcpTransport.cpp
* Description:
*    Implementation of the CTcpTransport class.
*
* Modified Code History
* Mark    Date        By            Modification Reason
*******************************************************************************
* 01    2007-10-8    songxw    Initial creation.
******************************************************************************/
 
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <poll.h>
 
#include "TcpTransport.h"
 
CTcpTransport::CTcpTransport() : SockFd(-1)
{
    memset(sTcpClientIpAddr, '\0', sizeof(sTcpClientIpAddr));
    uTcpClientPort = 0;
}
 
CTcpTransport::CTcpTransport(char *pIpAddr, UINT16 port) : SockFd(-1)
{
    strncpy2(sIpAddr, pIpAddr, 32);
    uPort = port;
 
    memset(sTcpClientIpAddr, '\0', sizeof(sTcpClientIpAddr));
    uTcpClientPort = 0;
}
 
CTcpTransport::~CTcpTransport()
{
    Close();
}
 
int CTcpTransport::Create(TcpServiceType_E eType, char *pAddr, UINT16 port)
{
    if((pAddr!= NULL) && (strlen(pAddr) > 0) && (port > 0))
    {
        strncpy2(sIpAddr, pAddr, 32);
        uPort = port;
    }
    sType = eType;
    
    //Create the TCP socket
    SockFd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (-1 == SockFd)
    {
        perror("CTcpTransport socket");
        return -1;
    }
 
    //Set Non-Blocking model
    //fcntl(SockFd, F_SETFL, O_NDELAY);  // O_NONBLOCK
 
    //Create Tcp server or client
    if (TCP_SERVER == sType)
    {
        return Listen();
    }
    else
    {
        if((strlen(sTcpClientIpAddr) > 0) && (uTcpClientPort > 0))
        {
//----------------------------------°ó¶¨Ô´IPºÍ¶Ë¿Ú---------------        
            struct sockaddr_in MyAddr;
            bzero(&MyAddr, sizeof(struct sockaddr_in) );
              MyAddr.sin_family      = AF_INET;
              MyAddr.sin_addr.s_addr = inet_addr(sTcpClientIpAddr);
            MyAddr.sin_port        = htons(uTcpClientPort);
 
            if (bind(SockFd,(struct sockaddr *)&MyAddr, sizeof(struct sockaddr_in) ) == -1)
              {
                   printf("%s socket bind %s %d\r\n",__FUNCTION__,sTcpClientIpAddr,uTcpClientPort);
                perror("bind");
              }
//--------------------------------------------------------------------            
        }
        return Connect();
    }
 
    return 0;
}
 
int CTcpTransport::Close()
{
    if (SockFd != -1)
    {
        close(SockFd);
        SockFd = -1;
    }
 
    return 0;
}
 
int CTcpTransport::Listen(void)
{
    struct sockaddr_in MyAddr;
    struct linger linger;
    int    optval;
 
    bzero(&MyAddr, sizeof(struct sockaddr_in) );
      MyAddr.sin_family      = AF_INET;
      //MyAddr.sin_addr.s_addr = inet_addr(sIpAddr);
    MyAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    MyAddr.sin_port        = htons(uPort);
 
    linger.l_onoff = 1;
    linger.l_linger = 0;
      if (setsockopt(SockFd, SOL_SOCKET, SO_LINGER, (char *)&linger, sizeof(struct linger) ) == -1)
      {
        close(SockFd);
           perror("CTcpTransport set tcp sockopt linger error");
        return -1;
    }
 
    optval = 1;
      if (setsockopt(SockFd, SOL_SOCKET, SO_REUSEADDR, (char *)&optval, sizeof(optval) ) == -1)
      {
        close(SockFd);
           perror("CTcpTransport set tcp sockopt reuse address error");
        return -1;
    }
 
      if (bind(SockFd, (struct sockaddr *)&MyAddr, sizeof(struct sockaddr_in) ) == -1)
      {
        close(SockFd);
           perror("CTcpTransport tcp bind error");
           return -1;
      }
 
    if (listen(SockFd, 20) == -1)
    {
        close(SockFd);
           return -1;
    }
 
    return 0;
}
 
int CTcpTransport::Connect(void)
{
    struct sockaddr_in SvrAddr;
 
    bzero(&SvrAddr, sizeof(struct sockaddr_in) );
    SvrAddr.sin_family = AF_INET;
    SvrAddr.sin_addr.s_addr = inet_addr(sIpAddr);
    SvrAddr.sin_port = htons(uPort);
#if 0    
//------------set O_NONBLOCK------------------------------
    int nFlags = fcntl(SockFd, F_GETFL, 0);
    if(nFlags < 0)
    {
        perror("fcntl F_GETFL");
        close(SockFd);
        return -1;
    }
    if(fcntl(SockFd, F_SETFL, nFlags | O_NONBLOCK) < 0)
     {  
         perror("setting O_NONBLOCK");  
         close(SockFd);  
         return -1;  
     }  
//----------------------------------------------------------    
#endif
    if (connect(SockFd, (struct sockaddr *)&SvrAddr, sizeof(struct sockaddr_in) ) < 0)
    {
        if(errno != EINPROGRESS) 
        {
            perror("connect");
            close(SockFd);
            return -1;
        }
        else
        {
            if(!ReadSelect(100*1000))
            {
                perror("connect");
                close(SockFd);
                return -1;
            }
        }
    }
 
    return 0;
}
 
bool CTcpTransport::ReadSelect(UINT32 timeout)
{
    int res;
    struct timeval tv;
    fd_set wrset;
    int valopt;
    socklen_t sock_len;
 
    if (timeout == 0)
    {
        tv.tv_sec=tv.tv_usec=0;
    }
    else
    { 
        tv.tv_sec = timeout / 1000000; 
        tv.tv_usec = timeout % 1000000; 
    }
 
    FD_ZERO(&wrset);
    FD_SET(SockFd, &wrset);
 
    res = select(SockFd + 1, NULL, &wrset, NULL, &tv);
    if (res > 0)
    {
        sock_len = sizeof(int);
        if(getsockopt(SockFd, SOL_SOCKET, SO_ERROR, (void *) (&valopt), &sock_len) == 0)
        {
            if(!valopt)
                return true;
        }
    }
    return false;
}
 
bool CTcpTransport::IsReadSelect(UINT32 timeout)
{
    int res;
    struct timeval tv;
    fd_set rdset;
 
    if (timeout == 0)
    {
        tv.tv_sec=tv.tv_usec=0;
    }
    else
    { 
        tv.tv_sec = timeout / 1000000; 
        tv.tv_usec = timeout % 1000000; 
    }
 
    FD_ZERO(&rdset);
    FD_SET(SockFd, &rdset);
 
    res = select(SockFd + 1, &rdset, NULL, NULL, &tv);
    if (res > 0)
    {
        if(FD_ISSET(SockFd,&rdset))
        {
            return true;
        }
            
    }
    return false;
}
 
 
int CTcpTransport::Send(UINT8 *pBuf, int len)
{
#if defined(TCP_SUPPORT_TPKT)
    //TPKT 4 bytes
    pBuf[0] = 0x03;
    pBuf[1] = 0x00;
    *(UINT16 *)(pBuf+2) = htons( (UINT16)(len - TPKT_MAX_LEN) );
#endif
 
    UINT8* p = pBuf;
    int    iRet = 0, WLen = 0;
    do {
        iRet = send(SockFd, p+WLen, (len - WLen), 0);
        if (iRet <= 0)  //(-1 == ret) && (errno != EINTR)
        {
            perror("send falid");
            return iRet;
        }
        WLen += iRet;
    } while (len > WLen);
 
    return WLen;
}
 
int CTcpTransport::Read(UINT8 *pBuf, int BufLen, int param)
{
    int iRet = recv(SockFd, pBuf, BufLen, param);
    if (iRet <= 0)
    {
        return -1;
    }
 
    return iRet;
}
 
 
int CTcpTransport::ServerRead(UINT8 *pBuf, int BufLen, int param)
{
    struct sockaddr_in pin;
    socklen_t  address_size = sizeof(pin);
    int ConnFd = accept(SockFd, (struct sockaddr *)&pin, &address_size);
    int iRet = recv(ConnFd, pBuf, BufLen, param);
    if (iRet <= 0)
    {
        return -1;
    }
 
    close(ConnFd);
 
    return iRet;
}
 
 
 
int CTcpTransport::GetSockName(char *pIpAddr, UINT16 &port)
{
    struct sockaddr_in addr;
    socklen_t len = sizeof(sockaddr_in);
 
    //get socket addr and port info
    if (getsockname(SockFd, (struct sockaddr *)&addr, &len) == -1)
    {
        perror("getsockname");
        return -1;
    }
 
    //strncpy2(pIpAddr, (char *)inet_ntoa(addr.sin_addr), IPSTR_MAX_LEN);
    inet_ntop(AF_INET, (struct in_addr *)&addr.sin_addr, pIpAddr, 32);  //ʹÓÃḬ̈߳²È«º¯Êý
    port = ntohs(addr.sin_port);
 
    return 0;
}