testtoolsconn/stat/desktop/source/common/transport/src/statsocket.cpp
changeset 0 3da2a79470a7
equal deleted inserted replaced
-1:000000000000 0:3da2a79470a7
       
     1 /*
       
     2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 #include "stdafx.h"
       
    22 #include "statsocket.h"
       
    23 
       
    24 ////////////////////////////////////////////////////////////////////////////////////////
       
    25 // 
       
    26 CSTATSocket::CSTATSocket()
       
    27 : m_pSocket((CClientSocket*)NULL),
       
    28   m_pFile((CSocketFile*)NULL),
       
    29   m_pArchiveIn((CArchive*)NULL),
       
    30   m_pArchiveOut((CArchive*)NULL),
       
    31   m_pListen((CClientSocket*)NULL),
       
    32   IsRecvData(false),
       
    33   ID(0), Length(0), pData(NULL)
       
    34 {
       
    35 }
       
    36 
       
    37 
       
    38 ////////////////////////////////////////////////////////////////////////////////////////
       
    39 // 
       
    40 CSTATSocket::~CSTATSocket()
       
    41 {
       
    42 	Disconnect();
       
    43 	Release();
       
    44 }
       
    45 
       
    46 
       
    47 ////////////////////////////////////////////////////////////////////////////////////////
       
    48 // 
       
    49 int CSTATSocket::Initialise(void)
       
    50 {
       
    51 	return ITS_OK;
       
    52 }
       
    53 
       
    54 
       
    55 ////////////////////////////////////////////////////////////////////////////////////////
       
    56 // 
       
    57 int CSTATSocket::Connect(const char *pAddress)
       
    58 {
       
    59 	m_pSocket = new CClientSocket(this);
       
    60 
       
    61 	if (m_pSocket)
       
    62 	{
       
    63 		// if address supplied then we are attempting a connection
       
    64 		// else we are listening for a connection
       
    65 		if (pAddress && (*pAddress))
       
    66 		{
       
    67 			if (!m_pSocket->Create())
       
    68 			{
       
    69 				Disconnect();
       
    70 				return GENERAL_FAILURE;
       
    71 			}
       
    72 
       
    73 			char *p = strrchr(pAddress, ':');
       
    74 			int iPort = 700;
       
    75 			if (p)
       
    76 			{
       
    77 				(*p) = (char)0;
       
    78 
       
    79 				p++;
       
    80 				if (p && *p)
       
    81 					iPort = atoi(p);
       
    82 			}
       
    83 			int iRetry = 0;
       
    84 			while (!m_pSocket->Connect(pAddress, iPort))
       
    85 			{
       
    86 				iRetry++;
       
    87 				if (iRetry > 5)
       
    88 				{
       
    89 					Disconnect();
       
    90 					return E_TOOMANYERRORS;
       
    91 				}
       
    92 
       
    93 				Sleep(250);
       
    94 			}
       
    95 		}
       
    96 		else
       
    97 		{
       
    98 			if (!m_pListen->Accept(*m_pSocket))
       
    99 			{
       
   100 				Disconnect();
       
   101 				return E_CONNECTIONFAILED;
       
   102 			}
       
   103 
       
   104 			// don't need this object anymore
       
   105 			BYTE Buffer[50];
       
   106 			m_pListen->ShutDown();
       
   107 
       
   108 			while(m_pListen->Receive(Buffer,50) > 0);
       
   109 
       
   110 			delete m_pListen;
       
   111 			m_pListen = NULL;
       
   112 		}
       
   113 
       
   114 		m_pFile = new CSocketFile(m_pSocket);
       
   115 		m_pArchiveIn = new CArchive(m_pFile,CArchive::load);
       
   116 		m_pArchiveOut = new CArchive(m_pFile,CArchive::store);
       
   117 
       
   118 		// send acknowledgement message
       
   119 		Send('A', "Connected", 9);
       
   120 	}
       
   121 	else
       
   122 		return E_OUTOFMEM;
       
   123 
       
   124 	return ITS_OK;
       
   125 }
       
   126 
       
   127 
       
   128 ////////////////////////////////////////////////////////////////////////////////////////
       
   129 // 
       
   130 int CSTATSocket::Listen(const char *pPort)
       
   131 {
       
   132 	m_pListen = new CClientSocket(this);
       
   133 
       
   134 	if (m_pListen)
       
   135 	{
       
   136 		if (!m_pListen->Create(atoi(pPort)))
       
   137 			return E_CONNECTIONFAILED;
       
   138 
       
   139 		if (!m_pListen->Listen())
       
   140 			return E_CONNECTIONFAILED;
       
   141 	}
       
   142 	else
       
   143 		return E_OUTOFMEM;
       
   144 
       
   145 	return ITS_OK;
       
   146 }
       
   147 
       
   148 
       
   149 ////////////////////////////////////////////////////////////////////////////////////////
       
   150 // 
       
   151 int CSTATSocket::Send(const char cIdentifier, const char *pData, const unsigned long ulLength)
       
   152 {
       
   153 	if (m_pArchiveOut != NULL)
       
   154 	{
       
   155 		TRY
       
   156 		{
       
   157 			(*m_pArchiveOut) << cIdentifier;
       
   158 			(*m_pArchiveOut) << ulLength;
       
   159 
       
   160 			if (ulLength)
       
   161 				m_pArchiveOut->Write(pData, ulLength);
       
   162 
       
   163 			m_pArchiveOut->Flush();
       
   164 		}
       
   165 		CATCH(CFileException, e)
       
   166 		{
       
   167 			Disconnect();
       
   168 			return E_WRITEFAILED;
       
   169 		}
       
   170 		END_CATCH
       
   171 	}
       
   172 
       
   173 	return ITS_OK;
       
   174 }
       
   175 
       
   176 
       
   177 ////////////////////////////////////////////////////////////////////////////////////////
       
   178 // External: passed received data back to calling function
       
   179 int CSTATSocket::Receive(char *cIdentifier, char **ppData, unsigned long *pLength)
       
   180 {
       
   181 	int ret = NO_DATA_AT_PORT;
       
   182 
       
   183 	if (IsRecvData)
       
   184 	{
       
   185 		(*cIdentifier) = ID;
       
   186 		(*pLength) = Length;
       
   187 		(*ppData) = pData;
       
   188 
       
   189 		IsRecvData = false;
       
   190 		ret = ITS_OK;
       
   191 	}
       
   192 
       
   193 	return ret;
       
   194 }
       
   195 	
       
   196 	
       
   197 ////////////////////////////////////////////////////////////////////////////////////////
       
   198 // Internal: Receives data from the socket
       
   199 int CSTATSocket::ReceiveData()
       
   200 {
       
   201 	if (!IsRecvData)
       
   202 	{
       
   203 		unsigned long ActualRead = 0;
       
   204 
       
   205 		(*m_pArchiveIn) >> ID;
       
   206 		(*m_pArchiveIn) >> Length;
       
   207 
       
   208 		if(pData)
       
   209 		{
       
   210 			delete [] pData;
       
   211 		}
       
   212 
       
   213 		if (Length)
       
   214 		{
       
   215 			pData = new char [Length + 1];
       
   216 			if (pData)
       
   217 			{
       
   218 				ActualRead = m_pArchiveIn->Read(pData, Length);
       
   219 				*(pData + Length) = (char)0;
       
   220 			}
       
   221 			else
       
   222 				Length = 0;
       
   223 		}
       
   224 
       
   225 		IsRecvData = true;
       
   226 		return true;
       
   227 	}
       
   228 
       
   229 	return false;
       
   230 }
       
   231 
       
   232 
       
   233 ////////////////////////////////////////////////////////////////////////////////////////
       
   234 // 
       
   235 int CSTATSocket::Disconnect(void)
       
   236 {
       
   237 	if (m_pArchiveOut)
       
   238 	{
       
   239 		delete m_pArchiveOut;
       
   240 		m_pArchiveOut = NULL;
       
   241 	}
       
   242 
       
   243 	if (m_pArchiveIn)
       
   244 	{
       
   245 		delete m_pArchiveIn;
       
   246 		m_pArchiveIn = NULL;
       
   247 	}
       
   248 
       
   249 	if (m_pFile)
       
   250 	{
       
   251 		delete m_pFile;
       
   252 		m_pFile = NULL;
       
   253 	}
       
   254 
       
   255 	if (m_pSocket)
       
   256 	{
       
   257 		BYTE Buffer[50];
       
   258 		m_pSocket->ShutDown();
       
   259 
       
   260 		while(m_pSocket->Receive(Buffer,50) > 0);
       
   261 
       
   262 		delete m_pSocket;
       
   263 		m_pSocket = NULL;
       
   264 	}
       
   265 
       
   266 	return ITS_OK;
       
   267 }
       
   268 
       
   269 
       
   270 ////////////////////////////////////////////////////////////////////////////////////////
       
   271 // 
       
   272 int CSTATSocket::Release(void)
       
   273 {
       
   274 	return ITS_OK;
       
   275 }
       
   276 
       
   277 
       
   278 ////////////////////////////////////////////////////////////////////////////////////////
       
   279 //	PRIVATE FUNCTIONS
       
   280 ////////////////////////////////////////////////////////////////////////////////////////