00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <e32base.h>
00026 #include <e32math.h>
00027
00028 #include "clientsession.h"
00029
00030 #include "cobainserver.h"
00031 #include "clientservercommon.h"
00032
00033 #ifdef __WINS__
00034 static const TUint KServerMinHeapSize = 0x1000;
00035 static const TUint KServerMaxHeapSize = 0x10000;
00036 #endif
00037
00038 static TInt StartServer();
00039 static TInt CreateServerProcess();
00040
00042
00044
00045 RCobainClientSession::RCobainClientSession()
00046 : iIsConnected(EFalse)
00047 {
00048 }
00049
00050 RCobainClientSession::~RCobainClientSession()
00051 {
00052 Log::Print(_L("~RCobainClientSession()"));
00053
00054 if( iIsConnected ) {
00055 Close();
00056 iIsConnected = EFalse;
00057 }
00058
00059 Log::Print(_L("~RCobainClientSession() exiting.."));
00060 }
00061
00062 void RCobainClientSession::ConstructL()
00063 {
00064
00065 User::LeaveIfError(Connect());
00066 }
00067
00068
00069 void RCobainClientSession::DiscoverPeers(TProtocol aProtocol,
00070 TUint aServiceID,
00071 TPckgBuf<TInt> *aPeerCountBuffer,
00072 TRequestStatus &aNotifyStatus)
00073 {
00074 TInt p[4];
00075
00076 p[0] = aProtocol;
00077 p[1] = (TInt)aPeerCountBuffer;
00078 p[2] = (TInt)aServiceID;
00079 SendReceive(CCobainServer::EGetNumPeers, p, aNotifyStatus);
00080 }
00081
00082 void RCobainClientSession::FetchPeerData(TInt aPeerOrdinal, TPeerDataBuf *aPeerBuffer,
00083 TRequestStatus &aNotifyStatus)
00084 {
00085 TInt p[4];
00086
00087 p[0] = aPeerOrdinal;
00088 p[1] = (TInt)aPeerBuffer;
00089 SendReceive(CCobainServer::EGetPeer, p, aNotifyStatus);
00090 }
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136 TInt RCobainClientSession::Connect()
00137 {
00138 TInt rc = ::StartServer();
00139
00140 __ASSERT_ALWAYS(rc == KErrNone,
00141 User::Panic(KCobainClientPanic, KErrCCPServerStartFailed));
00142
00143 TVersion version(KCobainServerMajorVersion,
00144 KCobainServerMinorVersion,
00145 KCobainServerBuildVersion);
00146
00147 rc = CreateSession(KCobainServerName,
00148 version,
00149 KNumMessageSlots);
00150
00151 return rc;
00152 }
00153
00154
00155 void RCobainClientSession::StartListeningL(TProtocol aProtocol,
00156 TUint aPort, TUint aExtraInfo)
00157 {
00158 TInt p[4];
00159
00160 p[0] = (TInt)aProtocol;
00161 p[1] = (TInt)aPort;
00162 p[2] = (TInt)aExtraInfo;
00163
00164 User::LeaveIfError(SendReceive(CCobainServer::EStartListen, p));
00165 }
00166
00167
00168 void RCobainClientSession::StopListening(TProtocol aProtocol, TUint aPort)
00169 {
00170 TInt p[4];
00171
00172 p[0] = (TInt)aProtocol;
00173 p[1] = (TInt)aPort;
00174
00175
00176 SendReceive(CCobainServer::EAbortListen, p);
00177 }
00178
00179
00180 void RCobainClientSession::GetIncomingSocketL(TRequestStatus &aNotifyStatus)
00181 {
00182 TInt p[4];
00183
00184
00185 SendReceive(CCobainServer::EGetIncomingConnection, p, aNotifyStatus);
00186 }
00187
00188
00189 RCobainSocket* RCobainClientSession::ConnectSocketL(CNetworkPeer *aPeer)
00190 {
00191 TInt p[4];
00192 TPeerData data;
00193 TPckgBuf<TInt> id_buf;
00194
00195 Log::Print(_L("RCobainClientSession::ConnectSocketL()"));
00196
00197
00198 aPeer->WritePeerData(data);
00199 TPeerDataBuf peer_buf(data);
00200
00201 p[0] = aPeer->GetProtocol();
00202 p[1] = (TInt)&peer_buf;
00203 p[2] = (TInt)&id_buf;
00204
00205 Log::Print(_L("calling SendReceive(): EConnect"));
00206
00207 User::LeaveIfError(SendReceive(CCobainServer::EConnect, p));
00208
00209 RCobainSocket *socket = new (ELeave) RCobainSocket(*this, id_buf());
00210
00211 Log::Print(_L("RCobainClientSession::ConnectSocketL() returning.."));
00212
00213 return socket;
00214 }
00215
00216
00217 void RCobainClientSession::CloseSocket(TInt aId, TBool aImmediately)
00218 {
00219 TInt p[4];
00220
00221 p[0] = aId;
00222 p[1] = (TInt)aImmediately;
00223 SendReceive(CCobainServer::ECloseSocket, p);
00224 }
00225
00226 TInt RCobainClientSession::Send(TInt aSocketId, const TDesC8 *aBuf)
00227 {
00228 TInt p[4];
00229
00230 p[0] = aSocketId;
00231 p[1] = aBuf->Length();
00232 p[2] = (TInt)aBuf;
00233
00234 return SendReceive(CCobainServer::ESend, p);
00235 }
00236
00237 void RCobainClientSession::Receive(TInt aSocketId,
00238 TDes8* aBuffer,
00239 TRequestStatus& aStatus)
00240 {
00241 TInt p[4];
00242
00243 p[0] = aSocketId;
00244 p[1] = (TInt)aBuffer;
00245 SendReceive(CCobainServer::EReceive, p, aStatus);
00246 }
00247
00248 void RCobainClientSession::CancelReceive(TInt aSocketId)
00249 {
00250 TInt p[4];
00251
00252 p[0] = aSocketId;
00253 SendReceive(CCobainServer::ECancelReceive, p);
00254 }
00255
00257
00259
00260 static TInt StartServer()
00261 {
00262 TInt rc = 0;
00263
00264 TFindServer findServer(KCobainServerName);
00265 TFullName name;
00266
00267 rc = findServer.Next(name);
00268 if (rc == KErrNone)
00269 {
00270
00271 return KErrNone;
00272 }
00273
00274 RSemaphore semaphore;
00275 rc = semaphore.CreateGlobal(KCobainServerSemaphoreName, 0);
00276 if (rc != KErrNone)
00277 {
00278 return rc;
00279 }
00280
00281 rc = CreateServerProcess();
00282 if (rc != KErrNone)
00283 {
00284 return rc;
00285 }
00286
00287 semaphore.Wait();
00288 semaphore.Close();
00289
00290 return KErrNone;
00291 }
00292
00293 static TInt CreateServerProcess()
00294 {
00295 TInt rc = 0;
00296
00297 const TUidType serverUid(KNullUid, KNullUid, KCobainServerUid3);
00298
00299 #ifdef __WINS__
00300
00301 RLibrary lib;
00302 rc = lib.Load(KCobainServerFilename, serverUid);
00303 if( rc != KErrNone )
00304 {
00305 return rc;
00306 }
00307
00308
00309 TLibraryFunction functionWinsMain = lib.Lookup(1);
00310
00311
00312 TThreadFunction serverThreadFunction =
00313 reinterpret_cast<TThreadFunction>(functionWinsMain());
00314
00315 TName threadName(KCobainServerName);
00316
00317
00318 threadName.AppendNum(Math::Random(), EHex);
00319
00320 RThread server;
00321
00322 rc = server.Create(threadName,
00323 serverThreadFunction,
00324 KDefaultStackSize,
00325 NULL,
00326 &lib,
00327 NULL,
00328 KServerMinHeapSize,
00329 KServerMaxHeapSize,
00330 EOwnerProcess);
00331
00332 lib.Close();
00333
00334 if (rc != KErrNone)
00335 {
00336 return rc;
00337 }
00338
00339 server.SetPriority(EPriorityMore);
00340
00341 #else
00342 RProcess server;
00343
00344 rc = server.Create(KCobainServerFilename,
00345 _L(""),
00346 serverUid);
00347 if (rc != KErrNone)
00348 {
00349 return rc;
00350 }
00351
00352 #endif
00353
00354
00355 server.Resume();
00356 server.Close();
00357
00358 return KErrNone;
00359 }
00360
00361
00362 GLDEF_C TInt E32Dll(TDllReason) {
00363 return KErrNone;
00364 }