Main Page | Class Hierarchy | Data Structures | File List | Data Fields | Globals

cobainsocket.cpp

Go to the documentation of this file.
00001 /*
00002  * cobainsocket.cpp,v 1.21 2004/01/10 14:27:11 mattid Exp
00003  *
00004  * COBAIN - Communications API for EPOC Environments
00005  * Copyright (C) 2002-2004 Matti Dahlbom, Matti Kokkola
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  *
00021  * Contact: Matti Dahlbom <matti@777-team.org>
00022  *          Matti Kokkola <matti.kokkola@iki.fi>
00023  */
00024 
00025 #include "cobain.h"
00026 #include "clientsession.h"
00027 
00028 RCobainSocket::RCobainSocket(RCobainClientSession &aSession,
00029                              TInt aId)
00030     : CActive(CActive::EPriorityStandard),
00031       iSession(aSession),
00032       iId(aId),
00033       iIsClosed(EFalse)
00034 {
00035     CActiveScheduler::Add(this);
00036 }
00037 
00038 EXPORT_C RCobainSocket::~RCobainSocket()
00039 {
00040     // close the socket 
00041     Close(ETrue);
00042 
00043     // mark the listener null as it is not used any more
00044     iSocketListener = NULL;
00045 }
00046 
00047 EXPORT_C void RCobainSocket::SendL(const TDesC8 *aBuffer)
00048 {
00049     if( iIsClosed ) {
00050         User::Leave(KErrCobainSocketClosed);
00051     }
00052 
00053     TInt ret = iSession.Send(iId, aBuffer);
00054     if( (iSocketListener != NULL) && (ret != KErrNone) ) {
00055         iSocketListener->SocketError(ret, this);
00056     }
00057 }
00058 
00059 EXPORT_C void RCobainSocket::Close(TBool aImmediately)
00060 {
00061     if( iIsClosed ) {
00062         // already closed
00063         return;
00064     }
00065 
00066     // close the network connection
00067     iSession.CloseSocket(iId, aImmediately);
00068 
00069     // stop receiving data
00070     Cancel();
00071 
00072     // mark the socket closed
00073     iIsClosed = ETrue;
00074 }
00075 
00076 EXPORT_C void RCobainSocket::SetSocketListener(MSocketListener *aSocketListener)
00077 {
00078     __ASSERT_ALWAYS(aSocketListener != NULL,
00079                     User::Panic(KCobainSocketPanic, KErrCobainSocketNullSocketListener));
00080 
00081     // cancel the existing request if any 
00082     Cancel();
00083 
00084     iSocketListener = aSocketListener;
00085 
00086     // start receiving data
00087     Receive();
00088 }
00089 
00090 // launches asynchronous call to receive data
00091 void RCobainSocket::Receive()
00092 {
00093     Log::Print(_L("RCobainSocket::Receive()"));
00094 
00095     iSession.Receive(iId, &iRecvBuf, iStatus);
00096     SetActive();
00097 
00098     Log::Print(_L("RCobainSocket::Receive() exiting"));
00099 }
00100 
00102 // from CActive
00104 
00105 void RCobainSocket::DoCancel() 
00106 {
00107 }
00108 
00109 // data available
00110 void RCobainSocket::RunL() 
00111 {
00112     Log::Print(_L("RCobainSocket::RunL()"));
00113 
00114     // assert there is a socket listener
00115     if( iSocketListener == NULL ) {
00116         return;
00117     }
00118 
00119     TInt status = iStatus.Int();
00120     if( status != KErrNone ) {
00121         // notify the socket listener
00122         if( status == KErrDisconnected ) {
00123             // close this socket
00124             Close();
00125             iSocketListener->SocketDisconnected(this);
00126         } else {
00127             iSocketListener->SocketError(status, this);
00128         }
00129     } else {
00130         // hand the data over to the listener
00131         iSocketListener->Data(iRecvBuf, this);
00132 
00133         // receive some more
00134         Receive();
00135     }
00136 
00137     Log::Print(_L("RCobainSocket::RunL() exiting"));
00138 }
00139 

Generated on Tue Jan 13 15:47:07 2004 for CobainAPIImplementation by doxygen 1.3.5