📄 server.cpp
字号:
// Server.cpp
//
// Copyright (c) 1999-2007 Symbian Software Ltd. All rights reserved.
//
// $Change: 937687 $
// SYSTEM HEADERS
#include <e32svr.h>
#include <e32math.h>
#include <e32uid.h>
// PROJECT HEADERS
#include "srvapi.h"
#include "server.h"
//**********************************
//CCountServServer
//**********************************
CCountServServer::CCountServServer(TInt aPriority) : CServer2(aPriority)
{
__DECLARE_NAME(_S("CCountServer"));
}
// Create and start a new count server.
void CCountServServer::New()
{
CCountServServer *pS = new CCountServServer(EPriority);
__ASSERT_ALWAYS(pS != NULL, PanicServer(ESvrCreateServer));
pS->StartL(KCountServerName);
}
// Create a new server session.
CSession2 *CCountServServer::NewSessionL(const TVersion &aVersion, const RMessage2& /* aMessage */) const
{
// check we're the right version
TVersion v(KCountServMajorVersionNumber, KCountServMinorVersionNumber, KCountServBuildVersionNumber);
if (!User::QueryVersionSupported(v, aVersion))
{
User::Leave(KErrNotSupported);
}
// make new session
return CCountServSession::NewL((CCountServServer*) this);
}
//**********************************
//CCountServSession
//**********************************
// constructor - must pass client to CSession
CCountServSession::CCountServSession(CCountServServer * aServer)
{
__DECLARE_NAME(_S("CCountServSession"));
iCountSvr = aServer;
}
CCountServSession* CCountServSession::NewL(CCountServServer * aServer)
{
return new(ELeave) CCountServSession(aServer);
}
CCountServSession::~CCountServSession()
{
CActiveScheduler::Stop();
}
void CCountServSession::ServiceL(const RMessage2& aMessage)
{
TRAPD(err, DispatchMessageL(aMessage));
aMessage.Complete(err);
}
// service a client request; test the opcode and then do appropriate servicing
void CCountServSession::DispatchMessageL(const RMessage2& aMessage)
{
switch (aMessage.Function())
{
case ECountServIncrease:
Increase();
return;
case ECountServIncreaseBy:
IncreaseBy(aMessage.Int0());
return;
case ECountServDecrease:
Decrease();
return;
case ECountServDecreaseBy:
DecreaseBy(aMessage.Int0());
return;
case ECountServReset:
Reset();
return;
case ECountServValue:
CounterValue(aMessage);
return;
// we know about the stop request, but don't support it,
// so return KErrNotSupported here
case ECountServStop:
return;
// requests we don't understand at all are a different thing,
// so panic the client here, this function also completes the message
default:
PanicClient(aMessage, EBadRequest);
return;
}
}
// increase the session counter by default (1)
void CCountServSession::Increase()
{
iCount++;
}
// increase the session counter by an integer
void CCountServSession::IncreaseBy(TInt aValue)
{
iCount = iCount + aValue;
}
// decrease the session counter by default (1)
void CCountServSession::Decrease()
{
iCount--;
}
// decrease the session counter by an integer
void CCountServSession::DecreaseBy(TInt aValue)
{
iCount = iCount - aValue;
}
// reset the session counter
void CCountServSession::Reset()
{
iCount = 0;
}
// write the counter value to a descriptor in the client address space
// This function demonstrates writing to the client.
void CCountServSession::CounterValue(const RMessage2& aMessage)
{
TPckgBuf<TInt> p(iCount);
Write(aMessage, p);
}
// panic the client
void CCountServSession::PanicClient(const RMessage2& aMessage, TInt aPanic) const
{
_LIT(KTxtServer, "CountServ server");
aMessage.Panic(KTxtServer, aPanic);
}
// write to the client thread; if unsuccessful, panic the client
void CCountServSession::Write(const RMessage2& aMessage, const TDesC8& aDes, TInt anOffset)
{
TRAPD(ret, aMessage.WriteL(0, aDes, anOffset);)
if (ret != KErrNone)
{
PanicClient(aMessage, EBadDescriptor);
}
}
//**********************************
//Global functions
//**********************************
// The count server thread.
GLDEF_C TInt CCountServServer::ThreadFunction(TAny* anArg)
{
// convert argument into semaphore reference
RSemaphore& semaphore=*(RSemaphore *)anArg;
// start scheduler and server
CActiveScheduler *pA = new CActiveScheduler;
__ASSERT_ALWAYS(pA != NULL, PanicServer(EMainSchedulerError));
CActiveScheduler::Install(pA);
CCountServServer::New();
// signal that we've started
semaphore.Signal();
// start fielding requests from clients
CActiveScheduler::Start();
// finished
return(KErrNone);
}
// Panic the server
GLDEF_C void PanicServer(TCountServPanic aPanic)
{
_LIT(KTxtServerPanic, "Count server panic");
User::Panic(KTxtServerPanic, aPanic);
}
// Create the server thread
// This function is exported from the DLL and called from the client
EXPORT_C TInt StartThread()
{
TInt res = KErrNone;
// create server - if one of this name does not already exist
TFindServer findCountServer(KCountServerName);
TFullName name;
if (findCountServer.Next(name) != KErrNone) // we don't exist already
{
RThread thread;
RSemaphore semaphore;
semaphore.CreateLocal(0); // create a semaphore so we know when thread finished
res = thread.Create(KCountServerName, // create new server thread
CCountServServer::ThreadFunction, // thread's main function
KDefaultStackSize,
KDefaultHeapSize,
KDefaultHeapSize,
&semaphore // passed as TAny* argument to thread function
);
if (res == KErrNone) // thread created ok - now start it going
{
thread.SetPriority(EPriorityNormal);
thread.Resume(); // start it going
semaphore.Wait(); // wait until it's initialized
thread.Close(); // we're no longer interested in the other thread
}
else // thread not created ok
{
thread.Close(); // therefore we've no further interest in it
}
semaphore.Close();
}
return res;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -