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

📄 socketsengine.h

📁 Symbian 手机网络通信程序 sockets-vc
💻 H
字号:
/* Copyright (c) 2004, Nokia. All rights reserved */


#ifndef __SOCKETSENGINE_H__
#define __SOCKETSENGINE_H__

// INCLUDES
#include <in_sock.h>

#include "TimeOutNotifier.h"
#include "EngineNotifier.h"
#include "Sockets.hrh"

// CONSTANTS
_LIT( KDefaultServerName, "127.0.0.1" );
_LIT( KStrNewLine, "\r\n" );

// String length for localized messages shown in non-leaving functions
const TInt KMaxMessageLength = 256;  

// FORWARD DECLARATIONS
class CSocketsReader;
class CSocketsWriter;
class CTimeOutTimer;
class MUINotifier;

// CLASS DECLARATION
/**
* CSocketsEngine
*  This class is the main engine part of the sockets application.
*  It establishes a TCP connection using its server name and port number
*  (performing a DNS lookup operation first, if appropriate).
*  It creates instances of separate active objects to perform reading from,
*  and writing to, the socket.
*/
class CSocketsEngine : public CActive,
                       public MTimeOutNotifier,
                       public MEngineNotifier
    {
    public: // Constructors and destructors

        /**
        * NewL.
        * Two-phased constructor.
        * Creates a CSocketsEngine object using two phase construction,
        * and returns a pointer to the created object.
        * @param aConsole Console to use for ui output.
        * @return A pointer to the created instance of CSocketsEngine.
        */
        static CSocketsEngine* NewL( MUINotifier& aConsole );

        /**
        * NewLC.
        * Two-phased constructor.
        * Creates a CSocketsEngine object using two phase construction,
        * and returns a pointer to the created object.
        * @param aConsole Console to use for ui output.
        * @return A pointer to the created instance of CSocketsEngine.
        */
        static CSocketsEngine* NewLC( MUINotifier& aConsole );

        /**
        * ~CSocketsEngine.
        * Destructor.
        * Destroys the object and release all memory objects.
        */
        virtual ~CSocketsEngine();

    public: // New functions

        /**
        * ConnectL.
        * Initiates connection of socket, using iServerName and iPort.
        */
        void ConnectL();

        /**
        * Disconnect.
        * Disconnects socket.
        */
        void Disconnect();

        /**
        * WriteL.
        * Writes data to socket.
        * @param aData Data to be written.
        */
        void WriteL( const TDesC8& aData );

        /**
        * Read.
        * Initiates read of data from socket.
        */
        void Read();

        /**
        * SetServerName.
        * Sets name of server to connect to.
        * @param aName New server name.
        */
        void SetServerName( const TDesC& aName );

        /**
        * ServerName.
        * Gets server name.
        * @return Name of server.
        */
        const TDesC& ServerName() const;

        /**
        * SetPort.
        * Sets port number to connect to.
        * @param aPort New port number.
        */
        void SetPort( TInt aPort );

        /**
        * Port.
        * Gets port number.
        * @return Port number.
        */
        TInt Port() const;

        /**
        * Connected.
        * Checks if socket is fully connected.
        * @return ETrue if socket is connected.
        */
        TBool Connected() const;

    public: // Functions from base classes

        /**
        * From MTimeOutNotifier, TimerExpired.
        * The function to be called when a timeout occurs.
        */
        void TimerExpired();

        /**
        * From MEngineNotifier, ReportError.
        * Report a communication error.
        * @param aErrorType Error type.
        * @param aErrorCode Associated error code.
        */
        void ReportError( MEngineNotifier::TErrorType aErrorType,
                          TInt aErrorCode );

        /**
        * From MEngineNotifier, ResponseReceived.
        * Data has been received on the socket and read into a buffer.
        * @param aBuffer The data buffer.
        */
        void ResponseReceived( const TDesC8& aBuffer );

    protected: // Functions from base classes

        /**
        * From CActive, DoCancel.
        * Cancels any outstanding operation.
        */
        void DoCancel();

        /**
        * From CActive, RunL.
        * Called when operation completes.
        */
        void RunL();

    private: // Constructors and destructors

        /**
        * CSocketsEngine.
        * C++ default constructor.
        * Performs the first phase of two phase construction.
        * @param aConsole The console to use for ui output.
        */
        CSocketsEngine( MUINotifier& aConsole );

        /**
        * ConstructL.
        * 2nd phase constructor.
        */
        void ConstructL();

    private: // Enumerations

        /**
        * TSocketsEngineState.
        * Tracks the state of this object through the connection process.
        *   - ENotConnected     The initial ( idle ) state.
        *   - EConnecting       A connect request is pending with the
        *                       socket server.
        *   - EConnected        A connection has been established.
        *   - ELookingUp        A DNS lookup request is pending with the
        *                       socket server.
        */
        enum TSocketsEngineState
            {
            ENotConnected,
            EConnecting,
            EConnected,
            ELookingUp
            };

    private: // New functions

        /**
        * ConnectL.
        * Initiates a connect operation on a socket.
        * @param aAddr The ip address to connect to.
        */
        void ConnectL( TUint32 aAddr );

        /**
        * ChangeStatus.
        * Handles a change in this object's status.
        * @param aNewStatus New status.
        */
        void ChangeStatus( TSocketsEngineState aNewStatus );

        /**
        * Print.
        * Displays text on the console.
        * @param aDes Text to display.
        */
        void Print( const TDesC& aDes );

    private: // Constants

        /**
        * KTimeOut, the maximum time allowed for a lookup or connect
        *           requests to complete.
        */
        static const TInt KTimeOut;

        /**
        * KDefaultPortNumber, the initial port number displayed to the user.
        */
        static const TInt KDefaultPortNumber;

    private: // Data

        /**
        * iEngineStatus, this object's current status.
        */
        TSocketsEngineState iEngineStatus;

        /**
        * iConsole, console for displaying text etc.
        */
        MUINotifier& iConsole;

        /**
        * iSocket, the actual socket.
        */
        RSocket iSocket;

        /**
        * iSocketsReader, active object to control reads from the socket.
        * Owned by CSocketsEngine object.
        */
        CSocketsReader* iSocketsReader;

        /**
        * iSocketsWriter, active object to control writes to the socket.
        * Owned by CSocketsEngine object.
        */
        CSocketsWriter* iSocketsWriter;

        /**
        * iSocketServ, the socket server.
        */
        RSocketServ iSocketServ;

        /**
        * iResolver, DNS name resolver.
        */
        RHostResolver iResolver;

        /**
        * iNameEntry, the result from the name resolver.
        */
        TNameEntry iNameEntry;

        /**
        * iNameRecord, the name record found by the resolver.
        */
        TNameRecord iNameRecord;

        /**
        * iTimer, timer active object.
        * Owned by CSocketsEngine object.
        */
        CTimeOutTimer* iTimer;

        /**
        * iAddress, the address to be used in the connection.
        */
        TInetAddr iAddress;

        /**
        * iPort, the port number to connect to.
        */
        TInt iPort;

        /**
        * iServerName, the server name to connect to.
        */
        TBuf<KMaxServerNameLength> iServerName;
    };

#endif // __SOCKETSENGINE_H__

// End of File

⌨️ 快捷键说明

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