00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
00043 Close(ETrue);
00044
00045
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
00065 return;
00066 }
00067
00068 CCobainLayer::Log(_L("RCobainSocket::CloseL()"));
00069
00070 CCobainLayer::Log(_L("RCobainSocket::CloseL() calling server"));
00071
00072
00073 iSession.CloseSocket(iId, aImmediately);
00074
00075
00076 Cancel();
00077
00078
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
00090 Cancel();
00091
00092 iSocketListener = aSocketListener;
00093
00094
00095 Receive();
00096 }
00097
00098
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
00112
00113 void RCobainSocket::DoCancel()
00114 {
00115 CCobainLayer::Log(_L("RCobainSocket::DoCancel()"));
00116
00117
00118 }
00119
00120
00121 void RCobainSocket::RunL()
00122 {
00123 TBuf<64> logbuf;
00124
00125 CCobainLayer::Log(_L("RCobainSocket::RunL()"));
00126
00127
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
00139 if( status == KErrDisconnected ) {
00140
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
00154 iSocketListener->Data(iRecvBuf, this);
00155
00156
00157 Receive();
00158 }
00159
00160 CCobainLayer::Log(_L("RCobainSocket::RunL() exiting"));
00161 }