|
1 /* |
|
2 * Copyright (c) 2004 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: See class definition below. |
|
15 * |
|
16 */ |
|
17 |
|
18 #ifndef __CTCBASECONNECTION_H__ |
|
19 #define __CTCBASECONNECTION_H__ |
|
20 |
|
21 // INCLUDES |
|
22 #include <e32base.h> |
|
23 #include <es_sock.h> |
|
24 #include "MTcBearerObserver.h" |
|
25 #include <in_sock.h> |
|
26 |
|
27 // CLASS DEFINITION |
|
28 /** |
|
29 * CTcBaseConnection implements a base class for socket connections. |
|
30 * Basically provides core send and receive operations. Send and receive |
|
31 * cannot be used at the same time (this is a design choice; those are |
|
32 * not actually ever needed simultaneously with the TestClient). |
|
33 */ |
|
34 class CTcBaseConnection |
|
35 : public CActive |
|
36 { |
|
37 protected: // Enumerations |
|
38 |
|
39 /// Internal connection state |
|
40 enum TState |
|
41 { |
|
42 /// Not connected |
|
43 EIdle, |
|
44 /// Waiting for incoming connections or connecting to remote address |
|
45 EConnecting, |
|
46 /// Waiting after unsuccesfull connection attempt |
|
47 EWaitingRetry, |
|
48 /// Connected with a remote party |
|
49 EConnected, |
|
50 /// Sending or waiting to send data |
|
51 ESending, |
|
52 /// Receiving or waiting to receive data |
|
53 EReceiving |
|
54 }; |
|
55 |
|
56 public: // Constructors and destructor |
|
57 |
|
58 /// Destructor |
|
59 ~CTcBaseConnection(); |
|
60 |
|
61 protected: // Constructors and destructor |
|
62 |
|
63 /// Default constructor |
|
64 CTcBaseConnection(); |
|
65 |
|
66 public: // From CActive |
|
67 |
|
68 void RunL(); |
|
69 void DoCancel(); |
|
70 |
|
71 public: // New methods |
|
72 |
|
73 /// Initialize port and start waiting for incoming connections |
|
74 void ConnectL(); |
|
75 |
|
76 /// Connect to remote address |
|
77 void ConnectL( TInetAddr& aRemoteAddr ); |
|
78 |
|
79 /// Shut down port and stop waiting for incoming connections |
|
80 void Close(); |
|
81 |
|
82 /** |
|
83 * Send a block of data contained in aDes |
|
84 * |
|
85 * @param aDes Data to be sent |
|
86 * @exceptions Panics with KErrNotReady if not connected, and with |
|
87 * KErrInUse if we were already sending or receiving. |
|
88 */ |
|
89 void Send( const TDesC8& aDes ); |
|
90 |
|
91 /** |
|
92 * Receive a block of data to aDes. Completes when aDes is full. |
|
93 * |
|
94 * @param aDes Buffer for incoming data |
|
95 * @exceptions Panics with KErrNotReady if not connected, and with |
|
96 * KErrInUse if we were already sending or receiving. |
|
97 */ |
|
98 void Receive( TDes8& aDes ); |
|
99 |
|
100 /** |
|
101 * Receive a block of data to aDes. Completes when at least one |
|
102 * character (byte) has been received. Length of aDes is changed |
|
103 * to reflect the amount of bytes received. |
|
104 * |
|
105 * @param aDes Buffer for incoming data |
|
106 * @exceptions Panics with KErrNotReady if not connected, and with |
|
107 * KErrInUse if we were already sending or receiving. |
|
108 */ |
|
109 void ReceiveOneOrMore( TDes8& aDes ); |
|
110 |
|
111 /** |
|
112 * Set connection event observer. Use NULL to deregister. |
|
113 * |
|
114 * @param aObserver Pointer to observer object or NULL. |
|
115 */ |
|
116 void SetObserver( MTcBearerObserver* aObserver ); |
|
117 |
|
118 /** |
|
119 * Set connection. |
|
120 * |
|
121 * @param aSocketServ |
|
122 * @param aConnection |
|
123 */ |
|
124 void SetConnection( RSocketServ* aSocketServ, RConnection* aConnection ); |
|
125 |
|
126 /// @return Local port number for the connection |
|
127 TInt LocalPort() const; |
|
128 |
|
129 protected: // Abstract methods |
|
130 |
|
131 /// Called to perform bearer specific port setup. Implemented |
|
132 /// by derived classes. |
|
133 virtual void SetupPortL() = 0; |
|
134 |
|
135 virtual void SetupPort2L(); |
|
136 |
|
137 protected: // New metods |
|
138 |
|
139 /** |
|
140 * Notifies a registered observer about state transitions. |
|
141 * |
|
142 * @param aState Connection state |
|
143 * @param aStatus System-wide error code or KErrNone |
|
144 */ |
|
145 void Notify( TState aState, TInt aStatus ); |
|
146 |
|
147 protected: // Data |
|
148 |
|
149 /// Connection state. |
|
150 TState iState; |
|
151 |
|
152 /// Pointer to connection observer. Not owned. |
|
153 MTcBearerObserver* iObserver; |
|
154 |
|
155 /// Socket server session. Not owned. |
|
156 RSocketServ* iSocketServ; |
|
157 |
|
158 /// Data transfer socket. Owned. |
|
159 RSocket iSocket; |
|
160 |
|
161 /// Listening socket. Owned. |
|
162 RSocket iListeningSocket; |
|
163 |
|
164 /// Socket address (generic). Owned. |
|
165 TSockAddr iSockAddr; |
|
166 |
|
167 /// Local port where we're listening |
|
168 TInt iLocalPort; |
|
169 |
|
170 /// Transfer length for RecvOneOrMore(), value not used but |
|
171 /// must still be present. |
|
172 TSockXfrLength iIgnoredLength; |
|
173 |
|
174 RConnection* iConnection; |
|
175 |
|
176 // Remote address for connection retry |
|
177 TInetAddr iRemoteAddr; |
|
178 |
|
179 // Timer for waiting for retry |
|
180 RTimer iTimer; |
|
181 |
|
182 }; |
|
183 |
|
184 #endif // __CTCBASECONNECTION_H__ |