testexecmgmt/ucc/GenericService/TestDriverService/src/CListeningServer.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 * CListeningServer.h
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 #include <stdio.h>
       
    22 
       
    23 #include "CListeningServer.h"
       
    24 
       
    25 const int KTimeOut		= 120000; // 2 minute timeout for recieve calls
       
    26 
       
    27 CListeningServer::CListeningServer()
       
    28 	{
       
    29 	}
       
    30 
       
    31 CListeningServer::~CListeningServer()
       
    32 	{
       
    33 	}
       
    34 
       
    35 int CListeningServer::Open( const int aPort )
       
    36 	{
       
    37 	WORD version;
       
    38 	WSADATA wsaData;
       
    39 
       
    40 	version = MAKEWORD( 2, 2 );
       
    41 	if( WSAStartup( version, &wsaData ) )
       
    42 		{
       
    43 		return WSAGetLastError();
       
    44 		}
       
    45 
       
    46 	// Create a socket
       
    47 	iLocalSock = socket( AF_INET, SOCK_STREAM, 0 ) ;
       
    48 	if( iLocalSock == INVALID_SOCKET )
       
    49 		{
       
    50 		return WSAGetLastError();
       
    51 		}
       
    52 
       
    53 	iPort = aPort;
       
    54 
       
    55 	// Set the socket
       
    56 	iLocalAddr.sin_family = AF_INET;		
       
    57 	iLocalAddr.sin_port = htons( iPort );
       
    58 	iLocalAddr.sin_addr.S_un.S_addr = inet_addr( "0.0.0.0" );
       
    59 
       
    60 	// Bind
       
    61 	if( bind(iLocalSock,(struct sockaddr*)&iLocalAddr, sizeof(SOCKADDR_IN)) != 0 )
       
    62 		{
       
    63 		return WSAGetLastError();
       
    64 		}
       
    65 
       
    66 	// Listen to the socket
       
    67 	if( listen( iLocalSock, 1 ) == SOCKET_ERROR ) 
       
    68 		{
       
    69 		return WSAGetLastError();
       
    70 		}
       
    71 	
       
    72 	// Wait for the next connection
       
    73 	int remote_addr_len = sizeof(iRemoteAddr);
       
    74 	iRemoteSock = accept( iLocalSock, (struct sockaddr*)&iRemoteAddr, &remote_addr_len );
       
    75 	if( iRemoteSock == INVALID_SOCKET )
       
    76 		{
       
    77 		return WSAGetLastError();
       
    78 		}
       
    79 
       
    80 	// Finally set the socket timeout for recieve calls (2 mins)
       
    81 	return setsockopt( iRemoteSock, SOL_SOCKET, SO_RCVTIMEO, (char *)&KTimeOut, sizeof(KTimeOut) );
       
    82 	}
       
    83 
       
    84 void CListeningServer::Close()
       
    85 {
       
    86 	closesocket(iRemoteSock);
       
    87 	closesocket(iLocalSock);
       
    88 	WSACleanup();
       
    89 }
       
    90 
       
    91 int CListeningServer::Send( const int aValue )
       
    92 	{
       
    93 	int ret = 0;
       
    94 	int bytes_sent_this_round;
       
    95 	int total_bytes_to_send = sizeof(int);
       
    96 	int total_bytes_sent = 0;
       
    97 
       
    98 	int pre_send_data = htonl(aValue);
       
    99 	char* buf = (char*)&pre_send_data;
       
   100 
       
   101 	while( (total_bytes_sent < total_bytes_to_send) && !ret )
       
   102 		{
       
   103 		bytes_sent_this_round = send( iRemoteSock, &(buf[total_bytes_sent]), total_bytes_to_send - total_bytes_sent, 0 );
       
   104 		if( bytes_sent_this_round == SOCKET_ERROR )
       
   105 			{
       
   106 			ret = WSAGetLastError();
       
   107 			}
       
   108 		total_bytes_sent += bytes_sent_this_round;
       
   109 		}
       
   110 	return ret;
       
   111 	}
       
   112 
       
   113 int CListeningServer::Recieve( int& aValue )
       
   114 	{
       
   115 	int ret = 0;
       
   116 	int received_byte_count = 0;
       
   117 	int bytes_to_receive = sizeof(int);
       
   118 	int bytes_received_this_round = 1;
       
   119 	char buf[sizeof(int)];
       
   120 	memset( buf, 0, sizeof(int) );
       
   121 
       
   122 	while( (received_byte_count < bytes_to_receive) && !ret )
       
   123 		{
       
   124 		bytes_received_this_round = recv( iRemoteSock, &(buf[received_byte_count]), bytes_to_receive - received_byte_count, 0 );
       
   125 		if( bytes_received_this_round == SOCKET_ERROR )
       
   126 			{
       
   127 			ret = WSAGetLastError();
       
   128 			}
       
   129 		else if( bytes_received_this_round == 0 )
       
   130 			{
       
   131 			// The socket has been killed
       
   132 			// Now close it at this end
       
   133 			closesocket(iRemoteSock);
       
   134 			closesocket(iLocalSock);
       
   135 			WSACleanup();
       
   136 			ret = INVALID_SOCKET;
       
   137 			}
       
   138 
       
   139 		received_byte_count += bytes_received_this_round;
       
   140 		}
       
   141 
       
   142 	if( received_byte_count == bytes_to_receive )
       
   143 		{
       
   144 		aValue = *((int*)buf);
       
   145 		aValue = ntohl(aValue);
       
   146 		}
       
   147 
       
   148 	return ret;
       
   149 	}