00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef _sock_h
00018 #define _sock_h
00019
00020 #include <spl/types.h>
00021 #include <spl/Debug.h>
00022
00023 #ifdef _WINDOWS
00024 #include <spl/configwin32.h>
00025 #else
00026 #include <spl/autoconf/config.h>
00027 #endif
00028
00029 #include <spl/collection/Array.h>
00030 #include <spl/io/IStream.h>
00031 #include <spl/Exception.h>
00032 #include <spl/Memory.h>
00033 #include <spl/RefCountPtr.h>
00034 #include <spl/String.h>
00035 #include <spl/WeakReference.h>
00036
00037 #ifdef HAVE_ERRNO_H
00038 #include <errno.h>
00039 #endif
00040 #include <sys/types.h>
00041
00042 #if defined(_WINDOWS)
00043
00044 #define _WINSOCKAPI_
00045 #include <spl/cleanwindows.h>
00046 #include <winsock2.h>
00047 #include <spl/configwin32.h>
00048 #endif
00049
00050 #include <sys/types.h>
00051 #ifdef HAVE_NETINET_IN_H
00052 #include <netinet/in.h>
00053 #endif
00054 #ifdef HAVE_SYS_SOCKET_H
00055 #include <sys/socket.h>
00056 #endif
00057 #ifdef HAVE_ERRNO_H
00058 #include <errno.h>
00059 #endif
00060 #ifdef HAVE_UNISTD_H
00061 #include <unistd.h>
00062 #endif
00063 #ifdef HAVE_SYS_FCNTL_H
00064 #include <sys/fcntl.h>
00065 #endif
00066 #ifdef HAVE_FCNTL_H
00067 #include <fcntl.h>
00068 #endif
00069 #ifdef HAVE_NETINET_TCP_H
00070 #include <netinet/tcp.h>
00071 #endif
00072 #ifdef HAVE_ARPA_INET_H
00073 #include <arpa/inet.h>
00074 #endif
00075 #ifdef HAVE_NETDB_H
00076 #include <netdb.h>
00077 #endif
00078
00079 #if !defined(SOCKET) && !defined(_WINDOWS)
00080 typedef int SOCKET;
00081 #endif
00082 #if !defined(_WINDOWS)
00083 typedef struct sockaddr_in SOCKADDR_IN;
00084 #endif
00085 #if !defined(_WINDOWS)
00086 #define closesocket(A) close(A)
00087 #define INVALID_SOCKET -1
00088 #endif
00089 #if !defined(SD_BOTH)
00090 #define SD_BOTH SHUT_RDWR
00091 #endif
00092 #ifdef HAVE_SYS_TIME_H
00093 #include <sys/time.h>
00094 #endif
00095 #ifdef HAVE_SYS_IOCTL_H
00096 #include <sys/ioctl.h>
00097 #endif
00098 #ifdef HAVE_TIME_H
00099 #include <time.h>
00100 #endif
00101
00113 #define SOCKBUF_SIZE 1024
00114
00115 class Socket;
00116 typedef RefCountPtr<Socket> SocketPtr;
00117 typedef WeakReference<Socket, SocketPtr> SocketRef;
00118
00119 REGISTER_TYPEOF(48, SocketPtr);
00120 REGISTER_TYPEOF( 474, SocketRef );
00121
00126 class Socket : public IMemoryValidate
00127 {
00128 private:
00129
00130 protected:
00131 static bool m_inited;
00132
00133 int m_errorStatus;
00134 SOCKET m_fd;
00135 SOCKADDR_IN m_saAddr;
00136 bool m_closed;
00137
00138 void SetAddrReuse(int i);
00139
00140 static void sinit();
00141
00142 Socket(SOCKET fd, bool dummy);
00143 void Init();
00144
00145 Socket ( int port, int family, int streamType, int protocol );
00146
00147 public:
00148 Socket();
00149 Socket(const Socket& sock);
00150 Socket ( int port );
00151 ~Socket();
00152
00153 Socket& operator =(const Socket& sock);
00154
00155 inline void SetAddrReuseOn() { SetAddrReuse(1); }
00156
00157 void SetNonBlocking();
00158 void SetBlocking();
00159
00160 void Send (const Array<byte>& data, const int offset, const int len);
00161 inline void Send (const Array<byte>& data, const int len) { Send(data, 0, len); }
00162 inline void Send (const Array<byte>& data) { Send(data, data.Length()); }
00163 inline void Send (const String& str) { Send(str.ToByteArray()); }
00164
00165
00166 int Recv (Array<byte>& buf, const int offset, const int blen);
00167 inline int Recv (Array<byte>& buf, const int blen) { return Recv(buf, 0, blen); }
00168 int RecvByte();
00169
00170 void Flush();
00171 int GetBytesAvail();
00172
00173 inline SOCKET GetFD() const { return m_fd; }
00174 inline const SOCKADDR_IN *GetAddr() const { return &m_saAddr; }
00175
00176 virtual bool IsClosed();
00177
00178 inline void Close()
00179 {
00180 if (!m_closed)
00181 {
00182 m_closed = true;
00183 ::closesocket(m_fd);
00184 }
00185 }
00186
00187 void Shutdown( int i=SD_BOTH );
00188
00189 void SetLingerOn();
00190 void SetLingerOff();
00191 void SetNoDelay();
00192
00193 void SetSendTimeout(int toMS);
00194 void SetRecvTimeout(int toMS);
00195 int GetSendTimeout();
00196 int GetRecvTimeout();
00197
00198 inline int GetErrorCode() const { return m_errorStatus; }
00199
00200 String GetRemoteIp( );
00201
00202 static void ShutdownService();
00203
00204 friend class ServerSocket;
00205 friend class TcpSocket;
00206 friend class UdpSocket;
00207
00208 #if defined(DEBUG) || defined(_DEBUG)
00209 void CheckMem() const;
00210 void ValidateMem() const;
00211 #endif
00212 };
00213
00214 REGISTER_TYPEOF(50, Socket);
00215
00219 #endif