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
/******************************************************************************
* FILE:    TcpTransport.h
* Description:    
*    Interface for the TCP transport class.
*
* Modified Code History
* Mark    Date        By            Modification Reason
*******************************************************************************
* 01    2007-10-8    songxw    Initial creation.
******************************************************************************/
 
#if !defined(__TCP_TRANSPORT_H__)
#define __TCP_TRANSPORT_H__
 
using namespace std;
 
#include <time.h>
#include <netinet/in.h>
#include "TypeDef.h"
#include "str.h"
 
 
typedef enum
{
    TCP_CLIENT,
    TCP_SERVER
}TcpServiceType_E;
 
#define TPKT_MAX_LEN  4
#define RTSP_PACKET_LEN_WITHOUT_BODY  1024
#define STREAM_BUFFER_MAX_LEN         (1024 * 100)
#define TCP_STREAM_BUFFER_MAX_LEN         (1024 * 50)
 
/* TCP transport class for create and destroy TCP socket programme, receive and send TCP packet */
class CTcpTransport  
{
public:
    CTcpTransport();
    CTcpTransport(char *pIpAddr, UINT16 port);
    virtual ~CTcpTransport();
    int Create(TcpServiceType_E eType = TCP_CLIENT, char *pAddr = NULL, UINT16 port = 0);
    int Listen(void);
    bool ReadSelect(UINT32 timeout);
    bool IsReadSelect(UINT32 timeout);
    int Connect(void);
    int Send(UINT8 *pBuf, int len);
    int Read(UINT8 *pBuf, int BufLen, int param = 0);
    int ServerRead(UINT8 *pBuf, int BufLen, int param = 0);
    int Close(void);
    
    int GetSockName(char *pIpAddr, UINT16 &port);
 
    int SetTcpClientSrcInfo(const char *pIpAddr, UINT16 port)
    {
        if((pIpAddr!= NULL) && (strlen(pIpAddr) > 0) && (port>0))
        {
            strncpy2(sTcpClientIpAddr, pIpAddr, 32);
            uTcpClientPort = port;
            return 0;
        }
        return -1;
    }
public:
    TcpServiceType_E sType;
    int SockFd;
    
    //remote ip address and port
    char   sIpAddr[32+1];
    UINT16 uPort;
 
    //tcpclient srcip and srcport
    //mrs ·¢ËÍÂëÁ÷µÄʱºò Òª¹Ì¶¨Ô´Port
    char     sTcpClientIpAddr[32+1];
    UINT16    uTcpClientPort;
};
 
#endif    //#if !defined(__TCP_TRANSPORT_H__)