00001
00002
00003
00004
00005
00006
00007 #include <wx/sckstrm.h>
00008
00009 #include "cmdprot.h"
00010
00011 #define SOCKET_ID 1
00012
00013 class wxCmdlineProtocolSocketEventHandler : public wxEvtHandler
00014 {
00015 public:
00016 wxCmdlineProtocolSocketEventHandler(wxCmdlineProtocol& callback) : m_callback(callback)
00017 {
00018 }
00019
00020 void OnSocketEvent(wxSocketEvent& event)
00021 {
00022 m_callback.OnSocketEvent(event);
00023 }
00024
00025 private:
00026 wxCmdlineProtocol& m_callback;
00027
00028 DECLARE_EVENT_TABLE()
00029 };
00030
00031 BEGIN_EVENT_TABLE(wxCmdlineProtocolSocketEventHandler, wxEvtHandler)
00032 EVT_SOCKET(SOCKET_ID, wxCmdlineProtocolSocketEventHandler::OnSocketEvent)
00033 END_EVENT_TABLE()
00034
00035
00036 wxCmdlineProtocol::wxCmdlineProtocol()
00037 {
00038 m_pCmdlineProtocolSocketEventHandler = new wxCmdlineProtocolSocketEventHandler(*this);
00039 }
00040
00041 wxCmdlineProtocol::~wxCmdlineProtocol()
00042 {
00043 delete m_pCmdlineProtocolSocketEventHandler;
00044 }
00045
00046 void wxCmdlineProtocol::SetHost(const wxString& host, const wxString& user, const wxString& password)
00047 {
00048 m_host = host;
00049 m_user = user;
00050 m_password = password;
00051 }
00052
00053 void wxCmdlineProtocol::Connect()
00054 {
00055 if (IsConnected()) Close();
00056 SetTimeout(60);
00057 SetEventHandler(*m_pCmdlineProtocolSocketEventHandler, SOCKET_ID);
00058 SetNotify(wxSOCKET_CONNECTION_FLAG | wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG);
00059 Notify(TRUE);
00060
00061
00062 wxIPV4address addr;
00063 addr.Hostname(m_host);
00064 addr.Service(25);
00065 wxSocketClient::Connect(addr, FALSE);
00066 }
00067
00068 void wxCmdlineProtocol::OnInput(wxSocketEvent& event)
00069 {
00070
00071
00072
00073 const int bufsize = 256;
00074 char buf[bufsize];
00075 Read(buf, bufsize);
00076 m_inputLine += wxString(buf, LastCount());
00077
00078
00079 while (true) {
00080 size_t pos = 0;
00081 while (pos < m_inputLine.Length() - 1) {
00082 if (m_inputLine[pos] == 13) {
00083 if (m_inputLine[pos + 1] == 10)
00084 {
00085
00086 EvaluateLine(m_inputLine.Mid(0, pos));
00087
00088
00089 m_inputLine = m_inputLine.Mid(pos + 2);
00090 return;
00091 }
00092 break;
00093 }
00094 pos++;
00095 }
00096 }
00097 }
00098
00099 void wxCmdlineProtocol::OnSocketEvent(wxSocketEvent& event)
00100 {
00101 wxString s = _("OnSocketEvent: ");
00102
00103 switch(event.GetSocketEvent())
00104 {
00105 case wxSOCKET_INPUT:
00106 OnInput(event);
00107 break;
00108 case wxSOCKET_LOST:
00109
00110 break;
00111 case wxSOCKET_CONNECTION:
00112 OnConnect(event);
00113 break;
00114 }
00115 }
00116
00117 void wxCmdlineProtocol::Write(const wxString& msg)
00118 {
00119 wxSocketClient::Write(msg, msg.Length());
00120 }
00121