|
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 "CTCPTransport.h" |
|
21 |
|
22 const int KMaxIpAddressSize = 30; |
|
23 const int KMaxPortSize = 10; |
|
24 const char KSeparator[] = { ":" }; |
|
25 |
|
26 CTCPTransport::CTCPTransport() |
|
27 : iSocket(INVALID_SOCKET) |
|
28 { |
|
29 } |
|
30 |
|
31 CTCPTransport::~CTCPTransport() |
|
32 { |
|
33 WSACleanup(); |
|
34 } |
|
35 |
|
36 int CTCPTransport::Connect( const string &aDestination ) |
|
37 { |
|
38 int ret = 0; |
|
39 |
|
40 // Extract the ipaddress andport number |
|
41 int offset = aDestination.find( KSeparator, 0 ); |
|
42 if( offset == -1 ) |
|
43 { |
|
44 return SOCKET_ERROR; // Find a better error code |
|
45 } |
|
46 |
|
47 char ipaddress[KMaxIpAddressSize]; |
|
48 char port[KMaxPortSize]; |
|
49 memset( ipaddress, 0, sizeof(ipaddress) ); |
|
50 memset( port, 0, sizeof(port) ); |
|
51 aDestination.copy( ipaddress, offset, 0 ); |
|
52 aDestination.copy( port, aDestination.length()-offset, offset+1 ); |
|
53 |
|
54 SOCKADDR_IN addr; |
|
55 addr.sin_family = AF_INET; |
|
56 addr.sin_port = htons( atoi(port) ); |
|
57 addr.sin_addr.S_un.S_addr = inet_addr( ipaddress ); |
|
58 |
|
59 WORD version; |
|
60 WSADATA wsaData; |
|
61 |
|
62 version = MAKEWORD( 2, 2 ); |
|
63 if( WSAStartup( version, &wsaData ) ) |
|
64 { |
|
65 return WSAGetLastError(); |
|
66 } |
|
67 |
|
68 // open the socket |
|
69 iSocket = socket( AF_INET, SOCK_STREAM, 0 ); |
|
70 if( iSocket == INVALID_SOCKET ) |
|
71 { |
|
72 return WSAGetLastError(); |
|
73 } |
|
74 |
|
75 ret = connect( iSocket, (struct sockaddr*)&addr, sizeof(addr) ); |
|
76 if( ret == SOCKET_ERROR ) |
|
77 { |
|
78 return WSAGetLastError(); |
|
79 } |
|
80 |
|
81 return 0; |
|
82 } |
|
83 |
|
84 |
|
85 void CTCPTransport::Disconnect() |
|
86 { |
|
87 if( iSocket != INVALID_SOCKET ) |
|
88 { |
|
89 closesocket( iSocket ); |
|
90 iSocket = INVALID_SOCKET; |
|
91 } |
|
92 } |
|
93 |
|
94 int CTCPTransport::RequestSend( const char* aData, const int aLength ) |
|
95 { |
|
96 int bytes_sent_this_round; |
|
97 int total_bytes_to_send = aLength; |
|
98 int total_bytes_sent = 0; |
|
99 |
|
100 while( total_bytes_sent < total_bytes_to_send ) |
|
101 { |
|
102 bytes_sent_this_round = send( iSocket, &(aData[total_bytes_sent]), total_bytes_to_send - total_bytes_sent, 0 ); |
|
103 if( bytes_sent_this_round == SOCKET_ERROR ) |
|
104 { |
|
105 return WSAGetLastError(); |
|
106 } |
|
107 total_bytes_sent += bytes_sent_this_round; |
|
108 } |
|
109 return 0; |
|
110 } |
|
111 |
|
112 int CTCPTransport::RequestReceive( char *aBuff, const int aLength ) |
|
113 { |
|
114 int received_byte_count = 0; |
|
115 int bytes_to_receive = aLength; |
|
116 int bytes_received_this_round = 1; |
|
117 |
|
118 while( received_byte_count < bytes_to_receive ) |
|
119 { |
|
120 bytes_received_this_round = recv( iSocket, &(aBuff[received_byte_count]), bytes_to_receive - received_byte_count, 0 ); |
|
121 if( bytes_received_this_round == SOCKET_ERROR ) |
|
122 { |
|
123 return WSAGetLastError(); |
|
124 } |
|
125 else if( bytes_received_this_round == 0 ) |
|
126 { |
|
127 return SOCKET_ERROR; |
|
128 } |
|
129 |
|
130 received_byte_count += bytes_received_this_round; |
|
131 } |
|
132 |
|
133 return 0; |
|
134 } |
|
135 |