Update of /cvsroot/wxlua/wxLua/modules/wxluasocket/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv23333/wxLua/modules/wxluasocket/src Modified Files: Makefile Added Files: wxldbgio.cpp wxldserv.cpp wxldtarg.cpp wxlsock.cpp Removed Files: debugio.cpp dserver.cpp dtarget.cpp socket.cpp Log Message: rename wxluasocket files to wxl* for consistency and make it easier to find them --- NEW FILE: wxldbgio.cpp --- ///////////////////////////////////////////////////////////////////////////// // Name: wxLuaDebugIO.cpp // Purpose: Debugging I/O functions for wxLua // Author: J. Winwood // Created: May 2002 // Copyright: (c) 2002 Lomtick Software. All rights reserved. // Licence: wxWidgets licence ///////////////////////////////////////////////////////////////////////////// #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) #pragma implementation "wxldbgio.h" #endif #include "wx/wxprec.h" #ifdef __BORLANDC__ #pragma hdrstop #endif #ifndef WX_PRECOMP #include "wx/wx.h" #endif #include "wxluasocket/include/wxldbgio.h" #include "wxluasocket/include/wxlsock.h" #include "wxlua/include/internal.h" #include "wxluadebug/include/debug.h" // ---------------------------------------------------------------------------- // wxLuaDebugIO // ---------------------------------------------------------------------------- unsigned char wxLuaDebugIO::ReadByte(wxLuaSocket *pSocket) { unsigned char value = 0; pSocket->Read((char *) &value, sizeof(value)); return value; } short wxLuaDebugIO::ReadWord(wxLuaSocket *pSocket) { short value = 0; pSocket->Read((char *)&value, sizeof(value)); return value; } unsigned short wxLuaDebugIO::ReadUWord(wxLuaSocket *pSocket) { unsigned short value = 0; pSocket->Read((char *)&value, sizeof(value)); return value; } int wxLuaDebugIO::ReadInt(wxLuaSocket *pSocket) { int value = 0; pSocket->Read((char *) &value, sizeof(value)); return value; } unsigned int wxLuaDebugIO::ReadUInt(wxLuaSocket *pSocket) { unsigned int value = 0; pSocket->Read((char *) &value, sizeof(value)); return value; } long wxLuaDebugIO::ReadLong(wxLuaSocket *pSocket) { long value = 0; pSocket->Read((char *) &value, sizeof(value)); return value; } unsigned long wxLuaDebugIO::ReadULong(wxLuaSocket *pSocket) { unsigned long value = 0; pSocket->Read((char *) &value, sizeof(value)); return value; } wxString wxLuaDebugIO::ReadString(wxLuaSocket *pSocket) { wxString value; unsigned int length = ReadInt(pSocket); if (length > 0) { char *buffer = new char[length + 1]; memset(buffer, 0, length+1); pSocket->Read(buffer, length); buffer[length] = 0; value = lua2wx(buffer); delete[] buffer; } return value; } wxLuaDebugData *wxLuaDebugIO::ReadDebugData(wxLuaSocket *pSocket) { wxLuaDebugData *pSortedList = new wxLuaDebugData(); try { size_t idx, idxMax; pSocket->Read((char *) &idxMax, sizeof(idxMax)); for (idx = 0; idx < idxMax; ++idx) { int bufferLength; pSocket->Read((char *) &bufferLength, sizeof(bufferLength)); if (bufferLength > 0) { char *pBuffer = new char[bufferLength]; char *pMemory = pBuffer; pSocket->Read(pMemory, bufferLength); int nReference = *(int *) pMemory; pMemory += sizeof(int); int nIndex = *(int *) pMemory; pMemory += sizeof(int); bool fExpanded = (0 != *pMemory++); const char *pNamePtr = pMemory; pMemory += strlen(pNamePtr) + 1; const char *pTypePtr = pMemory; pMemory += strlen(pTypePtr) + 1; const char *pValuePtr = pMemory; wxLuaDebugDataItem *pItem = new wxLuaDebugDataItem(lua2wx(pNamePtr), lua2wx(pTypePtr), lua2wx(pValuePtr), wxEmptyString, // FIXME! What goes here!? nReference, nIndex, fExpanded); pSortedList->Add(pItem); delete[] pBuffer; } } } catch(wxLuaSocketException & /*e*/) { delete pSortedList; pSortedList = NULL; } return pSortedList; } bool wxLuaDebugIO::WriteByte(wxLuaSocket *pSocket, unsigned char value) { bool result = false; try { pSocket->Write((const char *) &value, sizeof(value)); result = true; } catch(wxLuaSocketException & /*e*/) { } return result; } bool wxLuaDebugIO::WriteWord(wxLuaSocket *pSocket, short value) { bool result = false; try { pSocket->Write((const char *) &value, sizeof(value)); result = true; } catch(wxLuaSocketException & /*e*/) { } return result; } bool wxLuaDebugIO::WriteUWord(wxLuaSocket *pSocket, unsigned short value) { bool result = false; try { pSocket->Write((const char *) &value, sizeof(value)); result = true; } catch(wxLuaSocketException & /*e*/) { } return result; } bool wxLuaDebugIO::WriteInt(wxLuaSocket *pSocket, int value) { bool result = false; try { pSocket->Write((const char *) &value, sizeof(value)); result = true; } catch(wxLuaSocketException & /*e*/) { } return result; } bool wxLuaDebugIO::WriteUInt(wxLuaSocket *pSocket, unsigned int value) { bool result = false; try { pSocket->Write((const char *) &value, sizeof(value)); result = true; } catch(wxLuaSocketException & /*e*/) { } return result; } bool wxLuaDebugIO::WriteLong(wxLuaSocket *pSocket, long value) { bool result = false; try { pSocket->Write((const char *) &value, sizeof(value)); result = true; } catch(wxLuaSocketException & /*e*/) { } return result; } bool wxLuaDebugIO::WriteULong(wxLuaSocket *pSocket, unsigned long value) { bool result = false; try { pSocket->Write((const char *) &value, sizeof(value)); result = true; } catch(wxLuaSocketException & /*e*/) { } return result; } bool wxLuaDebugIO::WriteString(wxLuaSocket *pSocket, const wxString &value) { bool result = FALSE; try { LuaCharBuffer buf(value); int buflen = buf.Length(); pSocket->Write((const char *) &buflen, sizeof(buflen)); if (buflen > 0) { pSocket->Write(buf.GetData(), buflen); } result = TRUE; } catch(wxLuaSocketException & /*e*/) { } return result; } bool wxLuaDebugIO::WriteDebugData(wxLuaSocket *pSocket, const wxLuaDebugData *pSortedList) { bool result = false; size_t idx, idxMax = pSortedList->Count(); pSocket->Write((const char *) &idxMax, sizeof(idxMax)); for (idx = 0; idx < idxMax; ++idx) { try { const wxLuaDebugDataItem *item = pSortedList->Item(idx); int nameLength = item->GetName().Length() + 1; int typeLength = item->GetType().Length() + 1; int valueLength = item->GetValue().Length() + 1; int bufferLength = (2 * sizeof(int)) + sizeof(unsigned char) + nameLength + typeLength + valueLength; unsigned char *pBuffer = new unsigned char[bufferLength]; unsigned char *pMemory = pBuffer; pSocket->Write((const char *) &bufferLength, sizeof(bufferLength)); *(int *) pMemory = item->GetReference(); pMemory += sizeof(int); *(int *) pMemory = item->GetIndex(); pMemory += sizeof(int); *pMemory++ = item->IsExpanded(); memcpy(pMemory, wx2lua(item->GetName()), nameLength); pMemory += nameLength; memcpy(pMemory, wx2lua(item->GetType()), typeLength); pMemory += typeLength; memcpy(pMemory, wx2lua(item->GetValue()), valueLength); pSocket->Write((const char *) pBuffer, bufferLength); delete[] pBuffer; result = true; } catch(wxLuaSocketException & /*e*/) { } if (result == false) break; } return result; } --- debugio.cpp DELETED --- --- socket.cpp DELETED --- --- NEW FILE: wxldserv.cpp --- ///////////////////////////////////////////////////////////////////////////// // Name: wxLuaDebugServer.cpp // Purpose: Provide remote debugging support for wxLua. // Author: J. Winwood // Created: May 2002. // Copyright: (c) 2002 Lomtick Software. All rights reserved. // Licence: wxWidgets licence ///////////////////////////////////////////////////////////////////////////// #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) #pragma implementation "wxldserv.h" #endif #include "wx/wxprec.h" #ifdef __BORLANDC__ #pragma hdrstop #endif #ifndef WX_PRECOMP #include "wx/wx.h" #endif #include "wx/thread.h" #include "wxluasocket/include/wxldserv.h" #include "wxlua/include/internal.h" #include "wxluasocket/include/wxlhandl.h" #if wxCHECK_VERSION(2, 3, 0) #include "wxluadebug/include/staktree.h" #endif // wxCHECK_VERSION(2, 3, 0) #if !wxCHECK_VERSION(2, 6, 0) #define wxMilliSleep wxUsleep #endif // !wxCHECK_VERSION(2, 6, 0) wxString g_strProgramName; wxString g_strNetworkName; // ---------------------------------------------------------------------------- // wxLuaDebugServer // ---------------------------------------------------------------------------- IMPLEMENT_CLASS(wxLuaDebugServer, wxLuaDebuggerBase) wxLuaDebugServer::wxLuaDebugServer(int portNumber) : m_serverSocket(NULL), m_acceptedSocket(NULL), m_processID(-1), m_portNumber(portNumber), m_pProcess(NULL), m_pThread(NULL), m_pStackFrameDialog(NULL), m_fShutdown(false) { StartServer(); } bool wxLuaDebugServer::StartServer() { m_serverSocket = new wxLuaSocket; if (m_serverSocket != NULL) { try { // listen on the specified port m_serverSocket->Listen(m_portNumber); } catch(wxLuaSocketException & e) { NotifyError(wxString::Format(_("Error while opening listening socket. %s"), e.description().c_str())); m_fShutdown = true; } } return (m_serverSocket != NULL); } bool wxLuaDebugServer::StartClient() { if ((m_pProcess == NULL) && StartServerThread()) { m_pProcess = new wxProcess(); wxString command = wxString::Format(wxT("%s -d%s:%u"), g_strProgramName.c_str(), #ifdef __WXMSW__ g_strNetworkName.c_str(), #else wxT("localhost"), #endif m_portNumber); m_processID = wxExecute(command, FALSE, m_pProcess); return (m_processID != -1); } return false; } bool wxLuaDebugServer::StartServerThread() { if (!m_fShutdown) { m_pThread = new wxLuaThread(this); return ((m_pThread != NULL) && (m_pThread->Create() == wxTHREAD_NO_ERROR) && (m_pThread->Run() == wxTHREAD_NO_ERROR)); } return false; } bool wxLuaDebugServer::StopServer() { // Set the shutdown flag m_fShutdown = true; // close the session socket if (m_acceptedSocket != NULL) { try { m_acceptedSocket->Shutdown(SD_BOTH); } catch(wxLuaSocketException & /*e*/) { NotifyError(_("Error while shutting down session socket")); } wxMilliSleep(100); wxLuaSocket *acceptedSocket = m_acceptedSocket; m_acceptedSocket = NULL; delete acceptedSocket; } // close the server socket if (m_serverSocket != NULL) { // close the server socket by connecting // to the socket, thus completing the // 'accept'. If a client has not connected, // this code will satisfy the accept // the m_fShutdown flag will be set // so the thread will not loop and instead // will just destroy the session socket // object and return. try { wxLuaSocket closeSocket; #ifdef __WXMSW__ closeSocket.Connect(g_strNetworkName, m_portNumber); #else closeSocket.Connect(wxT("localhost"), m_portNumber); #endif closeSocket.Shutdown(SD_BOTH); wxMilliSleep(100); wxLuaSocket *serverSocket = m_serverSocket; m_serverSocket = NULL; delete serverSocket; } catch(wxLuaSocketException & /*e*/) { NotifyError(_("Error while shutting down server socket")); } } // One of the above two operations // terminates the thread. // Wait for it to stop. if (m_pThread != NULL) m_pThread->Wait(); return true; } wxLuaDebugServer::~wxLuaDebugServer() { StopServer(); if (m_pThread != NULL) delete m_pThread; #if wxCHECK_VERSION(2, 3, 0) if (m_pProcess != NULL && m_processID != -1) m_pProcess->Kill(m_processID, wxSIGKILL); #endif } bool wxLuaDebugServer::AddBreakPoint(const wxString &fileName, int lineNumber) { if (m_acceptedSocket != NULL) { try { if (WriteByte(m_acceptedSocket, CMD_ADD_BREAKPOINT) && WriteString(m_acceptedSocket, fileName) && WriteInt(m_acceptedSocket, lineNumber)) { return true; } } catch(wxLuaSocketException & /*e*/) { } } return false; } bool wxLuaDebugServer::RemoveBreakPoint(const wxString &fileName, int lineNumber) { if (m_acceptedSocket != NULL) { try { if (WriteByte(m_acceptedSocket, CMD_REMOVE_BREAKPOINT) && WriteString(m_acceptedSocket, fileName) && WriteInt(m_acceptedSocket, lineNumber)) { return true; } } catch(wxLuaSocketException & /*e*/) { } } return false; } bool wxLuaDebugServer::ClearAllBreakPoints() { if (m_acceptedSocket != NULL) { try { if (WriteByte(m_acceptedSocket, CMD_CLEAR_ALL_BREAKPOINTS)) return true; } catch(wxLuaSocketException & /*e*/) { } } return false; } void wxLuaDebugServer::NotifyError(const wxString &msg) { wxLuaDebugEvent debugEvent(wxEVT_DEBUG_ERROR); debugEvent.SetMessage(msg); wxLuaHandler::GetHandler().AddPendingEvent(debugEvent); } bool wxLuaDebugServer::Compile(const wxString &fileName, const wxString &buffer) { bool fErrorsSeen = false; lua_State *luaState = lua_open(); luaopen_base(luaState); luaopen_table(luaState); luaopen_string(luaState); luaopen_math(luaState); luaopen_debug(luaState); luaopen_io(luaState); luaopen_loadlib(luaState); LuaCharBuffer charbuf(buffer); int status = luaL_loadbuffer(luaState, charbuf.GetData(), charbuf.Length(), wx2lua(fileName)); switch (status) { case LUA_ERRRUN: NotifyError(_("Error while compiling chunk\n")); fErrorsSeen = true; break; case LUA_ERRSYNTAX: { wxString strError(_("Syntax error during pre-compilation: ")); strError += lua2wx(lua_tostring(luaState, -1)); strError += wxT("\n"); NotifyError(strError); } fErrorsSeen = true; break; case LUA_ERRMEM: NotifyError(_("Memory allocation error\n")); fErrorsSeen = true; break; case LUA_ERRERR: NotifyError(_("Generic error or an error occurred while running the error handler\n")); fErrorsSeen = true; break; case LUA_ERRFILE: { wxString strError(_("Error occurred while opening file: ")); strError += lua2wx(lua_tostring(luaState, -1)); strError += wxT("\n"); NotifyError(strError); } fErrorsSeen = true; break; default: NotifyError(_("Compiled OK\n")); break; } lua_close(luaState); return !fErrorsSeen; } bool wxLuaDebugServer::Run(const wxString &fileName, const wxString &buffer) { if (m_acceptedSocket != NULL) { try { if (WriteByte(m_acceptedSocket, CMD_RUN_BUFFER) && WriteString(m_acceptedSocket, fileName) && WriteString(m_acceptedSocket, buffer)) { return true; } } catch(wxLuaSocketException & /*e*/) { } } return false; } bool wxLuaDebugServer::Step() { if (m_acceptedSocket != NULL) { try { return WriteByte(m_acceptedSocket, CMD_DEBUG_STEP); } catch(wxLuaSocketException & /*e*/) { } } return false; } bool wxLuaDebugServer::StepOver() { if (m_acceptedSocket != NULL) { try { return WriteByte(m_acceptedSocket, CMD_DEBUG_STEPOVER); } catch(wxLuaSocketException & /*e*/) { } } return false; } bool wxLuaDebugServer::StepOut() { if (m_acceptedSocket != NULL) { try { return WriteByte(m_acceptedSocket, CMD_DEBUG_STEPOUT); } catch(wxLuaSocketException & /*e*/) { } } return false; } bool wxLuaDebugServer::Continue() { if (m_acceptedSocket != NULL) { try { return WriteByte(m_acceptedSocket, CMD_DEBUG_CONTINUE); } catch(wxLuaSocketException & /*e*/) { } } return false; } bool wxLuaDebugServer::Break() { if (m_acceptedSocket != NULL) { try { return WriteByte(m_acceptedSocket, CMD_DEBUG_BREAK); } catch(wxLuaSocketException & /*e*/) { } } return false; } bool wxLuaDebugServer::EnumerateStack() { if (m_acceptedSocket != NULL) { try { return WriteByte(m_acceptedSocket, CMD_ENUMERATE_STACK); } catch(wxLuaSocketException & /*e*/) { } } return false; } bool wxLuaDebugServer::Reset() { if (m_acceptedSocket != NULL) { try { return WriteByte(m_acceptedSocket, CMD_RESET); } catch(wxLuaSocketException & /*e*/) { } } return false; } bool wxLuaDebugServer::EnumerateStackEntry(int stackEntry) { if (m_acceptedSocket != NULL) { try { if (WriteByte(m_acceptedSocket, CMD_ENUMERATE_STACK_ENTRY) && WriteInt(m_acceptedSocket, stackEntry)) { return true; } } catch(wxLuaSocketException & /*e*/) { } } return false; } bool wxLuaDebugServer::EnumerateTable(int tableRef, int nIndex, int nItemNode) { if (m_acceptedSocket != NULL) { try { if (WriteByte(m_acceptedSocket, CMD_ENUMERATE_TABLE_REF) && WriteInt(m_acceptedSocket, tableRef) && WriteInt(m_acceptedSocket, nIndex) && WriteInt(m_acceptedSocket, nItemNode)) { return true; } } catch(wxLuaSocketException & /*e*/) { } } return false; } bool wxLuaDebugServer::CleanupDebugReferences() { if (m_acceptedSocket != NULL) { try { if (WriteByte(m_acceptedSocket, CMD_CLEAR_DEBUG_REFERENCES)) { return true; } } catch(wxLuaSocketException & /*e*/) { } } return false; } void *wxLuaThread::Entry() { m_pServer->ThreadFunction(); return 0; } void wxLuaDebugServer::ThreadFunction() { try { m_acceptedSocket = new wxLuaSocket(m_serverSocket->Accept()); if (m_acceptedSocket != NULL) { wxLuaSocket *serverSocket = m_serverSocket; m_serverSocket = NULL; delete serverSocket; #if wxCHECK_VERSION(2, 3, 3) wxMilliSleep(500); // why ?? #endif // if not shutdown requested if (!m_fShutdown) { try { // Notify that a client has connected and we are ready to debug wxLuaDebugEvent debugEvent(wxEVT_DEBUG_CLIENT_CONNECTED); wxLuaHandler::GetHandler().AddPendingEvent(debugEvent); // Enter the debug loop while (!m_fShutdown) { int debugEvent = ReadByte(m_acceptedSocket); switch((debugEvents) debugEvent) { case EVENT_DEBUG_BREAK: { wxString fileName = ReadString(m_acceptedSocket); int lineNumber = ReadInt(m_acceptedSocket); wxLuaDebugEvent debugEvent(wxEVT_DEBUG_BREAK, lineNumber, fileName); wxLuaHandler::GetHandler().AddPendingEvent(debugEvent); } break; case EVENT_DEBUG_PRINT: { wxString strMessage = ReadString(m_acceptedSocket); wxLuaDebugEvent debugEvent(wxEVT_DEBUG_PRINT); debugEvent.SetMessage(strMessage); wxLuaHandler::GetHandler().AddPendingEvent(debugEvent); } break; case EVENT_DEBUG_ERROR: { wxString strMessage = ReadString(m_acceptedSocket); wxLuaDebugEvent debugEvent(wxEVT_DEBUG_ERROR); debugEvent.SetMessage(strMessage); wxLuaHandler::GetHandler().AddPendingEvent(debugEvent); } break; case EVENT_DEBUG_EXIT: { wxLuaDebugEvent debugEvent(wxEVT_DEBUG_EXIT); wxLuaHandler::GetHandler().AddPendingEvent(debugEvent); m_fShutdown = true; } break; case EVENT_DEBUG_STACK_ENUM: { wxLuaDebugEvent debugEvent(wxEVT_DEBUG_STACK_ENUM); wxLuaDebugData *pDebugData = ReadDebugData(m_acceptedSocket); if (pDebugData != NULL) debugEvent.SetDebugData(-1, pDebugData); wxLuaHandler::GetHandler().AddPendingEvent(debugEvent); } break; case EVENT_DEBUG_STACK_ENTRY_ENUM: { wxLuaDebugEvent debugEvent(wxEVT_DEBUG_STACK_ENTRY_ENUM); int stackRef = ReadInt(m_acceptedSocket); wxLuaDebugData *pDebugData = ReadDebugData(m_acceptedSocket); if (pDebugData != NULL) debugEvent.SetDebugData(stackRef, pDebugData); wxLuaHandler::GetHandler().AddPendingEvent(debugEvent); } break; case EVENT_DEBUG_TABLE_ENUM: { wxLuaDebugEvent debugEvent(wxEVT_DEBUG_TABLE_ENUM); int itemNode = ReadInt(m_acceptedSocket); wxLuaDebugData *pDebugData = ReadDebugData(m_acceptedSocket); if (pDebugData != NULL) debugEvent.SetDebugData(itemNode, pDebugData); wxLuaHandler::GetHandler().AddPendingEvent(debugEvent); } break; case EVENT_DEBUG_EVALUATE_EXPR: { wxLuaDebugEvent debugEvent(wxEVT_DEBUG_EVALUATE_EXPR); int exprRef = ReadInt(m_acceptedSocket); wxString strResult = ReadString(m_acceptedSocket); debugEvent.SetMessage(strResult); debugEvent.SetDebugData(exprRef); wxLuaHandler::GetHandler().AddPendingEvent(debugEvent); } break; case EVENT_DEBUG_BREAKPOINT_ADDED : break; // FIXME - unused case EVENT_DEBUG_BREAKPOINT_REMOVED : break; // FIXME - unused } } } catch(wxLuaSocketException & /*e*/) { } } // delete the accepted socket if (m_acceptedSocket != NULL) { wxLuaSocket *acceptedSocket = m_acceptedSocket; m_acceptedSocket = NULL; delete acceptedSocket; } } } catch(wxLuaSocketException & /*e*/) { } } void wxLuaThread::OnExit() { } #if wxCHECK_VERSION(2, 3, 0) bool wxLuaDebugServer::DisplayStackDialog(wxWindow *pParent) { if (m_pStackFrameDialog == NULL) { m_pStackFrameDialog = new wxLuaStackFrame(pParent, _("Stack frame"), wxDefaultPosition, wxSize(500, 250), this); m_pStackFrameDialog->ShowModal(); m_pStackFrameDialog->Destroy(); m_pStackFrameDialog = NULL; return true; } return false; } #endif bool wxLuaDebugServer::EvaluateExpr(int exprRef, const wxString &strExpression) { if (m_acceptedSocket != NULL) { try { if (WriteByte(m_acceptedSocket, CMD_EVALUATE_EXPR) && WriteInt(m_acceptedSocket, exprRef) && WriteString(m_acceptedSocket, strExpression)) { return true; } } catch(wxLuaSocketException & /*e*/) { } } return false; } --- NEW FILE: wxlsock.cpp --- ///////////////////////////////////////////////////////////////////////////// // Name: wxLuaSocket.cpp // Purpose: Socket class for wxLua. // Author: J. Winwood // Created: February 2002 // Copyright: (c) 2002 Lomtick Software. All rights reserved. // Licence: wxWidgets licence ///////////////////////////////////////////////////////////////////////////// #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) #pragma implementation "wxlsock.h" #endif #include "wx/wxprec.h" #ifdef __BORLANDC__ #pragma hdrstop #endif #ifndef WX_PRECOMP #include "wx/wx.h" #endif #include "wxluasocket/include/wxlsock.h" #ifdef _MSC_VER #pragma warning(push, 4) #endif extern const wxCharBuffer wx2lua(const wxString& AppString); extern wxString lua2wx(const char * wx2lua); // ---------------------------------------------------------------------------- // wxLuaSocket // ---------------------------------------------------------------------------- // Default socket constructor. wxLuaSocket::wxLuaSocket() : m_sockstate(SOCKET_CLOSED) { } // Socket constructor from an accepted socket wxLuaSocket::wxLuaSocket(const wxLuaSocket::wxLuaAcceptedSocket &acceptedSocket) : m_sock(acceptedSocket.m_sock), m_sockaddress(acceptedSocket.m_addr), m_sockstate(SOCKET_ACCEPTED) { } // Accepted socket constructor wxLuaSocket::wxLuaAcceptedSocket::wxLuaAcceptedSocket(socket_type sock, sockaddr_in addr) : m_sock(sock), m_addr(addr) { } // socket destructor: close the socket if not already closed. wxLuaSocket::~wxLuaSocket() { if (m_sockstate != SOCKET_CLOSED) { #ifdef WIN32 ::closesocket(m_sock); #else ::close(m_sock); #endif } } // Create a listening socket, using the specified port number void wxLuaSocket::Listen(u_short portNumber, int backLog) { if (m_sockstate != SOCKET_CLOSED) throw wxLuaSocketException(wxLuaSocketException::SOCKET_NOT_CLOSED); m_sock = ::socket(AF_INET, SOCK_STREAM, 0); if (m_sock == INVALID_SOCKET) throw wxLuaSocketException(wxLuaSocketException::SOCKET_INVALID_SOCKET); sockaddr_in localAddr = { 0 }; localAddr.sin_family = AF_INET; localAddr.sin_port = htons(portNumber); localAddr.sin_addr.s_addr = htonl(INADDR_ANY); if (::bind(m_sock, (sockaddr *) &localAddr, sizeof(localAddr)) == SOCKET_ERROR) throw wxLuaSocketException(wxLuaSocketException::SOCKET_BIND_FAILED); if (::listen(m_sock, backLog) == SOCKET_ERROR) throw wxLuaSocketException(wxLuaSocketException::SOCKET_LISTEN_FAILED); memset(&m_sockaddress, 0, sizeof(m_sockaddress)); m_sockstate = SOCKET_LISTENING; } // Accept a connection, returning an accepted socket. wxLuaSocket::wxLuaAcceptedSocket wxLuaSocket::Accept() { if (m_sockstate != SOCKET_LISTENING) throw wxLuaSocketException(wxLuaSocketException::SOCKET_NOT_LISTENING); sockaddr_in fromAddr = { 0 }; socklen_t length = sizeof(fromAddr); socket_type acceptedSocket = ::accept(m_sock, (sockaddr *)&fromAddr, &length); if (acceptedSocket == INVALID_SOCKET) throw wxLuaSocketException(wxLuaSocketException::SOCKET_ACCEPT_FAILED); return wxLuaAcceptedSocket(acceptedSocket, fromAddr); } // Connect to a given host and port number void wxLuaSocket::Connect(const wxString &addr, u_short portNumber) { hostent *pHost = NULL; if (m_sockstate != SOCKET_CLOSED) throw wxLuaSocketException(wxLuaSocketException::SOCKET_NOT_CLOSED); m_sock = ::socket(AF_INET, SOCK_STREAM, 0); if (m_sock == INVALID_SOCKET) throw wxLuaSocketException(wxLuaSocketException::SOCKET_INVALID_SOCKET); unsigned long address = ::inet_addr(wx2lua(addr)); if (address != INADDR_NONE) pHost = ::gethostbyaddr((const char*) &address, 4, AF_INET); else pHost = ::gethostbyname(wx2lua(addr)); if (pHost == NULL) throw wxLuaSocketException(wxLuaSocketException::SOCKET_ADDRESS_NOT_RESOLVED); if (pHost->h_addrtype != AF_INET) throw wxLuaSocketException(wxLuaSocketException::SOCKET_INCOMPATIBLE_TYPE); memset(&m_sockaddress, 0, sizeof(m_sockaddress)); memcpy(&(m_sockaddress.sin_addr), pHost->h_addr_list[0], pHost->h_length); m_sockaddress.sin_family = AF_INET; m_sockaddress.sin_port = htons(portNumber); if (::connect(m_sock, (sockaddr *) &m_sockaddress, sizeof(m_sockaddress)) == SOCKET_ERROR) throw wxLuaSocketException(wxLuaSocketException::SOCKET_CONNECT_FAILED); m_sockstate = SOCKET_CONNECTED; } // Get the address of an open socket wxString wxLuaSocket::GetAddress() const { if (m_sockstate != SOCKET_CONNECTED && m_sockstate != SOCKET_ACCEPTED) throw wxLuaSocketException(wxLuaSocketException::SOCKET_NOT_CONNECTED); return lua2wx(inet_ntoa(m_sockaddress.sin_addr)); } // Get the port number of an open socket int wxLuaSocket::GetPort() const { if (m_sockstate != SOCKET_CONNECTED && m_sockstate != SOCKET_ACCEPTED) throw wxLuaSocketException(wxLuaSocketException::SOCKET_NOT_CONNECTED); return ntohs(m_sockaddress.sin_port); } // Write data to an open socket, repeat until all data has been sent. void wxLuaSocket::Write(const char *buffer, int length) { if (m_sockstate != SOCKET_CONNECTED && m_sockstate != SOCKET_ACCEPTED) throw wxLuaSocketException(wxLuaSocketException::SOCKET_NOT_CONNECTED); while (length > 0) { int numWritten = ::send(m_sock, buffer, length, 0); if (numWritten == SOCKET_ERROR) throw wxLuaSocketException(wxLuaSocketException::SOCKET_SEND_FAILED); length -= numWritten; buffer += numWritten; } } // Read data from an open socket, repeat reading until all data has been read int wxLuaSocket::Read(char *buffer, int length) { if (m_sockstate != SOCKET_CONNECTED && m_sockstate != SOCKET_ACCEPTED) throw wxLuaSocketException(wxLuaSocketException::SOCKET_NOT_CONNECTED); while (length > 0) { int numRead = ::recv(m_sock, buffer, length, 0); if (numRead == 0) return length; if (numRead == SOCKET_ERROR) throw wxLuaSocketException(wxLuaSocketException::SOCKET_RECEIVE_FAILED); length -= numRead; buffer += numRead; } return 0; } // Shutdown a socket in an orderly fashion void wxLuaSocket::Shutdown(int how) { if (m_sockstate != SOCKET_CLOSED) { ::shutdown(m_sock, how); } } // Close an open socket void wxLuaSocket::Close() { if (m_sockstate != SOCKET_CLOSED) { #ifdef WIN32 if (::closesocket(m_sock) == SOCKET_ERROR) throw wxLuaSocketException(wxLuaSocketException::SOCKET_CLOSE_FAILED); #else if (::close(m_sock)) throw wxLuaSocketException(wxLuaSocketException::SOCKET_CLOSE_FAILED); #endif m_sockstate = SOCKET_CLOSED; } } Index: Makefile =================================================================== RCS file: /cvsroot/wxlua/wxLua/modules/wxluasocket/src/Makefile,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** Makefile 25 Nov 2005 19:03:29 -0000 1.3 --- Makefile 25 Nov 2005 19:22:58 -0000 1.4 *************** *** 51,65 **** HEADERS = \ ! ../include/debugio.h \ ! ../include/dserver.h \ ! ../include/dtarget.h \ ! ../include/socket.h \ ../include/wxlhandl.h SOURCES = \ ! debugio.cpp \ ! dserver.cpp \ ! dtarget.cpp \ ! socket.cpp \ wxlhandl.cpp --- 51,65 ---- HEADERS = \ ! ../include/wxldbgio.h \ ! ../include/wxldserv.h \ ! ../include/wxldtarg.h \ ! ../include/wxlsock.h \ ../include/wxlhandl.h SOURCES = \ ! wxldbgio.cpp \ ! wxldserv.cpp \ ! wxldtarg.cpp \ ! wxlsock.cpp \ wxlhandl.cpp --- dtarget.cpp DELETED --- --- NEW FILE: wxldtarg.cpp --- ///////////////////////////////////////////////////////////////////////////// // Purpose: Implements the client end of wxLua debugging session // Author: J. Winwood // Created: May 2002 // RCS-ID: $Id: wxldtarg.cpp,v 1.1 2005/11/25 19:22:58 jrl1 Exp $ // Copyright: (c) 2002 Lomtick Software. All rights reserved. // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) #pragma implementation "wxldtarg.h" #endif #include "wx/wxprec.h" #ifdef __BORLANDC__ #pragma hdrstop #endif [...1100 lines suppressed...] lua_pushvalue(L, idx); /* value to print */ lua_call(L, 1, 1); wxString s = lua2wx(lua_tostring(L, -1)); /* get result */ if (s.IsEmpty()) return luaL_error(L, "`tostring' must return a string to `print'"); if (idx > 1) stream.Append(wxT("\t")); stream.Append(s); lua_pop(L, 1); /* pop result */ } lua_getglobal(L, debugName); LuaDebugTarget *pTarget = (LuaDebugTarget *) lua_touserdata(L, -1); lua_pop(L, 1); if (pTarget != NULL) pTarget->NotifyPrint(stream); return 0; } --- dserver.cpp DELETED --- |