1 /***************************************************************************
2 *  mgsocket.h
3 *
4 *  Wed Sep  6 22:19:52 2006
5 *  Copyright  2006  liubin,China
6 *  Email multiget@gmail.com
7 ****************************************************************************/
8 
9 /*
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23  */
24 #ifndef _MG_SOCKET_H
25 #define _MG_SOCKET_H
26 
27 #ifdef WIN32
28 #include <winsock2.h>
29 //#pragma comment(lib,"ws2_32.lib")
30 #else
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <arpa/inet.h>
34 #include <unistd.h>
35 #include <sys/un.h>
36 #include <netdb.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #endif
40 
41 #if defined(__BSD__)||defined(__FreeBSD__)||defined(__OpenBSD__)||defined(__NetBSD__)	//wx preprocessor
42 //bsd need it, mac do not
43 #include <netinet/in.h>
44 #endif
45 
46 /*
47 
48 CMgSocket* -> CMgProxySocket -> CMgHttpSocket -> CMgHttpBase -> CMgHttpInfo
49 							|								\->CMgHttpAnts
50 							\-> CMgFtpSocket -> CMgFtpBase -> CMgFtpInfo
51 															\->CMgFtpAnts
52 */
53 
54 /*
55 Error code range define;
56 //use the last 8 bit
57 CMgSocket [0x000000**~0x000000**]
58 */
59 
60 #include "common.h"
61 
62 class CMgSocket
63 {
64 
65 public:
66     CMgSocket();
67     virtual ~CMgSocket();
68 
69 public:
70     bool Send( const void* buf, int buflen ); //block
71     int Read( void* buf, int buflen ); //block
72     bool SetTimeOut( unsigned int nsec );
73     void SetConnectTimeOut( unsigned int nsec );
74     virtual bool Connect( const char* server, int port ); //block
75 #ifdef WIN32
76 
77     int tConnect( SOCKET sock, const struct sockaddr *addr, unsigned int len );
78 #else
79 
80     int tConnect( int sock, const struct sockaddr *addr, unsigned int len );
81 #endif
82 
GetLastError()83     int GetLastError()
84     {
85         return m_nLastError;
86     };
87 
88     bool Create();
89     void Close();
90 
91 protected:
92     int m_nLastError;  //V
93     bool m_bConnected; //V
94     unsigned int m_nConnectTimeOut;
95     unsigned int m_nTimeOut;
96 
97 private:
98 
99 #ifdef WIN32
100 
101     SOCKET m_Sock;
102 #else
103 
104     int m_Sock;
105 #endif
106 
107     bool m_bSockValid; //V
108 
109 };
110 
111 #endif
112 
113