⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 csocket.h

📁 symbina上可以使用一个xml解析器,对开发网络应用很有好处
💻 H
字号:
/* External socket implementation using Symbian sockets.
** Copyright (C) 2004-2005 Darrell Karbott (djk2005@users.sf.net)
** This code is free software; you can redistribute it and/or
** modify it under the terms of GNU Lesser General Public License.
** See http://www.gnu.org/ for further details of the LGPL.
**/

/*
** This class provides a simplified asynchronous interface to
** TCP/IP sockets.
**
** OVERVIEW:
** Implement MSocketObserver in your client code. 
** Use CSocket::NewL() to create a new socket instance.
** Call CSocket::SetObserver() with a pointer to your MSocketObserver
** implementation.
** Start connecting by calling CSocket::ConnectL().
** Wait for MSocketObserver::SocketNotify() to be called with
** a reason code of EConnected.
** Call CSocket::EnableRead() when the client code is 
** ready to start receiving data from the socket.  The
** MSocketNotifier::SocketNotify callback will be called
** with reason == ERead when data arrives.  
** Use CSocket::RecvdData() to access the data.
** Write data to the socket using CSocket::Write().  
** Call CSocket::Close() when you are done with the socket.
**
** HINTS:
** Make sure SocketNotify() calls CSocket::EnableRead().  
** No ERead notifications will be posted until after this call.
** Typically client code calls CSocket::EnableRead() when 
** MSocketObserver is called with reason == EConnected.
**
** Your MSocketNotify::SocketNotify() implementation 
** *must* be re-entrant.
**
** Always check pSocket in MSocketObserver::SocketNotify().
** it can be NULL for notifications posted from the CSocket
** destructor.
**
** Always check BytesWriteable() before calling Write().
**
** SendLen(), BytesBuffered(), BytesWriteable() and LastError()
** are only valid from within the scope of MSocketNotifier::SocketNotify()
** callback.
**
** The same is true of RecvdData(), unless ConnectL() is called with
** readAutoRestart == EFalse. 
*/

#ifndef __CSOCKET_H__
#define __CSOCKET_H__ 1

#include <e32std.h>
#include <e32base.h>
#include <es_sock.h>

class CSocket;
class CNotifyOnDelete;
class CSocketSetup;
class CSocketTx;
class CSocketRx;

/*
** Abstract class for receiving notifications from a
** CSocket instance.
*/
class MSocketObserver {
 public:
  enum TSocketReason {
    EResolving,  // Resolving the host name. 
    EConnecting, // Connecting to host.
    EConnected,  // Connected to host.
    ERead,       // Data is available for read.
    EWrite,      // Data has been written through to the OS socket layer.
    EError,      // An error occurred.
    EClosed,     // The CSocket was closed.
  };

  // Note: pSocket == NULL when this is called from the CSocket destuctor.
  virtual void SocketNotify(CSocket* pSocket, const TSocketReason& reason) = 0;
  IMPORT_C virtual ~MSocketObserver();
};

class CSocket : public CBase {
 public:
  IMPORT_C static CSocket* NewL();

  IMPORT_C ~CSocket(); 
  
  /*
  ** Enables notifications to the MSocketObserver, pObserver.
  ** Calling this with pObserver == NULL disables notifications.
  */
  IMPORT_C void SetObserver(MSocketObserver* pObserver);

  /*
  ** Close the socket.
  ** 
  ** Returns: ETrue if the socket instance was deleted from
  **          the MSocketObserver::SocketNotify() callback,
  **          EFalse otherwise.
  **
  ** Note: This function can be called at any time.
  ** Note: Iff ConnectL() was called since the last
  **       call to Close(), this function is guaranteed
  **       to cause MSocketObserver::SocketNotify() to
  **       be called with reason == EClosed;
  */
  IMPORT_C TBool Close();

  enum TState { EClosed,     // quiescent
                EResolving,  // resolving the hostname
                EConnecting, // connecting to host
                EConnected,  // connection established
                EError,      // an error has occurred, the client should call Close.
  };

  /*
  ** Returns: The current socket state.
  **
  ** Note: This function can be called at any time.
  */
  IMPORT_C TState State() const;

  /*
  ** Start connecting the socket.
  **
  ** If readAutoRestart is ETrue the CSocket automatically restarts 
  ** reading after the MSocketObserver is called
  ** with reason == ERead,  and RecvdData() is only valid from
  ** within the callback.  If it is EFalse, RecvdData() remains
  ** valid until the next call to EnableRead(). EnableRead() should
  ** be called "soon" after the ERead notification.
  **
  ** Returns: ETrue if the the instance was deleted from within the
  **          the MSocketObserver callback, ETrue otherwise.
  */
  IMPORT_C TBool ConnectL(const TDesC& host, TInt port, TBool readAutoRestart = ETrue);

  /*
  ** Read and write accessors.
  **
  ** With the exception of RecvdData(), these values are only 
  ** gauranteed to be valid from within the scope of the 
  ** MSocketObserver::SocketNotify callback.
  */

  // Data from the last read. Can be empty.
  IMPORT_C const TDesC8& RecvdData() const;

  // Bytes sent by last write.
  IMPORT_C TInt SendLen() const;

  // Bytes buffered for write.
  IMPORT_C TInt BytesBuffered() const;

  // The maximum number of bytes that can be written.
  IMPORT_C TInt BytesWriteable() const;

  // Set when the observer is called with reason == EError
  IMPORT_C TInt LastError() const;

  /*
  ** The following functions can only be called when 
  ** CSocket::GetState() == EConnected
  */

  /*
  ** Start / Stop reading. 
  ** Reading is disabled by default.  Call EnableRead(ETrue) to start
  ** receiving ERead notifications. 
  ** Returns: ETrue if the CSocket instance was deleted from within the
  **          MSocketNotifier callback, EFalse otherwise.
  ** 
  ** Note: If ConnectL was called with autoReadRestart == ETrue,
  **       reading restarts automatically.  Otherwise EnableRead()
  **       must be called after every ERead notifcation.
  **       
  ** Note: You may lose data if you stop and restart.
  */
  IMPORT_C TBool EnableRead(TBool enabled = ETrue); 

  /*
  ** Asynchronously write data to the socket.
  **
  ** Requires: data.Length() <= CSocket::BytesWriteable().
  **
  */
  IMPORT_C void Write(const TDesC8& data);

 protected:
  void ConstructL();

  RSocket* Socket();
  RSocketServ* SocketServ();

  // Calls out to the observer.
  // Returns: ETrue if the socket instance was deleted.
  TBool Notify(const MSocketObserver::TSocketReason& r);
  
  // Calls out to the observer
  void HandleError(TInt error);

  void DeleteDelegates();
  void SetState(const TState& state);

  CSocket(); 
  
 private:
  CNotifyOnDelete* iDeletionNotifier;
  MSocketObserver* iObserver;
  RSocket iSocket;
  RSocketServ iSocketServ;

  CSocketSetup* iSetupAO;
  CSocketTx* iTxAO;
  CSocketRx* iRxAO;

  TState  iState;
  TInt iLastError;
  TBool iIsDeleting;

  friend class CSocketSetup;
  friend class CSocketTx;
  friend class CSocketRx;

 public:
  
  enum { 
    INVFAILED = 1, // An invariant was violated. i.e. assertion failure.
  };
};

////////////////////////////////////////////////////////////
// Panics emitted by the CSocket implementation
////////////////////////////////////////////////////////////

_LIT(KCSocketPanicCategory, "CSocket");


#endif



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -