plugins/networking/winsockprt/inc/wsp_socket.h
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // wsp_socket.h
       
     2 // 
       
     3 // Copyright (c) 2002 - 2010 Accenture. All rights reserved.
       
     4 // This component and the accompanying materials are made available
       
     5 // under the terms of the "Eclipse Public License v1.0"
       
     6 // which accompanies this distribution, and is available
       
     7 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 // 
       
     9 // Initial Contributors:
       
    10 // Accenture - Initial contribution
       
    11 //
       
    12 
       
    13 #ifndef __WSP_SOCKET_H__
       
    14 #define __WSP_SOCKET_H__
       
    15 
       
    16 #include <winsock2.h>
       
    17 #include "wsp_subsession.h"
       
    18 
       
    19 
       
    20 class RWin32Factory;
       
    21 class TInetAddr;
       
    22 
       
    23 class RWin32Socket : public RWin32SubSession
       
    24 	{
       
    25 public:
       
    26 	enum TType
       
    27 		{
       
    28 		EBlank,
       
    29 		ETcp,
       
    30 		EUdp
       
    31 		};
       
    32 public:
       
    33 	TInt Open(RWin32Factory& aFactory, TType aType);
       
    34 	void Close();
       
    35 	TInt Connect(const TInetAddr& aAddress, TRequestStatus& aStatus);
       
    36 	void CancelConnect();
       
    37 	TInt Send(const TDesC8& aSendBuffer, TRequestStatus& aStatus);
       
    38 	void CancelSend();
       
    39 	TInt SendTo(const TDesC8& aSendBuffer, const TInetAddr& aAddress, TRequestStatus& aStatus);
       
    40 	void CancelSendTo();
       
    41 	TInt Receive(TDes8& aReceiveBuffer, TRequestStatus& aStatus);
       
    42 	void CancelReceive();
       
    43 	TInt ReceiveFrom(TDes8& aReceiveBuffer, TInetAddr& aAddress, TRequestStatus& aStatus);
       
    44 	void CancelReceiveFrom();
       
    45 	TInt GetSocketName(TInetAddr& aAddress) const;
       
    46 	TInt GetPeerName(TInetAddr& aAddress) const;
       
    47 	TInt Bind(const TInetAddr& aAddress);
       
    48 	TInt Listen(TUint aQueueSize);
       
    49 	TInt Accept(RWin32Socket& aNewSocket, TRequestStatus& aStatus);
       
    50 	void CancelAccept();
       
    51 	TInt GetOption(TInt aLevel, TInt aName, char* aValue, TInt* aLength) const;
       
    52 	TInt SetOption(TInt aLevel, TInt aName, const char* aValue, TInt aLength);
       
    53 	};
       
    54 
       
    55 
       
    56 class CWin32Socket : public CWin32SubSession
       
    57 	{
       
    58 public:
       
    59 	enum TOppCode
       
    60 		{
       
    61 		EConnect,
       
    62 		ECancelConnect,
       
    63 		ESend,
       
    64 		ECancelSend,
       
    65 		ESendTo,
       
    66 		ECancelSendTo,
       
    67 		EReceive,
       
    68 		ECancelReceive,
       
    69 		EReceiveFrom,
       
    70 		ECancelReceiveFrom,
       
    71 		EGetSocketName,
       
    72 		EBind,
       
    73 		EGetPeerName,
       
    74 		EListen,
       
    75 		EAccept,
       
    76 		ECancelAccept,
       
    77 		EGetOption,
       
    78 		ESetOption
       
    79 		};
       
    80 public:
       
    81 	static CWin32Socket* NewL(CWin32Scheduler& aScheduler, RWin32Socket::TType aType);
       
    82 	virtual ~CWin32Socket();
       
    83 private: // From CWin32ActiveObject.
       
    84 	virtual void Run();
       
    85 private: // From CWin32SubSession.
       
    86 	virtual void ServiceL(TWin32Message& aMessage);
       
    87 private:
       
    88 	CWin32Socket(CWin32Scheduler& aScheduler, RWin32Socket::TType aType);
       
    89 	void ConstructL();
       
    90 	void Connect(TWin32Message& aMessage);
       
    91 	void Send(TWin32Message& aMessage);
       
    92 	void SendTo(TWin32Message& aMessage);
       
    93 	void Receive(TWin32Message& aMessage);
       
    94 	void ReceiveFrom(TWin32Message& aMessage);
       
    95 	void GetSocketName(TWin32Message& aMessage) const;
       
    96 	void Bind(TWin32Message& aMessage);
       
    97 	void GetPeerName(TWin32Message& aMessage) const;
       
    98 	void Listen(TWin32Message& aMessage);
       
    99 	void Accept(TWin32Message& aMessage);
       
   100 	void GetOption(TWin32Message& aMessage) const;
       
   101 	void SetOption(TWin32Message& aMessage);
       
   102 	void DoSend();
       
   103 	void CloseSocket();
       
   104 	void HandleConnectionComplete(TInt aError);
       
   105 	void HandleAcceptReady(TInt aError);
       
   106 	static void CALLBACK SendCompletion(DWORD dwError, DWORD cbTransferred, LPWSAOVERLAPPED lpOverlapped, DWORD dwFlags);
       
   107 	static void CALLBACK SendToCompletion(DWORD dwError, DWORD cbTransferred, LPWSAOVERLAPPED lpOverlapped, DWORD dwFlags);
       
   108 	static void CALLBACK ReceiveCompletion(DWORD dwError, DWORD cbTransferred, LPWSAOVERLAPPED lpOverlapped, DWORD dwFlags);
       
   109 	static void CALLBACK ReceiveFromCompletion(DWORD dwError, DWORD cbTransferred, LPWSAOVERLAPPED lpOverlapped, DWORD dwFlags);
       
   110 	void ConvertAddress(const TInetAddr& aESockAddress, SOCKADDR_IN& aWinSockAddress) const;
       
   111 	void ConvertAddress(const SOCKADDR_IN& aWinSockAddress, TInetAddr& aESockAddress) const;
       
   112 private:
       
   113 	RWin32Socket::TType iType;
       
   114 	SOCKET iSocket;
       
   115 	unsigned long iFlags;
       
   116 	WSABUF iSendBuffer;
       
   117 	WSABUF iReceiveBuffer;
       
   118 	WSAOVERLAPPED iSendOverlapped;
       
   119 	WSAOVERLAPPED iReceiveOverlapped;
       
   120 	SOCKADDR_IN iReceiveFromAddress;
       
   121 	TInt iReceiveFromAddressLength;
       
   122 	TInetAddr* iReceiveFromClientAddress;
       
   123 	TInt iBytesSent;
       
   124 	TWin32Message* iConnectMessage;
       
   125 	TWin32Message* iSendMessage;
       
   126 	TWin32Message* iReceiveMessage;
       
   127 	TWin32Message* iAcceptMessage;
       
   128 	CWin32Socket* iBlankAcceptSocket;
       
   129 	};
       
   130 
       
   131 
       
   132 class MWin32SocketObserver
       
   133 	{
       
   134 public:
       
   135 	enum TRequestType
       
   136 		{
       
   137 		ENone,
       
   138 		EConnect,
       
   139 		ESend,
       
   140 		ESendTo,
       
   141 		EReceive,
       
   142 		EReceiveFrom,
       
   143 		EAccept
       
   144 		};
       
   145 public:
       
   146 	virtual void HandleWin32SocketCompletion(TRequestType aRequestType, TInt aError) = 0;
       
   147 	};
       
   148 
       
   149 
       
   150 class CWin32SocketWrapper : public CActive
       
   151 	{
       
   152 public:
       
   153 	static CWin32SocketWrapper* NewL(MWin32SocketObserver& aObserver, RWin32Socket& aWin32Socket);
       
   154 	~CWin32SocketWrapper();
       
   155 	TInt Connect(const TInetAddr& aAddress);
       
   156 	TInt Send(const TDesC8& aSendBuffer);
       
   157 	TInt SendTo(const TDesC8& aSendBuffer, const TInetAddr& aAddress);
       
   158 	TInt Receive(TDes8& aReceiveBuffer);
       
   159 	TInt ReceiveFrom(TDes8& aReceiveBuffer, TInetAddr& aAddress);
       
   160 	TInt Accept(RWin32Socket& aNewSocket);
       
   161 private: // From CActive.
       
   162 	virtual void DoCancel();
       
   163 	virtual void RunL();
       
   164 	virtual TInt RunError(TInt aError);
       
   165 public:
       
   166 	CWin32SocketWrapper(MWin32SocketObserver& aObserver, RWin32Socket& aWin32Socket);
       
   167 	void ConstructL();
       
   168 private:
       
   169 	MWin32SocketObserver& iObserver;
       
   170 	RWin32Socket iWin32Socket; // Shallow copy to give each a unique TWin32Message object.
       
   171 	MWin32SocketObserver::TRequestType iRequestType;
       
   172 	};
       
   173 
       
   174 
       
   175 #endif // __WSP_SOCKET_H__