Permalink
Browse files

smtp, bugfix: refactor source code of async connect, fix ssl connecti…

…on errors.
  • Loading branch information...
xicilion committed Apr 14, 2018
1 parent aa1cf87 commit 1f871813544f267d2d98f55e8046253831f6756c
Showing with 72 additions and 37 deletions.
  1. +72 −37 fibjs/src/net/Smtp.cpp
View
@@ -29,7 +29,6 @@ class asyncSmtp : public AsyncState {
, m_retVal(retVal)
{
m_stmBuffered = pThis->m_stmBuffered;
set(ok);
}
asyncSmtp(Smtp* pThis, AsyncEvent* ac)
@@ -38,7 +37,6 @@ class asyncSmtp : public AsyncState {
, m_retVal(m_strLine)
{
m_stmBuffered = pThis->m_stmBuffered;
set(ok);
}
static int32_t ok(AsyncState* pState, int32_t n)
@@ -73,70 +71,107 @@ class asyncSmtp : public AsyncState {
return pThis->m_stmBuffered->readLine(SMTP_MAX_LINE, strLine, pThis);
}
static int32_t connected(AsyncState* pState, int32_t n)
virtual int32_t recv_ok()
{
asyncSmtp* pThis = (asyncSmtp*)pState;
pThis->m_pThis->m_stmBuffered = new BufferedStream(pThis->m_pThis->m_conn);
pThis->m_pThis->m_stmBuffered->set_EOL("\r\n");
pThis->m_stmBuffered = pThis->m_pThis->m_stmBuffered;
return ok(pState, n);
return done();
}
int32_t connect(exlib::string url)
protected:
obj_ptr<Smtp> m_pThis;
exlib::string& m_retVal;
obj_ptr<BufferedStream_base> m_stmBuffered;
exlib::string m_strLine;
};
class asyncCommand : public asyncSmtp {
public:
asyncCommand(Smtp* pThis, exlib::string cmd, exlib::string arg,
exlib::string& retVal, AsyncEvent* ac)
: asyncSmtp(pThis, retVal, ac)
{
set(connected);
int32_t r = net_base::connect(url, m_pThis->m_timeout, m_pThis->m_conn, this);
exlib::string s(cmd);
if (r == CALL_E_PENDDING)
return r;
return post(r);
}
s.append(" ", 1);
s.append(arg);
s.append("\r\n", 2);
int32_t send(Buffer_base* data)
{
int32_t r = m_stmBuffered->write(data, this);
m_buf = new Buffer(s);
if (r == CALL_E_PENDDING)
return r;
return post(r);
set(command);
}
int32_t command(exlib::string cmd, exlib::string arg)
asyncCommand(Smtp* pThis, exlib::string cmd, exlib::string arg, AsyncEvent* ac)
: asyncSmtp(pThis, ac)
{
exlib::string s(cmd);
s.append(" ", 1);
s.append(arg);
s.append("\r\n", 2);
obj_ptr<Buffer> buf = new Buffer(s);
return send(buf);
m_buf = new Buffer(s);
set(command);
}
virtual int32_t recv_ok()
public:
static int32_t command(AsyncState* pState, int32_t n)
{
return done();
asyncCommand* pThis = (asyncCommand*)pState;
pThis->set(ok);
return pThis->m_stmBuffered->write(pThis->m_buf, pThis);
}
protected:
obj_ptr<Smtp> m_pThis;
exlib::string& m_retVal;
obj_ptr<BufferedStream_base> m_stmBuffered;
exlib::string m_strLine;
private:
obj_ptr<Buffer> m_buf;
};
result_t Smtp::connect(exlib::string url, AsyncEvent* ac)
{
class asyncConnect : public asyncSmtp {
public:
asyncConnect(Smtp* pThis, exlib::string url, AsyncEvent* ac)
: asyncSmtp(pThis, ac)
, m_url(url)
{
set(connect);
}
public:
static int32_t connect(AsyncState* pState, int32_t n)
{
asyncConnect* pThis = (asyncConnect*)pState;
pThis->set(connected);
return net_base::connect(pThis->m_url, pThis->m_pThis->m_timeout,
pThis->m_pThis->m_conn, pThis);
}
static int32_t connected(AsyncState* pState, int32_t n)
{
asyncConnect* pThis = (asyncConnect*)pState;
pThis->m_pThis->m_stmBuffered = new BufferedStream(pThis->m_pThis->m_conn);
pThis->m_pThis->m_stmBuffered->set_EOL("\r\n");
pThis->m_stmBuffered = pThis->m_pThis->m_stmBuffered;
pThis->set(ok);
return 0;
}
private:
exlib::string m_url;
};
if (m_conn)
return CHECK_ERROR(CALL_E_INVALID_CALL);
if (ac->isSync())
return CHECK_ERROR(CALL_E_NOSYNC);
return (new asyncSmtp(this, ac))->connect(url);
return (new asyncConnect(this, url, ac))->post(0);
}
result_t Smtp::command(exlib::string cmd, exlib::string arg, exlib::string& retVal,
@@ -148,7 +183,7 @@ result_t Smtp::command(exlib::string cmd, exlib::string arg, exlib::string& retV
if (ac->isSync())
return CHECK_ERROR(CALL_E_NOSYNC);
return (new asyncSmtp(this, retVal, ac))->command(cmd, arg);
return (new asyncCommand(this, cmd, arg, retVal, ac))->post(0);
}
result_t Smtp::command(exlib::string cmd, exlib::string arg, AsyncEvent* ac)
@@ -159,7 +194,7 @@ result_t Smtp::command(exlib::string cmd, exlib::string arg, AsyncEvent* ac)
if (ac->isSync())
return CHECK_ERROR(CALL_E_NOSYNC);
return (new asyncSmtp(this, ac))->command(cmd, arg);
return (new asyncCommand(this, cmd, arg, ac))->post(0);
}
result_t Smtp::hello(exlib::string hostname, AsyncEvent* ac)

0 comments on commit 1f87181

Please sign in to comment.