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.18 2003/11/20 07:52:21 mattid Exp
00003  *
00004  * COBAIN - Communications API for EPOC Environments
00005  * Copyright (C) 2002 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     CCobainLayer::Log(_L("~RCobainSocket()"));
00041 
00042     // close the socket 
00043     Close(ETrue);
00044 
00045     // mark the listener null as it is not used any more
00046     iSocketListener = NULL;
00047 }
00048 
00049 EXPORT_C void RCobainSocket::SendL(const TDesC8 *aBuffer)
00050 {
00051     if( iIsClosed ) {
00052         User::Leave(KErrCobainSocketClosed);
00053     }
00054 
00055     TInt ret = iSession.Send(iId, aBuffer);
00056     if( (iSocketListener != NULL) && (ret != KErrNone) ) {
00057         iSocketListener->SocketError(ret, this);
00058     }
00059 }
00060 
00061 EXPORT_C void RCobainSocket::Close(TBool aImmediately)
00062 {
00063     if( iIsClosed ) {
00064         // already closed
00065         return;
00066     }
00067 
00068     CCobainLayer::Log(_L("RCobainSocket::CloseL()"));
00069 
00070     CCobainLayer::Log(_L("RCobainSocket::CloseL() calling server"));
00071 
00072     // close the network connection
00073     iSession.CloseSocket(iId, aImmediately);
00074 
00075     // stop receiving data
00076     Cancel();
00077 
00078     // mark the socket closed
00079     iIsClosed = ETrue;
00080 
00081     CCobainLayer::Log(_L("RCobainSocket::CloseL() exiting"));
00082 }
00083 
00084 EXPORT_C void RCobainSocket::SetSocketListener(MSocketListener *aSocketListener)
00085 {
00086     __ASSERT_ALWAYS(aSocketListener != NULL,
00087                     User::Panic(KCobainSocketPanic, KErrCobainSocketNullSocketListener));
00088 
00089     // cancel the existing request if any 
00090     Cancel();
00091 
00092     iSocketListener = aSocketListener;
00093 
00094     // start receiving data
00095     Receive();
00096 }
00097 
00098 // launches asynchronous call to receive data
00099 void RCobainSocket::Receive()
00100 {
00101     CCobainLayer::Log(_L("RCobainSocket::Receive()"));
00102 
00103     iSession.Receive(iId, &iRecvBuf, iStatus);
00104     SetActive();
00105 
00106     CCobainLayer::Log(_L("RCobainSocket::Receive() returning"));
00107 }
00108 
00110 // from CActive
00112 
00113 void RCobainSocket::DoCancel() 
00114 {
00115     CCobainLayer::Log(_L("RCobainSocket::DoCancel()"));
00116     //TRequestStatus *s = &iStatus;
00117     //User::RequestComplete(s, KErrCancel);
00118 }
00119 
00120 // data available
00121 void RCobainSocket::RunL() 
00122 {
00123     TBuf<64> logbuf;
00124 
00125     CCobainLayer::Log(_L("RCobainSocket::RunL()"));
00126 
00127     // assert there is a socket listener
00128     if( iSocketListener == NULL ) {
00129         CCobainLayer::Log(_L("iSocketListener == NULL!"));
00130         return;
00131     }
00132 
00133     TInt status = iStatus.Int();
00134     if( status != KErrNone ) {
00135         logbuf.Format(_L("RCobainSocket::RunL(): error (%d)!"), status);
00136         CCobainLayer::Log(logbuf);
00137         
00138         // notify the socket listener
00139         if( status == KErrDisconnected ) {
00140             // close this socket
00141             CCobainLayer::Log(_L("socket disconnected, calling Close().."));
00142 
00143             Close();
00144 
00145             iSocketListener->SocketDisconnected(this);
00146         } else {
00147             iSocketListener->SocketError(status, this);
00148         }
00149     } else {
00150         logbuf.Format(_L("%d bytes received"), iRecvBuf.Length());
00151         CCobainLayer::Log(logbuf);
00152 
00153         // hand the data over to the listener
00154         iSocketListener->Data(iRecvBuf, this);
00155 
00156         // receive some more
00157         Receive();
00158     }
00159 
00160     CCobainLayer::Log(_L("RCobainSocket::RunL() exiting"));
00161 }

Generated on Mon Dec 8 10:26:06 2003 for CobainAPIImplementation by doxygen 1.3.5