examples/sfexamples/RockPaperScissorsGameSourceCode_S60/RPS/src/BluetoothSocketWriterReader.cpp

00001 /*
00002 Copyright (c) 2002-2011 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description:  
00028 */ 
00029 
00030 // INCLUDE FILES
00031 
00032 #include "BluetoothSocketWriterReader.h"
00033 #include "common.hrh"
00034 /*
00035 ============================================================================
00036 CSocketReader's constructor
00037 ============================================================================
00038 */
00039 CSocketReader::CSocketReader(RSocket& aBtSocket, MSocketWriterReaderObserver& aObserver) :
00040         CActive(CActive::EPriorityStandard), iBtSocket(aBtSocket), iObserver(aObserver)
00041     {
00042         CActiveScheduler::Add(this);
00043         }
00044 
00045 /*
00046 ============================================================================
00047 CSocketReader's destructor
00048 ============================================================================
00049 */
00050 CSocketReader::~CSocketReader()
00051     {
00052     Cancel();
00053     }
00054 
00055 /*
00056 ============================================================================
00057 DoCancel is called as part of the active object's Cancel().
00058 Cancels an outstanding BT socket read operation.
00059 ============================================================================
00060 */
00061 void CSocketReader::DoCancel()
00062     {
00063         iBtSocket.CancelRead();
00064     }
00065 
00066 /*
00067 ============================================================================
00068 Handles the active object's reading data completion event.
00069 On completion it calls the callback function MSocketWriterReaderObserver::ReportData
00070 to pass to the caller the data received.
00071 ============================================================================
00072 */
00073 void CSocketReader::RunL()
00074     {
00075         iObserver.ReportData(iBuffer, iStatus.Int());
00076     }   
00077 
00078 /*
00079 ============================================================================
00080 Asynchronous call to wait for incoming data
00081 ============================================================================
00082 */
00083 void CSocketReader::ReadData()
00084     {
00085         if (!IsActive())
00086                 {
00087                 iBuffer.Zero();
00088                 iBtSocket.RecvOneOrMore(iBuffer, 0, iStatus, iLen);
00089             SetActive();
00090                 }
00091     }
00092 
00093 /*
00094 ============================================================================
00095 Handles a leave occurring in the request completion event handler RunL().
00096 ============================================================================
00097 */
00098 TInt CSocketReader::RunError(TInt aError)
00099         {
00100         iObserver.ReportData(iBuffer, aError);
00101         return KErrNone;
00102         }
00103 
00105 
00106 /*
00107 ============================================================================
00108 CSocketWriter's constructor
00109 ============================================================================
00110 */
00111 CSocketWriter::CSocketWriter(RSocket& aBtSocket, MSocketWriterReaderObserver& aObserver) :
00112         CActive(CActive::EPriorityStandard), iBtSocket(aBtSocket), iObserver(aObserver) 
00113     {
00114         CActiveScheduler::Add(this);
00115         }
00116 
00117 /*
00118 ============================================================================
00119 CSocketWriter's destructor
00120 ============================================================================
00121 */
00122 CSocketWriter::~CSocketWriter()
00123     {
00124     Cancel();
00125     }
00126 
00127 /*
00128 ============================================================================
00129 DoCancel is called as part of the active object's Cancel().
00130 Cancels an outstanding BT socket write operation.
00131 ============================================================================
00132 */
00133 void CSocketWriter::DoCancel()
00134     {
00135         iBtSocket.CancelWrite();
00136     }
00137 
00138 /*
00139 ============================================================================
00140 Handles the active object's writing data completion event.
00141 On completion it calls the callback function MSocketWriterReaderObserver::WriteComplete
00142 to notify the caller if the write was succesful
00143 ============================================================================
00144 */
00145 void CSocketWriter::RunL()
00146         {
00147         iObserver.WriteComplete(iStatus.Int());
00148     }
00149 
00150 /*
00151 ============================================================================
00152 Asynchronous call to send data to the remote device.
00153 Note that we handle only one event at a time. Data to write to the remote device
00154 is not queued if an outstanding write operation is already in progress.
00155 ============================================================================
00156 */
00157 TInt CSocketWriter::Write(const TDesC8& aBuf)
00158     {
00159         TInt err = KErrNone;
00160 
00161         if (!IsActive())
00162                 {
00163                 iBtSocket.Write(aBuf,iStatus);
00164                 SetActive();
00165                 }
00166         else
00167                 {
00168                 err = KErrInUse;
00169                 }
00170         return(err);
00171     }
00172 
00173 /*
00174 ============================================================================
00175 Handles a leave occurring in the request completion event handler RunL().
00176 ============================================================================
00177 */
00178 TInt CSocketWriter::RunError(TInt aError)
00179         {
00180         iObserver.WriteComplete(aError);
00181         return KErrNone;
00182         }

Generated by  doxygen 1.6.2