wxlsock.h

Go to the documentation of this file.
00001 /////////////////////////////////////////////////////////////////////////////
00002 // Name:            wxlsock.h
00003 // Purpose:         wxLua Socket interfaces
00004 // Author:          J. Winwood, John Labenski, Ray Gilbert
00005 // Created:         March 2002
00006 // Copyright:       (c) 2002 Lomtick Software. All rights reserved.
00007 // Licence:         wxWidgets licence.
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 // This is the MSW version of SHUT_RDWR for ::shutdown(sock, how=SHUT_RDWR)
00033 // Note that these are defined in winsock2.h, but if you try to include it
00034 // you get errors about redefinitions since it includes winsock.h anyway
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 // wxLuaSocketBase - a base class for different socket implementations
00048 //
00049 // The derived socket class must override
00050 //   virtual int Read(...) all ReadXXX functions use this
00051 //   virtual int Write(...) all WriteXXX functions use this
00052 //   virtual bool IsConnected()
00053 // ----------------------------------------------------------------------------
00054 
00055 class WXDLLIMPEXP_WXLUASOCKET wxLuaSocketBase : public wxObject
00056 {
00057 public:
00058     wxLuaSocketBase() : m_port_number(-1) {}
00059     virtual ~wxLuaSocketBase() {}
00060 
00061     // Get the port used for the socket, -1 if not initialized
00062     int GetPortNumber() const { return m_port_number; }
00063     // Get the address of the socket, empty string if not initialized
00064     wxString GetAddress() const { return m_address; }
00065 
00066     // Is the socket currently connected
00067     virtual bool IsConnected() = 0;
00068 
00069     // Read the number of bytes length into buffer from the socket
00070     //  the buffer must be large enough to hold the data.
00071     virtual int Read(char *buffer, wxUint32 length) = 0;
00072     // Write the whole buffer of number of bytes length to the socket
00073     virtual int Write(const char *buffer, wxUint32 length) = 0;
00074 
00075     // Note: Read/WriteCmd reads/writes a byte and if debugging prints the cmd/event type
00076 
00077     // Read data from the socket, calls virtual int Read(...)
00078     //  false is returned on failure and the input var is not modified
00079     bool ReadCmd(unsigned char& value);
00080     bool ReadInt32(wxInt32& value);
00081     bool ReadLong(long& value); // reads platform independent long using a string
00082     bool ReadString(wxString& value);
00083     bool ReadDebugData(wxLuaDebugData& data);
00084 
00085     // Write data to the socket, calls virtual void Write(...)
00086     //   returns success
00087     bool WriteCmd(char value);
00088     bool WriteInt32(wxInt32 value);
00089     bool WriteLong(long value); // write platform independent long using a string
00090     bool WriteString(const wxString &value);
00091     bool WriteDebugData(const wxLuaDebugData& debugData);
00092 
00093     // Concat the error message array together and optionally clear the message
00094     virtual wxString GetErrorMsg(bool clear_msg);
00095     // Add a message to the internal error message and then append any
00096     //   additional from GetLastErrorMsg() to it.
00097     void AddErrorMessage(const wxString& msg);
00098     // Get the last/current error message as a string
00099     virtual wxString GetLastErrorMsg() const = 0;
00100 
00101     // implementation
00102 
00103     wxString m_name;        // human readable name of socket, for debugging
00104     wxString m_errorMsg;    // human readable error message
00105     wxString m_address;     // The address for the socket to use
00106     int      m_port_number; // The port that's used, else -1
00107 
00108 private:
00109     DECLARE_ABSTRACT_CLASS(wxLuaSocketBase);
00110 };
00111 
00112 // ----------------------------------------------------------------------------
00113 // wxLuaCSocket - a C socket implementation for both client and server
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     // Socket constructor from an accepted socket
00136     wxLuaCSocket(socket_type socket, sockaddr_in address);
00137 
00138     virtual ~wxLuaCSocket();
00139 
00140     // Create a listening socket, using the specified port number
00141     //   server: bind and listen for client connections
00142     bool Listen(u_short port, int backLog = 100);
00143     // Accept a connection, returning an accepted socket.
00144     //   server: block until accepting a connection from a client
00145     wxLuaCSocket* Accept();
00146     // Connect to a given host and port number
00147     //   client: connect a client to a server
00148     bool Connect(const wxString &address, u_short port);
00149     // Get the socket state
00150     SocketState GetState() const { return m_sockstate; }
00151     // Get the socket handle
00152     int GetSocket() const { return m_sock; }
00153 
00154     // Get the address of the opened socket
00155     wxString GetAddress();
00156     // Get the port number of the opened socket
00157     int GetPort();
00158     // Shutdown the socket in an orderly fashion, ::shutdown(sock, how)
00159     //  returns true on success
00160     bool Shutdown(int how = SD_BOTH);
00161     // Close the open socket, returns true on success
00162     bool Close();
00163 
00164     // Is the socket connected?
00165     //   Overridden from wxLuaSocketBase
00166     virtual bool IsConnected()
00167     {
00168         return ((m_sockstate == SOCKET_CONNECTED) ||
00169                 (m_sockstate == SOCKET_ACCEPTED));
00170     }
00171 
00172     // Read the whole buffer of byte size length into buffer from the socket
00173     //   Overridden from wxLuaSocketBase
00174     virtual int Read(char *buffer, wxUint32 length);
00175     // Write the whole buffer of byte size length to the socket
00176     //   Overridden from wxLuaSocketBase
00177     virtual int Write(const char *buffer, wxUint32 length);
00178 
00179     // Get the last/current error message using the system error
00180     //   either errno in Unix or WSAGetLastError in MSW, doesn't clear error
00181     virtual wxString GetLastErrorMsg() const;
00182 
00183 protected:
00184     // Prevent copying and assignment of this class
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 // wxLuawxSocket - Handles Debugger/Debuggee IO
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     // Safely close and destroy the socket
00210     bool Destroy();
00211 
00212     // Get/Set the socket, if you set a new socket you must delete the
00213     //  previous one first
00214     wxSocketBase* GetSocket() const { return m_socket; }
00215     void SetSocket(wxSocketBase* sock) { m_socket = sock; }
00216     // Socket Error from wxSocketBase
00217     bool Error() { return !m_socket || m_socket->Error(); }
00218     // Is Socket Connected, from wxSocketBase
00219     bool WaitForRead(long seconds = 0, long milliseconds = 300) { return m_socket && m_socket->WaitForRead(seconds, milliseconds); }
00220 
00221     // Is Socket Connected, from wxSocketBase
00222     virtual bool IsConnected() { return m_socket && m_socket->IsConnected(); }
00223 
00224     // Read the whole buffer of size length into buffer buffer from the socket
00225     virtual int Read(char *buffer, wxUint32 length);
00226     // Write the whole buffer of size length to the socket
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 // wxLuaSocket - Choose the socket we want to use
00237 // ----------------------------------------------------------------------------
00238 
00239 typedef wxLuaCSocket wxLuaSocket;
00240 
00241 #endif // WX_LUA_SOCKET_H_
Generated on Tue Jul 13 10:30:39 2010 for wxLua by  doxygen 1.6.3