wxlsock.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef WX_LUA_SOCKET_H_
00011 #define WX_LUA_SOCKET_H_
00012
00013 #include "wxluasocket/include/wxluasocketdefs.h"
00014
00015 class WXDLLIMPEXP_WXLUADEBUG wxLuaDebugData;
00016
00017 #ifdef WIN32
00018 typedef int socklen_t;
00019 #include <winsock.h>
00020 #else
00021 #include <unistd.h>
00022 #include <sys/types.h>
00023 #include <sys/socket.h>
00024 #include <netinet/in.h>
00025 #include <cerrno>
00026 #include <netdb.h>
00027 #include <arpa/inet.h>
00028 #define INVALID_SOCKET -1
00029 #define SOCKET_ERROR -1
00030 #endif // !WIN32
00031
00032
00033
00034
00035 #if !defined(SD_RECEIVE) && defined(SHUT_RD)
00036 #define SD_RECEIVE SHUT_RD
00037 #define SD_SEND SHUT_WR
00038 #define SD_BOTH SHUT_RDWR
00039 #elif !defined(SD_RECEIVE)
00040 #define SD_RECEIVE 0
00041 #define SD_SEND 1
00042 #define SD_BOTH 2
00043 #endif // SD_RECEIVE
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 class WXDLLIMPEXP_WXLUASOCKET wxLuaSocketBase : public wxObject
00056 {
00057 public:
00058 wxLuaSocketBase() : m_port_number(-1) {}
00059 virtual ~wxLuaSocketBase() {}
00060
00061
00062 int GetPortNumber() const { return m_port_number; }
00063
00064 wxString GetAddress() const { return m_address; }
00065
00066
00067 virtual bool IsConnected() = 0;
00068
00069
00070
00071 virtual int Read(char *buffer, wxUint32 length) = 0;
00072
00073 virtual int Write(const char *buffer, wxUint32 length) = 0;
00074
00075
00076
00077
00078
00079 bool ReadCmd(unsigned char& value);
00080 bool ReadInt32(wxInt32& value);
00081 bool ReadLong(long& value);
00082 bool ReadString(wxString& value);
00083 bool ReadDebugData(wxLuaDebugData& data);
00084
00085
00086
00087 bool WriteCmd(char value);
00088 bool WriteInt32(wxInt32 value);
00089 bool WriteLong(long value);
00090 bool WriteString(const wxString &value);
00091 bool WriteDebugData(const wxLuaDebugData& debugData);
00092
00093
00094 virtual wxString GetErrorMsg(bool clear_msg);
00095
00096
00097 void AddErrorMessage(const wxString& msg);
00098
00099 virtual wxString GetLastErrorMsg() const = 0;
00100
00101
00102
00103 wxString m_name;
00104 wxString m_errorMsg;
00105 wxString m_address;
00106 int m_port_number;
00107
00108 private:
00109 DECLARE_ABSTRACT_CLASS(wxLuaSocketBase);
00110 };
00111
00112
00113
00114
00115
00116 #ifdef WIN32
00117 typedef SOCKET socket_type;
00118 #else
00119 typedef int socket_type;
00120 #endif
00121
00122 class WXDLLIMPEXP_WXLUASOCKET wxLuaCSocket : public wxLuaSocketBase
00123 {
00124 public:
00125
00126 enum SocketState
00127 {
00128 SOCKET_CLOSED,
00129 SOCKET_LISTENING,
00130 SOCKET_ACCEPTED,
00131 SOCKET_CONNECTED
00132 };
00133
00134 wxLuaCSocket();
00135
00136 wxLuaCSocket(socket_type socket, sockaddr_in address);
00137
00138 virtual ~wxLuaCSocket();
00139
00140
00141
00142 bool Listen(u_short port, int backLog = 100);
00143
00144
00145 wxLuaCSocket* Accept();
00146
00147
00148 bool Connect(const wxString &address, u_short port);
00149
00150 SocketState GetState() const { return m_sockstate; }
00151
00152 int GetSocket() const { return m_sock; }
00153
00154
00155 wxString GetAddress();
00156
00157 int GetPort();
00158
00159
00160 bool Shutdown(int how = SD_BOTH);
00161
00162 bool Close();
00163
00164
00165
00166 virtual bool IsConnected()
00167 {
00168 return ((m_sockstate == SOCKET_CONNECTED) ||
00169 (m_sockstate == SOCKET_ACCEPTED));
00170 }
00171
00172
00173
00174 virtual int Read(char *buffer, wxUint32 length);
00175
00176
00177 virtual int Write(const char *buffer, wxUint32 length);
00178
00179
00180
00181 virtual wxString GetLastErrorMsg() const;
00182
00183 protected:
00184
00185 wxLuaCSocket(const wxLuaCSocket&);
00186 wxLuaCSocket& operator=(const wxLuaCSocket&);
00187
00188 socket_type m_sock;
00189 sockaddr_in m_sockaddress;
00190 SocketState m_sockstate;
00191
00192 private:
00193 DECLARE_ABSTRACT_CLASS(wxLuaCSocket);
00194 };
00195
00196
00197
00198
00199 #include "wx/socket.h"
00200
00201 class WXDLLIMPEXP_WXLUASOCKET wxLuawxSocket : public wxLuaSocketBase
00202 {
00203 public:
00204 wxLuawxSocket() : m_socket(NULL) {}
00205 wxLuawxSocket(wxSocketBase* sock) : m_socket(sock) {}
00206
00207 virtual ~wxLuawxSocket() { Destroy(); }
00208
00209
00210 bool Destroy();
00211
00212
00213
00214 wxSocketBase* GetSocket() const { return m_socket; }
00215 void SetSocket(wxSocketBase* sock) { m_socket = sock; }
00216
00217 bool Error() { return !m_socket || m_socket->Error(); }
00218
00219 bool WaitForRead(long seconds = 0, long milliseconds = 300) { return m_socket && m_socket->WaitForRead(seconds, milliseconds); }
00220
00221
00222 virtual bool IsConnected() { return m_socket && m_socket->IsConnected(); }
00223
00224
00225 virtual int Read(char *buffer, wxUint32 length);
00226
00227 virtual int Write(const char *buffer, wxUint32 length);
00228
00229 virtual wxString GetLastErrorMsg() const;
00230
00231 protected:
00232 wxSocketBase* m_socket;
00233 };
00234
00235
00236
00237
00238
00239 typedef wxLuaCSocket wxLuaSocket;
00240
00241 #endif // WX_LUA_SOCKET_H_