hxsymbiantcpsock.cpp
Upload User: dangjiwu
Upload Date: 2013-07-19
Package Size: 42019k
Code Size: 21k
Category:

Symbian

Development Platform:

Visual C++

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Source last modified: $Id: hxsymbiantcpsock.cpp,v 1.6.2.3 2004/07/09 02:08:48 hubbe Exp $
  3.  * 
  4.  * Portions Copyright (c) 1995-2004 RealNetworks, Inc. All Rights Reserved.
  5.  * 
  6.  * The contents of this file, and the files included with this file,
  7.  * are subject to the current version of the RealNetworks Public
  8.  * Source License (the "RPSL") available at
  9.  * http://www.helixcommunity.org/content/rpsl unless you have licensed
  10.  * the file under the current version of the RealNetworks Community
  11.  * Source License (the "RCSL") available at
  12.  * http://www.helixcommunity.org/content/rcsl, in which case the RCSL
  13.  * will apply. You may also obtain the license terms directly from
  14.  * RealNetworks.  You may not use this file except in compliance with
  15.  * the RPSL or, if you have a valid RCSL with RealNetworks applicable
  16.  * to this file, the RCSL.  Please see the applicable RPSL or RCSL for
  17.  * the rights, obligations and limitations governing use of the
  18.  * contents of the file.
  19.  * 
  20.  * Alternatively, the contents of this file may be used under the
  21.  * terms of the GNU General Public License Version 2 or later (the
  22.  * "GPL") in which case the provisions of the GPL are applicable
  23.  * instead of those above. If you wish to allow use of your version of
  24.  * this file only under the terms of the GPL, and not to allow others
  25.  * to use your version of this file under the terms of either the RPSL
  26.  * or RCSL, indicate your decision by deleting the provisions above
  27.  * and replace them with the notice and other provisions required by
  28.  * the GPL. If you do not delete the provisions above, a recipient may
  29.  * use your version of this file under the terms of any one of the
  30.  * RPSL, the RCSL or the GPL.
  31.  * 
  32.  * This file is part of the Helix DNA Technology. RealNetworks is the
  33.  * developer of the Original Code and owns the copyrights in the
  34.  * portions it created.
  35.  * 
  36.  * This file, and the files included with this file, is distributed
  37.  * and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY
  38.  * KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS
  39.  * ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES
  40.  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET
  41.  * ENJOYMENT OR NON-INFRINGEMENT.
  42.  * 
  43.  * Technology Compatibility Kit Test Suite(s) Location:
  44.  *    http://www.helixcommunity.org/content/tck
  45.  * 
  46.  * Contributor(s):
  47.  * 
  48.  * ***** END LICENSE BLOCK ***** */
  49. #include "platform/symbian/hxsymbiantcpsock.h"
  50. #include "hxsymbiansockhlp.h"
  51. #include "debug.h"
  52. #include "hxassert.h"
  53. #include "ihxpckts.h"
  54. #include "smartptr.h"
  55. #include <in_sock.h>
  56. #define D_TCPSOCKET 0x10000000
  57. class HXSymbianTCPResolvResp : public IHXResolverResponse
  58. {
  59. public:
  60.     HXSymbianTCPResolvResp(HXSymbianTCPSocket* pParent);
  61.     ~HXSymbianTCPResolvResp();
  62.     /*
  63.      *  IUnknown methods
  64.      */
  65.     STDMETHOD(QueryInterface) (THIS_
  66. REFIID riid,
  67. void** ppvObj);
  68.     STDMETHOD_(ULONG32,AddRef) (THIS);
  69.     STDMETHOD_(ULONG32,Release) (THIS);
  70.     /*
  71.      * IHXResolverResponse methods
  72.      */
  73.     STDMETHOD(GetHostByNameDone) (THIS_
  74. HX_RESULT status,
  75. ULONG32 ulAddr);
  76. private:
  77.     ULONG32 m_lRefCount;
  78.     HXSymbianTCPSocket* m_pParent;
  79. };
  80. HXSymbianTCPResolvResp::HXSymbianTCPResolvResp(HXSymbianTCPSocket* pParent) :
  81.     m_lRefCount(0),
  82.     m_pParent(pParent)
  83. {}
  84. HXSymbianTCPResolvResp::~HXSymbianTCPResolvResp()
  85. {}
  86.     /*
  87.      *  IUnknown methods
  88.      */
  89. STDMETHODIMP HXSymbianTCPResolvResp::QueryInterface(THIS_
  90.     REFIID riid,
  91.     void** ppvObj)
  92. {
  93.     QInterfaceList qiList[] =
  94. {
  95. { GET_IIDHANDLE(IID_IUnknown), (IUnknown*)(IHXResolverResponse*)this },
  96. { GET_IIDHANDLE(IID_IHXResolverResponse), (IHXResolverResponse*) this },
  97. };
  98.     return QIFind(qiList, QILISTSIZE(qiList), riid, ppvObj);
  99. }
  100. STDMETHODIMP_(ULONG32) HXSymbianTCPResolvResp::AddRef(THIS)
  101. {
  102.     return InterlockedIncrement(&m_lRefCount);
  103. }
  104. STDMETHODIMP_(ULONG32) HXSymbianTCPResolvResp::Release(THIS)
  105. {
  106.     if (InterlockedDecrement(&m_lRefCount) > 0)
  107.     {
  108.         return m_lRefCount;
  109.     }
  110.     delete this;
  111.     return 0;
  112. }
  113.     /*
  114.      * IHXResolverResponse methods
  115.      */
  116. STDMETHODIMP HXSymbianTCPResolvResp::GetHostByNameDone(THIS_
  117.        HX_RESULT status,
  118.        ULONG32 ulAddr)
  119. {
  120.     return m_pParent->GetHostByNameDone(status, ulAddr);
  121. }
  122. class HXSymbianTCPConnector : public CActive
  123. {
  124. public:
  125.     HXSymbianTCPConnector(HXSymbianTCPSocket* pParent);
  126.     ~HXSymbianTCPConnector();
  127.     void Connect(RSocket& socket, ULONG32 ulAddr, UINT16 nPort);
  128. private:
  129.     void RunL();
  130.     void DoCancel();
  131.     HXSymbianTCPSocket* m_pParent;
  132.     TInetAddr m_addr;
  133. };
  134. HXSymbianTCPConnector::HXSymbianTCPConnector(HXSymbianTCPSocket* pParent) :
  135.     CActive(EPriorityStandard),
  136.     m_pParent(pParent)
  137. {
  138.     CActiveScheduler::Add(this);
  139. }
  140. HXSymbianTCPConnector::~HXSymbianTCPConnector()
  141. {
  142.     if (IsActive())
  143. Cancel();
  144.     m_pParent = 0;
  145. }
  146. void HXSymbianTCPConnector::Connect(RSocket& socket,
  147.     ULONG32 ulAddr, UINT16 nPort)
  148. {
  149.     m_addr.SetAddress(ulAddr);
  150.     m_addr.SetPort(nPort);
  151.     iStatus = KRequestPending;
  152.     socket.Connect(m_addr, iStatus);
  153.     SetActive();
  154. }
  155. void HXSymbianTCPConnector::RunL()
  156. {
  157.     m_pParent->OnConnect((iStatus == KErrNone) ? HXR_OK : HXR_NET_CONNECT);
  158. }
  159. void HXSymbianTCPConnector::DoCancel()
  160. {}
  161. class HXSymbianTCPWriter : public CActive
  162. {
  163. public:
  164.     HXSymbianTCPWriter(HXSymbianTCPSocket* pParent);
  165.     ~HXSymbianTCPWriter();
  166.     void Write(RSocket& socket, IHXBuffer* pBuffer);
  167. private:
  168.     void RunL();
  169.     void DoCancel();
  170.     HXSymbianTCPSocket* m_pParent;
  171.     IHXBuffer* m_pBuffer;
  172.     TPtrC8 m_bufDes;
  173. };
  174. HXSymbianTCPWriter::HXSymbianTCPWriter(HXSymbianTCPSocket* pParent) :
  175.     CActive(EPriorityStandard),
  176.     m_pParent(pParent),
  177.     m_pBuffer(0)
  178. {
  179.     CActiveScheduler::Add(this);
  180. }
  181. HXSymbianTCPWriter::~HXSymbianTCPWriter()
  182. {
  183.     if (IsActive())
  184. Cancel();
  185.     HX_RELEASE(m_pBuffer);
  186.     m_pParent = 0;
  187. }
  188. void HXSymbianTCPWriter::Write(RSocket& socket, IHXBuffer* pBuffer)
  189. {
  190.     HX_RELEASE(m_pBuffer);
  191.     m_pBuffer = pBuffer;
  192.     if (m_pBuffer)
  193.     {
  194. m_pBuffer->AddRef();
  195. m_bufDes.Set(m_pBuffer->GetBuffer(), m_pBuffer->GetSize());
  196. iStatus = KRequestPending;
  197. socket.Write(m_bufDes, iStatus);
  198. SetActive();
  199.     }
  200. }
  201. void HXSymbianTCPWriter::RunL()
  202. {
  203.     HX_RESULT res = HXR_FAILED;
  204.     if (iStatus == KErrNone)
  205.     {
  206. res = HXR_OK;
  207.     }
  208.     else if(iStatus == KErrEof)
  209.     {
  210. res = HXR_STREAM_DONE;
  211.     }
  212.     else if(iStatus == KErrNoMemory)
  213.     {
  214. res = HXR_OUTOFMEMORY;
  215.     }
  216.     HX_RELEASE(m_pBuffer);
  217.     m_pParent->OnWriteDone(res);
  218. }
  219. void HXSymbianTCPWriter::DoCancel()
  220. {}
  221. class HXSymbianTCPReader : public CActive
  222. {
  223. public:
  224.     HXSymbianTCPReader(HXSymbianTCPSocket* pParent,
  225.                        IHXCommonClassFactory* pCCF);
  226.     ~HXSymbianTCPReader();
  227.     HX_RESULT Read(RSocket& socket, UINT16 uSize);
  228. private:
  229.     void RunL();
  230.     void DoCancel();
  231.     HXSymbianTCPSocket* m_pParent;
  232.     IHXBuffer* m_pBuffer;
  233.     TPtr8 m_bufDes;
  234.     TSockXfrLength m_amountRead;
  235.     IHXCommonClassFactory* m_pCCF;
  236. };
  237. HXSymbianTCPReader::HXSymbianTCPReader(HXSymbianTCPSocket* pParent,
  238.                                        IHXCommonClassFactory* pCCF) :
  239.     CActive(EPriorityStandard),
  240.     m_pParent(pParent),
  241.     m_pBuffer(0),
  242.     m_bufDes(0, 0),
  243.     m_pCCF(pCCF)
  244. {
  245.     CActiveScheduler::Add(this);
  246.     if (m_pCCF)
  247.     {
  248.         m_pCCF->AddRef();
  249.     }
  250. }
  251. HXSymbianTCPReader::~HXSymbianTCPReader()
  252. {
  253.     if (IsActive())
  254. Cancel();
  255.     HX_RELEASE(m_pBuffer);
  256.     HX_RELEASE(m_pCCF);
  257.     m_pParent = 0;
  258. }
  259. HX_RESULT HXSymbianTCPReader::Read(RSocket& socket, UINT16 uSize)
  260. {
  261.     HX_RESULT res = HXR_FAILED;
  262.     if (m_pParent)
  263.     {
  264.         res = HXSymbianSocketHelper::ResizeOrCreate(m_pCCF, uSize, m_pBuffer);
  265.         
  266.         if (HXR_OK == res)
  267.         {
  268.             m_bufDes.Set(m_pBuffer->GetBuffer(), 0, m_pBuffer->GetSize());
  269.             iStatus = KRequestPending;
  270.             socket.RecvOneOrMore(m_bufDes, 0, iStatus, m_amountRead);
  271.             SetActive();
  272.         }
  273.     }
  274.     return res;
  275. }
  276. void HXSymbianTCPReader::RunL()
  277. {
  278.     HX_RESULT res = HXR_FAILED;
  279.     if (iStatus == KErrNone)
  280.     {
  281. res = HXR_OK;
  282.     }
  283.     else if(iStatus == KErrEof)
  284.     {
  285. res = HXR_STREAM_DONE;
  286.     }
  287.     else if(iStatus == KErrNoMemory)
  288.     {
  289. res = HXR_OUTOFMEMORY;
  290.     }
  291.     IHXBuffer* pBuffer = 0;
  292.     if(res == KErrNone)
  293.     {
  294.         HXSymbianSocketHelper::CopyOrTransfer(m_pCCF, m_amountRead(), 
  295.                                               m_pBuffer, pBuffer);
  296.     }
  297.     m_pParent->OnReadDone(res, pBuffer);
  298.     HX_RELEASE(pBuffer);
  299. }
  300. void HXSymbianTCPReader::DoCancel()
  301. {}
  302. HXSymbianTCPSocket::HXSymbianTCPSocket(IHXCommonClassFactory* pCCF,
  303.        IHXResolver* pResolver) :
  304.     m_lRefCount(0),
  305.     m_pResponse(0),
  306.     m_pResolver(0),
  307.     m_state(tcpNotInitialized),
  308.     m_pConnector(0),
  309.     m_nConnectPort(0),
  310.     m_pWriter(0),
  311.     m_bWantWrite(FALSE),
  312.     m_pReader(0)
  313. {
  314.     IHXResolverResponse* pResolvResp = new HXSymbianTCPResolvResp(this);
  315.     m_pConnector = new HXSymbianTCPConnector(this);
  316.     m_pWriter = new HXSymbianTCPWriter(this);
  317.     m_pReader = new HXSymbianTCPReader(this, pCCF);
  318.     if (pResolvResp)
  319.     {
  320. pResolvResp->AddRef();
  321.     }
  322.     if (pCCF && pResolver && m_pConnector && m_pWriter && m_pReader &&
  323. (pResolvResp) &&
  324. (pResolver->Init(pResolvResp) == HXR_OK) &&
  325. (m_socketServ.Connect() == KErrNone) &&
  326. (m_socket.Open(m_socketServ, KAfInet,
  327.        KSockStream, KProtocolInetTcp) == KErrNone))
  328.     {
  329. m_pResolver = pResolver;
  330. m_pResolver->AddRef();
  331. m_state = tcpInitialized;
  332.     }
  333.     HX_RELEASE(pResolvResp);
  334. }
  335. HXSymbianTCPSocket::~HXSymbianTCPSocket()
  336. {
  337.     DPRINTF(D_TCPSOCKET, ("HXSymbianTCPSocket::~HXSymbianTCPSocket()n"));
  338.     HX_RELEASE(m_pResponse);
  339.     HX_RELEASE(m_pResolver);
  340.     if (m_state != tcpNotInitialized)
  341.     {
  342. CloseConnection(HXR_OK);
  343. m_socket.Close();
  344. m_socketServ.Close();
  345.     }
  346.     HX_DELETE(m_pConnector);
  347.     HX_DELETE(m_pWriter);
  348.     HX_DELETE(m_pReader);
  349. }
  350.     /*
  351.      *  IUnknown methods
  352.      */
  353. STDMETHODIMP HXSymbianTCPSocket::QueryInterface(THIS_
  354. REFIID riid,
  355. void** ppvObj)
  356. {
  357.     DPRINTF(D_TCPSOCKET, ("HXSymbianTCPSocket::QueryInterface()n"));
  358. QInterfaceList qiList[] =
  359. {
  360. { GET_IIDHANDLE(IID_IUnknown), (IUnknown*)(IHXTCPSocket*)this },
  361. { GET_IIDHANDLE(IID_IHXTCPSocket), (IHXTCPSocket*) this },
  362. { GET_IIDHANDLE(IID_IHXSetSocketOption), (IHXSetSocketOption*) this },
  363. };
  364.     return QIFind(qiList, QILISTSIZE(qiList), riid, ppvObj);
  365. }
  366. STDMETHODIMP_(ULONG32) HXSymbianTCPSocket::AddRef(THIS)
  367. {
  368.     return InterlockedIncrement(&m_lRefCount);
  369. }
  370. STDMETHODIMP_(ULONG32)HXSymbianTCPSocket::Release(THIS)
  371. {
  372.     if (InterlockedDecrement(&m_lRefCount) > 0)
  373.     {
  374.         return m_lRefCount;
  375.     }
  376.     delete this;
  377.     return 0;
  378. }
  379.     /*
  380.      * IHXTCPSocket methods
  381.      *
  382.      *  Network addresses and ports are in native byte order
  383.      *
  384.      */
  385. STDMETHODIMP HXSymbianTCPSocket::Init(THIS_
  386.       IHXTCPResponse* /*IN*/ pTCPResponse)
  387. {
  388.     DPRINTF(D_TCPSOCKET, ("HXSymbianTCPSocket::Init()n"));
  389.     HX_RELEASE(m_pResponse);
  390.     m_pResponse = pTCPResponse;
  391.     if (m_pResponse)
  392. m_pResponse->AddRef();
  393.     return (m_pResponse) ? HXR_OK : HXR_FAILED;
  394. }
  395. STDMETHODIMP HXSymbianTCPSocket::SetResponse(THIS_
  396.      IHXTCPResponse* pTCPResponse)
  397. {
  398.     DPRINTF(D_TCPSOCKET, ("HXSymbianTCPSocket::SetResponse()n"));
  399.     HX_RELEASE(m_pResponse);
  400.     m_pResponse = pTCPResponse;
  401.     if (m_pResponse)
  402. m_pResponse->AddRef();
  403.     return (m_pResponse) ? HXR_OK : HXR_FAILED;
  404. }
  405. STDMETHODIMP HXSymbianTCPSocket::Bind(THIS_
  406.       UINT32 ulLocalAddr,
  407.       UINT16 nPort)
  408. {
  409.     DPRINTF(D_TCPSOCKET, ("HXSymbianTCPSocket::Bind(%08lx, %u)n",
  410.   ulLocalAddr, nPort));
  411.     HX_RESULT res = HXR_FAILED;
  412.     TInetAddr addr(ulLocalAddr, nPort);
  413.     if ((m_state == tcpInitialized) &&
  414. (m_socket.Bind(addr) == KErrNone))
  415.     {
  416. m_state = tcpBound;
  417. res = HXR_OK;
  418.     }
  419.     return res;
  420. }
  421.     /*
  422.      * pDestination is a string containing host name or dotted-ip notation
  423.      */
  424. STDMETHODIMP HXSymbianTCPSocket::Connect(THIS_
  425.  const char* pDestination,
  426.  UINT16 nPort)
  427. {
  428.     DPRINTF(D_TCPSOCKET, ("HXSymbianTCPSocket::Connect('%s', %u)n",
  429.   pDestination, nPort));
  430.     HX_RESULT res = HXR_FAILED;
  431.     if (m_state == tcpInitialized)
  432. Bind(0, 0);
  433.     if (m_state == tcpBound)
  434.     {
  435. m_state = tcpResolving;
  436. m_nConnectPort = nPort;
  437. res = m_pResolver->GetHostByName(pDestination);
  438. if (HXR_OK != res)
  439. {
  440.     m_state = tcpBound;
  441. }
  442.     }
  443.     return res;
  444. }
  445. STDMETHODIMP HXSymbianTCPSocket::Read(THIS_ UINT16 Size)
  446. {
  447.     DPRINTF(D_TCPSOCKET, ("HXSymbianTCPSocket::Read(%u)n", Size));
  448.     HX_RESULT res = HXR_FAILED;
  449.     HX_ASSERT(m_state == tcpConnected);
  450.     if ((m_state == tcpConnected) && (!m_pReader->IsActive()))
  451.     {
  452. res = DoRead(Size);
  453.     }
  454.     return res;
  455. }
  456. STDMETHODIMP HXSymbianTCPSocket::Write(THIS_ IHXBuffer* pBuffer)
  457. {
  458.     DPRINTF(D_TCPSOCKET, ("HXSymbianTCPSocket::Write()n"));
  459.     HX_RESULT res = HXR_FAILED;
  460.     HX_ASSERT(m_state == tcpConnected);
  461.     if ((m_state == tcpConnected) && pBuffer)
  462.     {
  463. if (m_pWriter->IsActive())
  464. {
  465.     // A write is in progress. Add the buffer
  466.     // to the write list
  467.     pBuffer->AddRef();
  468.     LISTPOSITION listRet = m_writeList.AddTail(pBuffer);
  469.             if( listRet == NULL )
  470.             {
  471.                 res = HXR_OUTOFMEMORY;
  472.                 HX_RELEASE(pBuffer);
  473.             }
  474. }
  475. else
  476. {
  477.     // Do the write now
  478.     m_pWriter->Write(m_socket, pBuffer);
  479. }
  480. res = HXR_OK;
  481.     }
  482.     return res;
  483. }
  484.     /************************************************************************
  485.      * Method:
  486.      *     IHXTCPSocket::WantWrite
  487.      * Purpose:
  488.      *     This method is called when you wish to write a large amount of
  489.      *     data.  If you are only writing small amounts of data, you can
  490.      *     just call Write (all data not ready to be transmitted will be
  491.      *     buffered on your behalf).  When the TCP channel is ready to be
  492.      *     written to, the response interfaces WriteReady method will be
  493.      *     called.
  494.      */
  495. STDMETHODIMP HXSymbianTCPSocket::WantWrite(THIS)
  496. {
  497.     DPRINTF(D_TCPSOCKET, ("HXSymbianTCPSocket::WantWrite()n"));
  498.     m_bWantWrite = TRUE;
  499.     return HXR_OK;
  500. }
  501.     /************************************************************************
  502.      * Method:
  503.      *     IHXTCPSocket::GetForeignAddress
  504.      * Purpose:
  505.      *     Returns the address of the other end of the TCP socket as a
  506.      *     ULONG32 in local host order
  507.      */
  508. STDMETHODIMP HXSymbianTCPSocket::GetForeignAddress(THIS_ REF(ULONG32) lAddress)
  509. {
  510.     DPRINTF(D_TCPSOCKET, ("HXSymbianTCPSocket::GetForeignAddress()n"));
  511.     HX_RESULT res = HXR_FAILED;
  512.     if (m_state == tcpConnected)
  513.     {
  514. TInetAddr addr;
  515. m_socket.RemoteName(addr);
  516. lAddress = (ULONG32)addr.Address();
  517. res = HXR_OK;
  518.     }
  519.     return res;
  520. }
  521. STDMETHODIMP HXSymbianTCPSocket::GetLocalAddress(THIS_ REF(ULONG32) lAddress)
  522. {
  523.     DPRINTF(D_TCPSOCKET, ("HXSymbianTCPSocket::GetLocalAddress()n"));
  524.     HX_RESULT res = HXR_FAILED;
  525.     if ((m_state != tcpNotInitialized) &&
  526. (m_state != tcpInitialized))
  527.     {
  528. TInetAddr addr;
  529. m_socket.LocalName(addr);
  530. lAddress = (ULONG32)addr.Address();
  531. res = HXR_OK;
  532.     }
  533.     return res;
  534. }
  535.     /************************************************************************
  536.      * Method:
  537.      *     IHXTCPSocket::GetForeignPort
  538.      * Purpose:
  539.      *     Returns the port of the other end of the TCP socket in local
  540.      *      host order.
  541.      */
  542. STDMETHODIMP HXSymbianTCPSocket::GetForeignPort(THIS_ REF(UINT16) port)
  543. {
  544.     DPRINTF(D_TCPSOCKET, ("HXSymbianTCPSocket::GetForeignPort()n"));
  545.     HX_RESULT res = HXR_FAILED;
  546.     if (m_state == tcpConnected)
  547.     {
  548. TInetAddr addr;
  549. m_socket.RemoteName(addr);
  550. port = addr.Port();
  551. res = HXR_OK;
  552.     }
  553.     return res;
  554. }
  555. STDMETHODIMP HXSymbianTCPSocket::GetLocalPort(THIS_ REF(UINT16) port)
  556. {
  557.     DPRINTF(D_TCPSOCKET, ("HXSymbianTCPSocket::GetLocalPort()n"));
  558.     HX_RESULT res = HXR_FAILED;
  559.     if ((m_state != tcpNotInitialized) &&
  560. (m_state != tcpInitialized))
  561.     {
  562. port = m_socket.LocalPort();
  563. res = HXR_OK;
  564.     }
  565.     return res;
  566. }
  567. STDMETHODIMP HXSymbianTCPSocket::SetOption(THIS_
  568.    HX_SOCKET_OPTION option,
  569.    UINT32 ulValue)
  570. {
  571.     DPRINTF(D_TCPSOCKET, ("HXSymbianTCPSocket::SetOption(%d, %lu)n",
  572.   option, ulValue));
  573.     HX_RESULT res = HXR_FAILED;
  574.     if (m_state == tcpNotInitialized)
  575.     {
  576. switch(option)
  577. {
  578. case  HX_SOCKOPT_REUSE_ADDR:
  579. case HX_SOCKOPT_REUSE_PORT:
  580.     if (m_socket.SetOpt(KSoReuseAddr, KSOLSocket, ulValue) == KErrNone)
  581.     {
  582. res = HXR_OK;
  583.     }
  584.     break;
  585. case HX_SOCKOPT_BROADCAST:
  586. case HX_SOCKOPT_SET_RECVBUF_SIZE:
  587. case HX_SOCKOPT_SET_SENDBUF_SIZE:
  588. case HX_SOCKOPT_MULTICAST_IF:
  589.     res = HXR_UNEXPECTED;
  590.     break;
  591. default:
  592.     res = HXR_FAILED;
  593.     break;
  594. };
  595.     }
  596.     return res;
  597. }
  598. HX_RESULT HXSymbianTCPSocket::GetHostByNameDone(HX_RESULT status,
  599. ULONG32 ulAddr)
  600. {
  601.     DPRINTF(D_TCPSOCKET, ("HXSymbianTCPSocket::ResolveDone(%ld, %08lx)n",
  602.   status, ulAddr));
  603.     HX_ASSERT(m_state == tcpResolving);
  604.     DECLARE_SMART_POINTER_UNKNOWN scopeRef((IHXTCPSocket*)this);
  605.     if (m_state == tcpResolving)
  606.     {
  607. if (status == HXR_OK)
  608. {
  609.     m_pConnector->Connect(m_socket, ulAddr, m_nConnectPort);
  610.     m_state = tcpConnecting;
  611. }
  612. else if (m_pResponse)
  613. {
  614.     m_pResponse->ConnectDone(status);
  615.     m_state = tcpBound;
  616. }
  617.     }
  618.     return HXR_OK;
  619. }
  620. void HXSymbianTCPSocket::OnConnect(HX_RESULT status)
  621. {
  622.     DPRINTF(D_TCPSOCKET, ("HXSymbianTCPSocket::OnConnect(%ld)n",
  623.   status));
  624.     HX_ASSERT(m_state == tcpConnecting);
  625.     DECLARE_SMART_POINTER_UNKNOWN scopeRef((IHXTCPSocket*)this);
  626.     if (status == HXR_OK)
  627.     {
  628. m_state = tcpConnected;
  629.     }
  630.     else
  631.     {
  632. m_state = tcpBound;
  633.     }
  634.     if (m_pResponse)
  635.     {
  636. m_pResponse->ConnectDone(status);
  637.     }
  638. }
  639. void HXSymbianTCPSocket::OnWriteDone(HX_RESULT status)
  640. {
  641.     DPRINTF(D_TCPSOCKET, ("HXSymbianTCPSocket::OnWriteDone(%ld)n", status));
  642.     DECLARE_SMART_POINTER_UNKNOWN scopeRef((IHXTCPSocket*)this);
  643.     if (status == HXR_OK)
  644.     {
  645. if (m_writeList.GetCount() > 0)
  646. {
  647.     // Write the next buffer in the list
  648.     IHXBuffer* pBuffer = (IHXBuffer*)m_writeList.RemoveHead();
  649.     m_pWriter->Write(m_socket, pBuffer);
  650.     HX_RELEASE(pBuffer);
  651. }
  652. // Signal WriteReady() if we don't have any
  653. // writes pending and the response object
  654. // wants these calls
  655. if (!m_pWriter->IsActive() && m_bWantWrite && m_pResponse)
  656. {
  657.     m_pResponse->WriteReady(HXR_OK);
  658. }
  659.     }
  660.     else
  661.     {
  662. CloseConnection(status);
  663.     }
  664. }
  665. void HXSymbianTCPSocket::OnReadDone(HX_RESULT status, IHXBuffer* pBuffer)
  666. {
  667.     DPRINTF(D_TCPSOCKET, ("HXSymbianTCPSocket::OnReadDone(%ld)n", status));
  668.     DECLARE_SMART_POINTER_UNKNOWN scopeRef((IHXTCPSocket*)this);
  669.     if (m_pResponse)
  670.     {
  671.         // We assume error reporting/handling is done in the response object
  672.         // via the value of status passed here.
  673. m_pResponse->ReadDone(status, pBuffer);
  674.     }
  675.     if (status != HXR_OK)
  676.     {
  677. CloseConnection(status);
  678.     }
  679. }
  680. HX_RESULT HXSymbianTCPSocket::DoRead(UINT16 size)
  681. {
  682.     DPRINTF(D_TCPSOCKET, ("HXSymbianTCPSocket::DoRead(%u)n", size));
  683.     HX_RESULT res = HXR_FAILED;
  684.     if (m_pReader)
  685.     {
  686.         res = m_pReader->Read(m_socket, size);
  687.     }
  688.     return res;
  689. }
  690. void HXSymbianTCPSocket::CloseConnection(HX_RESULT status)
  691. {
  692.     DPRINTF(D_TCPSOCKET, ("HXSymbianTCPSocket::CloseConnection(%ld)n",
  693.   status));
  694.     if (m_state != tcpNotInitialized)
  695.     {
  696. // Cancel any connector
  697. if (m_pConnector->IsActive())
  698. {
  699.     m_socket.CancelConnect();
  700.     m_pConnector->Cancel();
  701. }
  702. // Cancel writer
  703. if (m_pWriter->IsActive())
  704. {
  705.     m_socket.CancelWrite();
  706.     m_pWriter->Cancel();
  707. }
  708. if (m_pReader->IsActive())
  709. {
  710.     m_socket.CancelRecv();
  711.     m_pReader->Cancel();
  712. }
  713. // Clear the writer list
  714. while (m_writeList.GetCount() > 0)
  715. {
  716.     // Write the next buffer in the list
  717.     IHXBuffer* pBuffer = (IHXBuffer*)m_writeList.RemoveHead();
  718.     HX_RELEASE(pBuffer);
  719. }
  720. if ((m_state != tcpInitialized) && m_pResponse)
  721. {
  722.     m_pResponse->Closed(HXR_OK);
  723. }
  724. m_state = tcpInitialized;
  725.     }
  726. }