|
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 <e32def.h> |
|
21 #include <inet.h> |
|
22 |
|
23 #include "CTCPTransport.h" |
|
24 #include "assert.h" |
|
25 |
|
26 #include <CommDbConnPref.h> |
|
27 |
|
28 |
|
29 CTCPTransport::CTCPTransport() |
|
30 { |
|
31 } |
|
32 |
|
33 CTCPTransport::~CTCPTransport() |
|
34 { |
|
35 iSocket.Close(); |
|
36 iServer.Close(); |
|
37 } |
|
38 |
|
39 TInt CTCPTransport::InitialiseL() |
|
40 { |
|
41 return KErrNone; |
|
42 } |
|
43 |
|
44 TInt CTCPTransport::Release( void ) |
|
45 { |
|
46 iSocket.Close(); |
|
47 iServer.Close(); |
|
48 return KErrNone; |
|
49 } |
|
50 |
|
51 TInt CTCPTransport::ConnectL( TDesC* aRemoteHost ) |
|
52 { |
|
53 // Retrieve the destination port location |
|
54 TInt colonPos = aRemoteHost->Locate( TChar(':') ); |
|
55 if(colonPos != KErrNotFound) |
|
56 { |
|
57 // Set the destination port |
|
58 TPtrC host( aRemoteHost->Left(colonPos) ); |
|
59 TLex portLex( aRemoteHost->Mid(colonPos + 1) ); |
|
60 TInt port = 0; |
|
61 portLex.Val( port ); |
|
62 iDestAddr.SetPort( port ); |
|
63 |
|
64 // Retrieve and set the destination ip address |
|
65 TBuf<20> ipAddress; |
|
66 ipAddress.Copy( host.Ptr(), colonPos ); |
|
67 iDestAddr.Input( ipAddress ); |
|
68 } |
|
69 else |
|
70 { |
|
71 iDestAddr.SetPort( KUCCTCPPort ); |
|
72 } |
|
73 |
|
74 // pick out the iap/snap index.. |
|
75 TInt index=0; |
|
76 TPtrC mode; |
|
77 |
|
78 TInt pipePos = aRemoteHost->Locate( TChar('|') ); |
|
79 if( pipePos != KErrNotFound ) |
|
80 { |
|
81 TPtrC tempstr( aRemoteHost->Ptr() + pipePos + 1 ); |
|
82 TInt indexPos = tempstr.Locate( TChar(':') ); |
|
83 |
|
84 if( indexPos != KErrNotFound ) |
|
85 { |
|
86 mode.Set( tempstr.Left(indexPos) ); |
|
87 TLex iapLex( tempstr.Mid(indexPos + 1) ); |
|
88 iapLex.Val( index ); |
|
89 } |
|
90 } |
|
91 |
|
92 // Connect to the server - need to retry as the first few attepts seem to fail over PPP |
|
93 User::LeaveIfError( iServer.Connect() ); |
|
94 User::LeaveIfError( iConnection.Open(iServer) ); |
|
95 |
|
96 if( index > 0 && mode.Compare( _L("IAP") ) == 0 ) |
|
97 { |
|
98 TCommDbConnPref pref; |
|
99 pref.SetIapId(index); |
|
100 pref.SetDialogPreference(ECommDbDialogPrefDoNotPrompt); |
|
101 User::LeaveIfError(iConnection.Start(pref)); |
|
102 } |
|
103 #ifdef SYMBIAN_NON_SEAMLESS_NETWORK_BEARER_MOBILITY |
|
104 else if( index > 0 && mode.Compare( _L("SNAP") ) == 0 ) |
|
105 { |
|
106 TConnSnapPref pref(index); |
|
107 User::LeaveIfError(iConnection.Start(pref)); |
|
108 } |
|
109 #endif // SYMBIAN_NON_SEAMLESS_NETWORK_BEARER_MOBILITY |
|
110 else |
|
111 { |
|
112 User::LeaveIfError(iConnection.Start()); |
|
113 } |
|
114 |
|
115 TInt ret = -1; |
|
116 while( ret ) |
|
117 { |
|
118 User::LeaveIfError( iSocket.Open(iServer, KAfInet, KSockStream, KProtocolInetTcp, iConnection) ); |
|
119 TRequestStatus status; |
|
120 iSocket.Connect( iDestAddr, status ); |
|
121 User::WaitForRequest( status ); |
|
122 ret = status.Int(); |
|
123 } |
|
124 |
|
125 return KErrNone; |
|
126 } |
|
127 |
|
128 TInt CTCPTransport::Disconnect( void ) |
|
129 { |
|
130 iSocket.Close(); |
|
131 iServer.Close(); |
|
132 return KErrNone; |
|
133 } |
|
134 |
|
135 TInt CTCPTransport::RequestSend( TDesC8 *aData, const TUint aDataLength ) |
|
136 { |
|
137 assert( aData->Length() == aDataLength ); |
|
138 // Write to the socket |
|
139 TRequestStatus status; |
|
140 iSocket.Write( *aData, status ); |
|
141 User::WaitForRequest( status ); |
|
142 return status.Int(); |
|
143 } |
|
144 |
|
145 TInt CTCPTransport::RequestReceive( TPtr8 *aRecvBufferPtr, TUint aByteCount ) |
|
146 { |
|
147 assert( aRecvBufferPtr->MaxLength() == aByteCount ); |
|
148 // Read from the socket |
|
149 TRequestStatus status; |
|
150 iSocket.Read( *aRecvBufferPtr, status ); |
|
151 User::WaitForRequest( status ); |
|
152 return status.Int(); |
|
153 } |
|
154 |
|
155 TText8 *CTCPTransport::Error( void ) |
|
156 { |
|
157 return NULL; |
|
158 } |