/* * Purpose: private wxWindows mail transport implementation * Author: Frank Buß * Created: 2002 */ #include #include "cmdprot.h" #define SOCKET_ID 1 class wxCmdlineProtocolSocketEventHandler : public wxEvtHandler { public: wxCmdlineProtocolSocketEventHandler(wxCmdlineProtocol& callback) : m_callback(callback) { } void OnSocketEvent(wxSocketEvent& event) { m_callback.OnSocketEvent(event); } private: wxCmdlineProtocol& m_callback; DECLARE_EVENT_TABLE() }; BEGIN_EVENT_TABLE(wxCmdlineProtocolSocketEventHandler, wxEvtHandler) EVT_SOCKET(SOCKET_ID, wxCmdlineProtocolSocketEventHandler::OnSocketEvent) END_EVENT_TABLE() wxCmdlineProtocol::wxCmdlineProtocol() { m_pCmdlineProtocolSocketEventHandler = new wxCmdlineProtocolSocketEventHandler(*this); } wxCmdlineProtocol::~wxCmdlineProtocol() { delete m_pCmdlineProtocolSocketEventHandler; } void wxCmdlineProtocol::SetHost(const wxString& host, int service, const wxString& user, const wxString& password) { m_host = host; m_user = user; m_service = service; m_password = password; } void wxCmdlineProtocol::Connect() { if (IsConnected()) Close(); SetTimeout(60); SetEventHandler(*m_pCmdlineProtocolSocketEventHandler, SOCKET_ID); SetNotify(wxSOCKET_CONNECTION_FLAG | wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG); Notify(TRUE); // connect wxIPV4address addr; addr.Hostname(m_host); addr.Service(m_service); // TODO: perhaps the port should not be hardcoded? wxSocketClient::Connect(addr, FALSE); } void wxCmdlineProtocol::OnInput(wxSocketEvent& event) { // TODO: implementing response timeout somewhere // get data const int bufsize = 256; #if wxUSE_UNICODE wchar_t buf[bufsize]; #else char buf[bufsize]; #endif Read(buf, bufsize); m_inputLine += wxString(buf, LastCount()); // search for a newline while (true) { size_t pos = 0; while (pos < m_inputLine.Length() - 1) { if (m_inputLine[pos] == 13) { if (m_inputLine[pos + 1] == 10) { // line found, evaluate EvaluateLine(m_inputLine.Mid(0, pos)); // adjust buffer m_inputLine = m_inputLine.Mid(pos + 2); return; } break; } pos++; } } } void wxCmdlineProtocol::OnSocketEvent(wxSocketEvent& event) { wxString s = _("OnSocketEvent: "); switch(event.GetSocketEvent()) { case wxSOCKET_INPUT: OnInput(event); break; case wxSOCKET_LOST: // TODO: error handling break; case wxSOCKET_CONNECTION: OnConnect(event); break; } } void wxCmdlineProtocol::Write(const wxString& msg) { wxSocketClient::Write(msg, msg.Length()); }