hti/PC_Tools/DataGateway/INC/Socket.h
branchRCL_3
changeset 59 8ad140f3dd41
parent 0 a03f92240627
equal deleted inserted replaced
49:7fdc9a71d314 59:8ad140f3dd41
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "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 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 * 
       
    14 * Description:
       
    15 *   This file contains headers of Socket, SocketClient, SocketServer and
       
    16 *   SocketSelect classes.
       
    17 */
       
    18 
       
    19 #ifndef __SOCKET_H__
       
    20 #define __SOCKET_H__
       
    21 
       
    22 #include <WinSock2.h>
       
    23 
       
    24 #include <string>
       
    25 
       
    26 
       
    27 typedef std::string String;
       
    28 
       
    29 //**********************************************************************************
       
    30 // Class Socket
       
    31 //
       
    32 // This class encapsulates Windows Sockets API Socket functionality
       
    33 //**********************************************************************************
       
    34 
       
    35 class Socket {
       
    36 public:
       
    37 	Socket(SOCKET s);
       
    38 	Socket();
       
    39 	virtual ~Socket();
       
    40 	Socket(const Socket&);
       
    41 	Socket& operator=(Socket&);
       
    42 
       
    43 	/*
       
    44 	 * This method is used to read characters from socket until line separator is encountered
       
    45 	 * Returns String including line separator
       
    46 	 */
       
    47 	String ReceiveLine();
       
    48 	/*
       
    49 	 * This method is used to read bytes from socket. The method reads all available and combines them to returned String object
       
    50 	 */
       
    51 	String ReceiveBytes();
       
    52 	/*
       
    53 	 * This method reads up to maximum amount of bytes from socket to the Data object given as parameter
       
    54 	 * The maximum amount is given as parameter
       
    55 	 */
       
    56 	int ReceiveBytes(BYTE *data, int bytes);
       
    57 
       
    58 	void   Close();
       
    59 
       
    60 	/*
       
    61 	 * This method is used to send String as a line
       
    62 	 * The parameter of SendLine is not a const reference
       
    63 	 * because SendLine modifes the String passed.
       
    64 	 */
       
    65 	void   SendLine (String);
       
    66 
       
    67 	/*
       
    68 	 * This method is used to send String as bytes
       
    69 	 * The parameter is a const reference
       
    70 	 * because the methos does not modify the String passed 
       
    71 	 * (in contrast to SendLine).
       
    72 	 */
       
    73 	void   SendBytes(const String&);
       
    74 	/*
       
    75 	 * This method is used to send specific amount of bytes of String
       
    76 	 * The parameter is a const reference
       
    77 	 * because the methos does not modify the String passed 
       
    78 	 * (in contrast to SendLine).
       
    79 	 */
       
    80 	int SendBytes(const BYTE* data, int bytes);
       
    81 
       
    82 protected:
       
    83 	friend class SocketServer;
       
    84 	friend class SocketSelect;
       
    85 	//winsock socket
       
    86 	SOCKET s_;
       
    87 
       
    88 	int* refCounter_;
       
    89 
       
    90 private:
       
    91 	static void Start();
       
    92 	static void End();
       
    93 	static int  nofSockets_;
       
    94 };
       
    95 
       
    96 //**********************************************************************************
       
    97 // Class SocketClient
       
    98 //
       
    99 //**********************************************************************************
       
   100 
       
   101 class SocketClient : public Socket {
       
   102 public:
       
   103   SocketClient(const String& host, int port);
       
   104 };
       
   105 
       
   106 //**********************************************************************************
       
   107 // Class SocketServer
       
   108 //
       
   109 // This class is used to:
       
   110 // -listen for incoming connections to a Socket
       
   111 // -connect to socket
       
   112 //**********************************************************************************
       
   113 
       
   114 class SocketServer// : public Socket 
       
   115 {
       
   116 public:
       
   117 	SocketServer();
       
   118 	/*
       
   119 	 * This method binds and opens the given socket to listening mode 
       
   120 	 */
       
   121 	void Accept(Socket*& s, int port, int connections, String& remoteHost);
       
   122 	/*
       
   123 	 * This method establishes a connection to the socket
       
   124 	 */
       
   125 	void Connect(Socket* s, const char* remoteHost, int remotePort);
       
   126 };
       
   127 
       
   128 //**********************************************************************************
       
   129 // Class SocketSelect
       
   130 //
       
   131 //**********************************************************************************
       
   132 
       
   133 class SocketSelect {
       
   134   public:
       
   135     SocketSelect(Socket const * const s1, Socket const * const s2);
       
   136 
       
   137     bool Readable(Socket const * const s);
       
   138 
       
   139   private:
       
   140     fd_set fds_;
       
   141 }; 
       
   142 
       
   143 
       
   144 #endif