testexecmgmt/ucc/Source/Uccs.v2/DeviceControlChannel/CTcpPort.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 #include <stdio.h>
       
    21 #include <assert.h>
       
    22 
       
    23 #include "CTcpPort.h"
       
    24 
       
    25 CTcpPort::CTcpPort()
       
    26 : iPort(0)
       
    27 {
       
    28 }
       
    29 
       
    30 CTcpPort::~CTcpPort()
       
    31 {
       
    32 }
       
    33 
       
    34 int CTcpPort::OpenPort( char *aPort )
       
    35 {
       
    36 	WORD version;
       
    37 	WSADATA wsaData;
       
    38 
       
    39 	version = MAKEWORD( 2, 2 );
       
    40 	if( WSAStartup( version, &wsaData ) )
       
    41 	{
       
    42 		return WSAGetLastError();
       
    43 	}
       
    44 
       
    45 	// Create a socket
       
    46 	iLocalSock = socket( AF_INET, SOCK_STREAM, 0 ) ;
       
    47 	if( iLocalSock == INVALID_SOCKET )
       
    48 	{
       
    49 		return WSAGetLastError();
       
    50 	}
       
    51 
       
    52 	// Set iPort for later use
       
    53 	iPort = atoi( 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 	return 0;
       
    81 }
       
    82 
       
    83 void CTcpPort::ClosePort()
       
    84 {
       
    85 	closesocket(iRemoteSock);
       
    86 	closesocket(iLocalSock);
       
    87 	WSACleanup();
       
    88 }
       
    89 
       
    90 int CTcpPort::ReceiveBytes( char *aBuff, int *aLength )
       
    91 {
       
    92 	int received_byte_count = 0;
       
    93 	int bytes_to_receive = *aLength;
       
    94 	int bytes_received_this_round = 1;
       
    95 
       
    96 	while( received_byte_count < bytes_to_receive )
       
    97 	{
       
    98 		bytes_received_this_round = recv( iRemoteSock, &(aBuff[received_byte_count]), bytes_to_receive - received_byte_count, 0 );
       
    99 		if( bytes_received_this_round == SOCKET_ERROR )
       
   100 		{
       
   101 			*aLength = received_byte_count;
       
   102 			return WSAGetLastError();
       
   103 		}
       
   104 		else if( bytes_received_this_round == 0 )
       
   105 		{
       
   106 			// The socket has been killed
       
   107 
       
   108 			// Now close it at this end
       
   109 			closesocket(iRemoteSock);
       
   110 			closesocket(iLocalSock);
       
   111 			WSACleanup();
       
   112 
       
   113 			// Wait for a new connection
       
   114 			char port[20];
       
   115 			sprintf(port, "%d", iPort);
       
   116 			int ret = OpenPort( port );
       
   117 			if( ret != 0 )
       
   118 			{
       
   119 				return ret;
       
   120 			}
       
   121 		}
       
   122 
       
   123 		received_byte_count += bytes_received_this_round;
       
   124 	}
       
   125 
       
   126 	*aLength = received_byte_count;
       
   127 	return 0;
       
   128 }
       
   129 
       
   130 int CTcpPort::SendBytes( char *aBuff, int *aLength )
       
   131 {
       
   132 	int bytes_sent_this_round;
       
   133 	int total_bytes_to_send = *aLength;
       
   134 	int total_bytes_sent = 0;
       
   135 
       
   136 	while( total_bytes_sent < total_bytes_to_send )
       
   137 	{
       
   138 		bytes_sent_this_round = send( iRemoteSock, &(aBuff[total_bytes_sent]), total_bytes_to_send - total_bytes_sent, 0 );
       
   139 		if( bytes_sent_this_round == SOCKET_ERROR )
       
   140 		{
       
   141 			return WSAGetLastError();
       
   142 		}
       
   143 		total_bytes_sent += bytes_sent_this_round;
       
   144 	}
       
   145 	return 0;
       
   146 }
       
   147