|
1 // server.cpp |
|
2 // |
|
3 // Copyright (c) 2006 - 2010 Accenture. All rights reserved. |
|
4 // This component and the accompanying materials are made available |
|
5 // under the terms of the "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 // Accenture - Initial contribution |
|
11 // |
|
12 |
|
13 #include "server.h" |
|
14 #include "pipe.h" |
|
15 #include "console.h" |
|
16 #include "file.h" |
|
17 #include "null.h" |
|
18 #include "persistentconsole.h" |
|
19 #include "readwrite.h" |
|
20 #include "clientserver.h" |
|
21 #include "session.h" |
|
22 |
|
23 |
|
24 #ifndef EKA2 |
|
25 |
|
26 // |
|
27 // TServerStart. |
|
28 // |
|
29 |
|
30 TServerStart::TServerStart() |
|
31 { |
|
32 } |
|
33 |
|
34 void TServerStart::SignalL() |
|
35 { |
|
36 RThread starter; |
|
37 User::LeaveIfError(starter.Open(iId)); |
|
38 starter.RequestComplete(iStatus,KErrNone); |
|
39 starter.Close(); |
|
40 } |
|
41 |
|
42 #endif |
|
43 |
|
44 |
|
45 // |
|
46 // CShutdownTimer. |
|
47 // |
|
48 |
|
49 CShutdownTimer::CShutdownTimer() |
|
50 : CTimer(-1) |
|
51 { |
|
52 CActiveScheduler::Add(this); |
|
53 } |
|
54 |
|
55 void CShutdownTimer::ConstructL() |
|
56 { |
|
57 CTimer::ConstructL(); |
|
58 } |
|
59 |
|
60 void CShutdownTimer::Start() |
|
61 { |
|
62 After(KShutdownDelay); |
|
63 } |
|
64 |
|
65 void CShutdownTimer::RunL() |
|
66 { |
|
67 CActiveScheduler::Stop(); |
|
68 } |
|
69 |
|
70 |
|
71 // |
|
72 // CIoServer. |
|
73 // |
|
74 |
|
75 CIoServer::CIoServer() |
|
76 #ifdef EKA2 |
|
77 : CServer2(0, ESharableSessions) |
|
78 #else |
|
79 : CServer(0, ESharableSessions) |
|
80 #endif |
|
81 { |
|
82 |
|
83 } |
|
84 |
|
85 #ifdef EKA2 |
|
86 CServer2* CIoServer::NewLC() |
|
87 #else |
|
88 CServer* CIoServer::NewLC() |
|
89 #endif |
|
90 { |
|
91 CIoServer* self=new(ELeave) CIoServer; |
|
92 CleanupStack::PushL(self); |
|
93 self->ConstructL(); |
|
94 return self; |
|
95 } |
|
96 |
|
97 CIoServer::~CIoServer() |
|
98 { |
|
99 if (iIoObjectContainerIndex) |
|
100 { |
|
101 iIoObjectContainerIndex->Remove(iIoObjectContainer); |
|
102 } |
|
103 delete iIoObjectContainerIndex; |
|
104 #ifdef IOSRV_LOGGING |
|
105 delete iLog; |
|
106 #endif |
|
107 } |
|
108 |
|
109 void CIoServer::ConstructL() |
|
110 { |
|
111 #if defined (__WINS__) && !defined (EKA2) |
|
112 RThread().SetPriority(EPriorityAbsoluteHigh); |
|
113 #else |
|
114 RProcess().SetPriority(::EPriorityHigh); |
|
115 #endif |
|
116 User::LeaveIfError(iFs.Connect()); |
|
117 #ifdef IOSRV_LOGGING |
|
118 iLog = CIoLog::NewL(iFs); |
|
119 #endif |
|
120 iConfig.Init(); // Ignore error (defaults will be used). |
|
121 StartL(KIoServerName); |
|
122 iIoObjectContainerIndex = CObjectConIx::NewL(); |
|
123 iIoObjectContainer = iIoObjectContainerIndex->CreateL(); |
|
124 iShutdownTimer.ConstructL(); |
|
125 iShutdownTimer.Start(); |
|
126 } |
|
127 |
|
128 #ifdef EKA2 |
|
129 CSession2* CIoServer::NewSessionL(const TVersion&, const RMessage2&) const |
|
130 #else |
|
131 CSharableSession* CIoServer::NewSessionL(const TVersion&) const |
|
132 #endif |
|
133 { |
|
134 CIoSession* session = new(ELeave) CIoSession(); |
|
135 LOG(CIoLog::Printf(_L("CIoSession 0x%08x created\r\n"), session)); |
|
136 return session; |
|
137 } |
|
138 |
|
139 void CIoServer::AddSession() |
|
140 { |
|
141 ++iSessionCount; |
|
142 iShutdownTimer.Cancel(); |
|
143 } |
|
144 |
|
145 void CIoServer::DropSession() |
|
146 { |
|
147 if (--iSessionCount == 0) |
|
148 { |
|
149 iShutdownTimer.Start(); |
|
150 } |
|
151 } |
|
152 |
|
153 CIoPipe& CIoServer::CreatePipeLC() |
|
154 { |
|
155 CIoPipe* pipe = CIoPipe::NewLC(); |
|
156 iIoObjectContainer->AddL(pipe); |
|
157 return *pipe; |
|
158 } |
|
159 |
|
160 CIoConsole& CIoServer::CreateConsoleLC(const TDesC& aImplementation, const TDesC& aTitle, TSize aSize, MIoWriteEndPoint* aUnderlyingConsole, TUint aOptions) |
|
161 { |
|
162 CIoConsole* underlying = NULL; |
|
163 while (aUnderlyingConsole && aUnderlyingConsole->IoepIsType(RIoHandle::EPersistentConsole)) |
|
164 { |
|
165 aUnderlyingConsole = ((CIoPersistentConsole*)aUnderlyingConsole)->TransientWriter(); |
|
166 } |
|
167 if (aUnderlyingConsole && aUnderlyingConsole->IoepIsConsole()) |
|
168 { |
|
169 underlying = (CIoConsole*)aUnderlyingConsole; |
|
170 } |
|
171 |
|
172 CIoConsole* console = CIoConsole::NewLC(aImplementation, aTitle, aSize, iConfig, underlying, aOptions); |
|
173 iIoObjectContainer->AddL(console); |
|
174 return *console; |
|
175 } |
|
176 |
|
177 CIoFile& CIoServer::CreateFileLC(const TDesC& aName, RIoFile::TMode aMode) |
|
178 { |
|
179 CIoFile* file = CIoFile::NewLC(iFs, aName, aMode); |
|
180 iIoObjectContainer->AddL(file); |
|
181 return *file; |
|
182 } |
|
183 |
|
184 CIoNull& CIoServer::CreateNullLC() |
|
185 { |
|
186 CIoNull* null = CIoNull::NewLC(); |
|
187 iIoObjectContainer->AddL(null); |
|
188 return *null; |
|
189 } |
|
190 |
|
191 CIoPersistentConsole& CIoServer::CreatePersistentConsoleLC(const TDesC& aName, const TDesC& aTitle, const RMsg& aMessage) |
|
192 { |
|
193 CIoPersistentConsole* pcons = CIoPersistentConsole::NewLC(aName, aTitle, *this, aMessage); |
|
194 iIoObjectContainer->AddL(pcons); |
|
195 return *pcons; |
|
196 } |
|
197 |
|
198 |
|
199 CIoReadObject& CIoServer::CreateReadObjLC() |
|
200 { |
|
201 CIoReadObject* obj = CIoReadObject::NewLC(iNextReadObjId++); |
|
202 iIoObjectContainer->AddL(obj); |
|
203 return *obj; |
|
204 } |
|
205 |
|
206 CIoWriteObject& CIoServer::CreateWriteObjLC() |
|
207 { |
|
208 CIoWriteObject* obj = CIoWriteObject::NewLC(iNextWriteObjId++); |
|
209 iIoObjectContainer->AddL(obj); |
|
210 return *obj; |
|
211 } |
|
212 |
|
213 CIoReadObject* CIoServer::NextReadObj(TThreadId aOwningThread) const |
|
214 { |
|
215 return (CIoReadObject*)DoFindObj(RIoHandle::EReadObject, aOwningThread, ETrue); |
|
216 } |
|
217 |
|
218 CIoReadWriteObject* CIoServer::DoFindObj(RIoHandle::TType aType, TThreadId aOwningThread, TBool aNext) const |
|
219 { |
|
220 CIoReadWriteObject* lastMatchingObj = NULL; |
|
221 const TInt numObjs = iIoObjectContainer->Count(); |
|
222 for (TInt i = 0; i < numObjs; ++i) |
|
223 { |
|
224 CIoObject* obj = (CIoObject*)(*iIoObjectContainer)[i]; |
|
225 if (obj->IsType(aType)) |
|
226 { |
|
227 CIoReadWriteObject* readWriteObj = (CIoReadWriteObject*)obj; |
|
228 if (aNext && readWriteObj->IsOwner(aOwningThread) && !readWriteObj->OpenedByOwner()) |
|
229 { |
|
230 // aNext==ETrue means we return the first matching object that isn't already opened by its owner |
|
231 return readWriteObj; |
|
232 } |
|
233 else if (!aNext && readWriteObj->IsOwner(aOwningThread) && readWriteObj->OpenedByOwner()) |
|
234 { |
|
235 // aNext==EFalse means we return the last matching object that *is* opened |
|
236 lastMatchingObj = readWriteObj; |
|
237 } |
|
238 } |
|
239 } |
|
240 return lastMatchingObj; |
|
241 } |
|
242 |
|
243 CIoWriteObject* CIoServer::NextWriteObj(TThreadId aOwningThread) const |
|
244 { |
|
245 return (CIoWriteObject*)DoFindObj(RIoHandle::EWriteObject, aOwningThread, ETrue); |
|
246 } |
|
247 |
|
248 CIoReadObject* CIoServer::LastOpenedReadObj(TThreadId aOwningThread) const |
|
249 { |
|
250 return (CIoReadObject*)DoFindObj(RIoHandle::EReadObject, aOwningThread, EFalse); |
|
251 } |
|
252 |
|
253 CIoWriteObject* CIoServer::LastOpenedWriteObj(TThreadId aOwningThread) const |
|
254 { |
|
255 return (CIoWriteObject*)DoFindObj(RIoHandle::EWriteObject, aOwningThread, EFalse); |
|
256 } |
|
257 |
|
258 CIoPersistentConsole& CIoServer::FindPersistentConsoleL(const TDesC& aName) |
|
259 { |
|
260 const TInt numObjs = iIoObjectContainer->Count(); |
|
261 for (TInt i=0; i<numObjs; ++i) |
|
262 { |
|
263 CIoObject* obj = (CIoObject*)(*iIoObjectContainer)[i]; |
|
264 if (obj->IsType(RIoHandle::EPersistentConsole)) |
|
265 { |
|
266 CIoPersistentConsole& pcons = (CIoPersistentConsole&)*obj; |
|
267 if (pcons.Name().Compare(aName)==0) |
|
268 { |
|
269 return pcons; |
|
270 } |
|
271 } |
|
272 } |
|
273 User::Leave(KErrNotFound); |
|
274 return *(CIoPersistentConsole*)1; // To keep the compiler happy. |
|
275 } |
|
276 |
|
277 CIoObject* CIoServer::FindObjectByName(RIoHandle::TType aType, TInt& aFindHandle, const TDesC& aMatch, TName& aName) const |
|
278 { |
|
279 FOREVER |
|
280 { |
|
281 TInt err = iIoObjectContainer->FindByName(aFindHandle, aMatch, aName); |
|
282 if (err == KErrNone) |
|
283 { |
|
284 CIoObject* obj = (CIoObject*)iIoObjectContainer->At(aFindHandle); |
|
285 if (obj->IsType(aType)) |
|
286 { |
|
287 return obj; |
|
288 } |
|
289 } |
|
290 else |
|
291 { |
|
292 break; |
|
293 } |
|
294 } |
|
295 return NULL; |
|
296 } |
|
297 |
|
298 CIoObject& CIoServer::OpenObjectLC(TInt aFindHandle) |
|
299 { |
|
300 CIoObject* obj = (CIoObject*)iIoObjectContainer->At(aFindHandle); |
|
301 if (obj) |
|
302 { |
|
303 User::LeaveIfError(obj->Open()); |
|
304 CleanupClosePushL(*obj); |
|
305 return *obj; |
|
306 } |
|
307 User::Leave(KErrNotFound); |
|
308 return *(CIoObject*)1; // To keep the compiler happy. |
|
309 } |
|
310 |
|
311 const TIoConfig& CIoServer::Config() |
|
312 { |
|
313 return iConfig; |
|
314 } |
|
315 |
|
316 void CIoServer::PersistentConsoleAddL(const TDesC16& aName, const CIoPersistentConsole& aCons) |
|
317 { |
|
318 if (iPersistentConsoleNames.Find(aName)!=NULL) |
|
319 { |
|
320 User::Leave(KErrAlreadyExists); |
|
321 } |
|
322 iPersistentConsoleNames.InsertL(&aName, &aCons); |
|
323 } |
|
324 |
|
325 void CIoServer::PersistentConsoleRemove(const TDesC16& aName, const CIoPersistentConsole& aCons) |
|
326 { |
|
327 if (iPersistentConsoleNames.Find(aName) == &aCons) |
|
328 { |
|
329 iPersistentConsoleNames.Remove(&aName); |
|
330 } |
|
331 } |
|
332 |
|
333 TInt CIoServer::RunError(TInt aError) |
|
334 { |
|
335 if (aError == KErrBadDescriptor) |
|
336 { |
|
337 PanicClient(Message(), EPanicBadDescriptor); |
|
338 } |
|
339 else if (aError == KErrBadHandle) |
|
340 { |
|
341 PanicClient(Message(), EPanicBadHandle); |
|
342 } |
|
343 else |
|
344 { |
|
345 LOG(CIoLog::LogCompletion(Message(), aError)); |
|
346 Complete(Message(), aError); |
|
347 } |
|
348 |
|
349 ReStart(); |
|
350 return KErrNone; |
|
351 } |
|
352 |
|
353 |
|
354 // |
|
355 // Statics. |
|
356 // |
|
357 |
|
358 void PanicClient(const RMsg& aMessage, TIoPanicReason aReason) |
|
359 { |
|
360 aMessage.Panic(KIoServerName, aReason); |
|
361 } |
|
362 |
|
363 #ifdef EKA2 |
|
364 static void RunServerL() |
|
365 #else |
|
366 static void RunServerL(TServerStart& aStart) |
|
367 #endif |
|
368 { |
|
369 CActiveScheduler* scheduler = new(ELeave) CActiveScheduler; |
|
370 CleanupStack::PushL(scheduler); |
|
371 CActiveScheduler::Install(scheduler); |
|
372 |
|
373 CIoServer::NewLC(); |
|
374 #ifdef EKA2 |
|
375 User::RenameThread(KIoServerName); |
|
376 RProcess::Rendezvous(KErrNone); |
|
377 #else |
|
378 User::LeaveIfError(RThread().Rename(KIoServerName)); |
|
379 aStart.SignalL(); |
|
380 #endif |
|
381 CActiveScheduler::Start(); |
|
382 CleanupStack::PopAndDestroy(2, scheduler); |
|
383 } |
|
384 |
|
385 #ifdef EKA2 |
|
386 static TInt RunServer() |
|
387 #else |
|
388 static TInt RunServer(TServerStart& aStart) |
|
389 #endif |
|
390 { |
|
391 __UHEAP_MARK; |
|
392 CTrapCleanup* cleanup=CTrapCleanup::New(); |
|
393 TInt r = KErrNoMemory; |
|
394 if (cleanup) |
|
395 { |
|
396 #ifdef EKA2 |
|
397 TRAP(r, RunServerL()); |
|
398 #else |
|
399 TRAP(r, RunServerL(aStart)); |
|
400 #endif |
|
401 delete cleanup; |
|
402 } |
|
403 __UHEAP_MARKEND; |
|
404 return r; |
|
405 } |
|
406 |
|
407 |
|
408 #if defined(__WINS__) && !defined(EKA2) |
|
409 |
|
410 static TInt ThreadFunction(TAny* aParms) |
|
411 { |
|
412 return RunServer(*static_cast<TServerStart*>(aParms)); |
|
413 } |
|
414 |
|
415 IMPORT_C TInt WinsMain(); |
|
416 EXPORT_C TInt WinsMain() |
|
417 { |
|
418 return reinterpret_cast<TInt>(&ThreadFunction); |
|
419 } |
|
420 |
|
421 TInt E32Dll(TDllReason) |
|
422 { |
|
423 return KErrNone; |
|
424 } |
|
425 |
|
426 #else |
|
427 #ifdef EKA2 |
|
428 |
|
429 TInt E32Main() |
|
430 { |
|
431 return RunServer(); |
|
432 } |
|
433 |
|
434 #else |
|
435 |
|
436 TInt TServerStart::GetCommand() |
|
437 { |
|
438 RProcess p; |
|
439 if (p.CommandLineLength() != (sizeof(TServerStart) / sizeof(TText))) |
|
440 { |
|
441 return KErrGeneral; |
|
442 } |
|
443 TPtr ptr(reinterpret_cast<TText*>(this), 0, (sizeof(TServerStart) / sizeof(TText))); |
|
444 p.CommandLine(ptr); |
|
445 return KErrNone; |
|
446 } |
|
447 |
|
448 TInt E32Main() |
|
449 { |
|
450 TServerStart start; |
|
451 TInt r = start.GetCommand(); |
|
452 if (r == KErrNone) |
|
453 { |
|
454 r = RunServer(start); |
|
455 } |
|
456 return r; |
|
457 } |
|
458 |
|
459 #endif |
|
460 #endif |
|
461 |
|
462 #ifndef EKA2 |
|
463 const TAny* MessagePtr(const RMsg& aMessage, TInt aParam) |
|
464 { |
|
465 const TAny* ptr; |
|
466 switch (aParam) |
|
467 { |
|
468 case 0: |
|
469 { |
|
470 ptr = aMessage.Ptr0(); |
|
471 break; |
|
472 } |
|
473 case 1: |
|
474 { |
|
475 ptr = aMessage.Ptr1(); |
|
476 break; |
|
477 } |
|
478 case 2: |
|
479 { |
|
480 ptr = aMessage.Ptr2(); |
|
481 break; |
|
482 } |
|
483 case 3: |
|
484 { |
|
485 ptr = aMessage.Ptr3(); |
|
486 break; |
|
487 } |
|
488 default: |
|
489 { |
|
490 ASSERT(EFalse); |
|
491 ptr = NULL; |
|
492 } |
|
493 } |
|
494 return ptr; |
|
495 } |
|
496 #endif |
|
497 |
|
498 TInt DesLengthL(const RMsg& aMessage, TInt aParam) |
|
499 { |
|
500 #ifdef EKA2 |
|
501 return aMessage.GetDesLengthL(aParam); |
|
502 #else |
|
503 return aMessage.Client().GetDesLength(MessagePtr(aMessage, aParam)); |
|
504 #endif |
|
505 } |
|
506 |
|
507 TInt MaxDesLengthL(const RMsg& aMessage, TInt aParam) |
|
508 { |
|
509 #ifdef EKA2 |
|
510 return aMessage.GetDesMaxLengthL(aParam); |
|
511 #else |
|
512 return aMessage.Client().GetDesMaxLength(MessagePtr(aMessage, aParam)); |
|
513 #endif |
|
514 } |
|
515 |
|
516 void MessageReadL(const RMsg& aMessage, TInt aParam, TDes8& aDes) |
|
517 { |
|
518 #ifdef EKA2 |
|
519 aMessage.ReadL(aParam, aDes); |
|
520 #else |
|
521 aMessage.ReadL(MessagePtr(aMessage, aParam), aDes); |
|
522 #endif |
|
523 } |
|
524 |
|
525 void MessageReadL(const RMsg& aMessage, TInt aParam, TDes8& aDes, TInt aOffset) |
|
526 { |
|
527 #ifdef EKA2 |
|
528 aMessage.ReadL(aParam, aDes, aOffset); |
|
529 #else |
|
530 aMessage.ReadL(MessagePtr(aMessage, aParam), aDes, aOffset); |
|
531 #endif |
|
532 } |
|
533 |
|
534 void MessageReadL(const RMsg& aMessage, TInt aParam, TDes16& aDes) |
|
535 { |
|
536 #ifdef EKA2 |
|
537 aMessage.ReadL(aParam, aDes); |
|
538 #else |
|
539 aMessage.ReadL(MessagePtr(aMessage, aParam), aDes); |
|
540 #endif |
|
541 } |
|
542 |
|
543 void MessageReadL(const RMsg& aMessage, TInt aParam, TDes16& aDes, TInt aOffset) |
|
544 { |
|
545 #ifdef EKA2 |
|
546 aMessage.ReadL(aParam, aDes, aOffset); |
|
547 #else |
|
548 aMessage.ReadL(MessagePtr(aMessage, aParam), aDes, aOffset); |
|
549 #endif |
|
550 } |
|
551 |
|
552 void MessageWriteL(const RMsg& aMessage, TInt aParam, const TDesC8& aDes) |
|
553 { |
|
554 #ifdef EKA2 |
|
555 aMessage.WriteL(aParam, aDes); |
|
556 #else |
|
557 aMessage.WriteL(MessagePtr(aMessage, aParam), aDes); |
|
558 #endif |
|
559 } |
|
560 |
|
561 void MessageWriteL(const RMsg& aMessage, TInt aParam, const TDesC16& aDes) |
|
562 { |
|
563 #ifdef EKA2 |
|
564 aMessage.WriteL(aParam, aDes); |
|
565 #else |
|
566 aMessage.WriteL(MessagePtr(aMessage, aParam), aDes); |
|
567 #endif |
|
568 } |
|
569 |
|
570 TInt MessageWrite(const RMsg& aMessage, TInt aParam, const TDesC8& aDes) |
|
571 { |
|
572 #ifdef EKA2 |
|
573 return aMessage.Write(aParam, aDes); |
|
574 #else |
|
575 return aMessage.Write(MessagePtr(aMessage, aParam), aDes); |
|
576 #endif |
|
577 } |
|
578 |
|
579 TInt MessageWrite(const RMsg& aMessage, TInt aParam, const TDesC16& aDes) |
|
580 { |
|
581 #ifdef EKA2 |
|
582 return aMessage.Write(aParam, aDes); |
|
583 #else |
|
584 return aMessage.Write(MessagePtr(aMessage, aParam), aDes); |
|
585 #endif |
|
586 } |
|
587 |
|
588 TBool MessagePending(const RMsg& aMessage) |
|
589 { |
|
590 #ifdef EKA2 |
|
591 return !aMessage.IsNull(); |
|
592 #else |
|
593 return aMessage != RMessagePtr(); |
|
594 #endif |
|
595 } |
|
596 |
|
597 TThreadId ClientThreadIdL(const RMsg& aMessage) |
|
598 { |
|
599 TThreadId clientThreadId; |
|
600 #ifdef EKA2 |
|
601 RThread clientThread; |
|
602 aMessage.ClientL(clientThread); |
|
603 clientThreadId = clientThread.Id(); |
|
604 clientThread.Close(); |
|
605 #else |
|
606 clientThreadId = aMessage.Client().Id(); |
|
607 #endif |
|
608 return clientThreadId; |
|
609 } |
|
610 |
|
611 TFullName ClientNameL(const RMsg& aMessage) |
|
612 { |
|
613 #ifdef EKA2 |
|
614 RThread clientThread; |
|
615 aMessage.ClientL(clientThread); |
|
616 TFullName clientName(clientThread.FullName()); |
|
617 clientThread.Close(); |
|
618 return clientName; |
|
619 #else |
|
620 return aMessage.Client().FullName(); |
|
621 #endif |
|
622 } |
|
623 |
|
624 void Complete(const RMsg& aMessage, TInt aError) |
|
625 { |
|
626 LOG(CIoLog::LogCompletion(aMessage, aError)); |
|
627 aMessage.Complete(aError); |
|
628 } |
|
629 |
|
630 void CompleteIfPending(const RMsg& aMessage, TInt aError) |
|
631 { |
|
632 if (MessagePending(aMessage)) |
|
633 { |
|
634 Complete(aMessage, aError); |
|
635 } |
|
636 } |