javacommons/comms/ipclib/clientserver/src.s60/creceiver.cpp
branchRCL_3
changeset 19 04becd199f91
child 35 85266cc22c7f
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2008 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: CReceiver enables asynchronous communication with server side
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "logger.h"
       
    20 
       
    21 #include "creceiver.h"
       
    22 #include "rcomms.h"
       
    23 #include "ipcclient.h"
       
    24 #include "common.h"
       
    25 
       
    26 namespace java
       
    27 {
       
    28 namespace comms
       
    29 {
       
    30 
       
    31 const TInt DEFAULT_BUFFER_SIZE  = 2048;
       
    32 
       
    33 CReceiver::CReceiver(IpcClient& aParent, IpcListener& aListener, RComms& aSession)
       
    34         : CActive(EPriorityStandard), mParent(aParent), mListener(aListener), mSession(aSession)
       
    35 {
       
    36     CActiveScheduler::Add(this);
       
    37     mBuffer.Create(DEFAULT_BUFFER_SIZE);
       
    38 }
       
    39 
       
    40 CReceiver::~CReceiver()
       
    41 {
       
    42     mBuffer.Close();
       
    43 }
       
    44 
       
    45 void CReceiver::Receive()
       
    46 {
       
    47     mSession.Receive(mBuffer, mRequiredLength, iStatus);
       
    48     SetActive();
       
    49 }
       
    50 
       
    51 void CReceiver::RunL()
       
    52 {
       
    53     switch (iStatus.Int())
       
    54     {
       
    55     case KErrNone:
       
    56     {
       
    57         char* buf = desToMessage(mBuffer);
       
    58         mListener.processMessage(reinterpret_cast<ipcMessage_t*>(buf));
       
    59         delete[] buf;
       
    60         Receive();
       
    61     }
       
    62     break;
       
    63 
       
    64     case KErrOverflow:
       
    65     {
       
    66         // buffer was not big enough for the message so reallocate buffer with correct size
       
    67         mBuffer.Close();
       
    68         mBuffer.ReAllocL(mRequiredLength());
       
    69         WLOG2(EJavaComms, "%s: increasing buffer size to %d", __PRETTY_FUNCTION__, mRequiredLength());
       
    70         Receive();
       
    71     }
       
    72     break;
       
    73 
       
    74     default:
       
    75         ELOG2(EJavaComms, "%s failed, err = %d", __PRETTY_FUNCTION__, iStatus.Int());
       
    76         mParent.error(iStatus.Int());
       
    77         break;
       
    78     }
       
    79 }
       
    80 
       
    81 void CReceiver::DoCancel()
       
    82 {
       
    83     mSession.CancelReceive();
       
    84 }
       
    85 
       
    86 TInt CReceiver::RunError(TInt aError)
       
    87 {
       
    88     ELOG2(EJavaComms, "%s failed, err = %d", __PRETTY_FUNCTION__, aError);
       
    89     return KErrNone;
       
    90 }
       
    91 
       
    92 
       
    93 } // namespace comms
       
    94 } // namespace java