1 // ============================================================== 2 // This file is part of Glest Shared Library (www.glest.org) 3 // 4 // Copyright (C) 2001-2008 Marti�o Figueroa 5 // 6 // You can redistribute this code and/or modify it under 7 // the terms of the GNU General Public License as published 8 // by the Free Software Foundation; either version 2 of the 9 // License, or (at your option) any later version 10 // ============================================================== 11 12 #ifndef _SHARED_PLATFORM_SOCKET_H_ 13 #define _SHARED_PLATFORM_SOCKET_H_ 14 15 #include <string> 16 #include <winsock.h> 17 18 using std::string; 19 20 namespace Shared{ namespace Platform{ 21 22 // ===================================================== 23 // class IP 24 // ===================================================== 25 26 class Ip{ 27 private: 28 unsigned char bytes[4]; 29 30 public: 31 Ip(); 32 Ip(unsigned char byte0, unsigned char byte1, unsigned char byte2, unsigned char byte3); 33 Ip(const string& ipString); 34 getByte(int byteIndex)35 unsigned char getByte(int byteIndex) {return bytes[byteIndex];} 36 string getString() const; 37 }; 38 39 // ===================================================== 40 // class Socket 41 // ===================================================== 42 43 class Socket{ 44 private: 45 class SocketManager{ 46 public: 47 SocketManager(); 48 ~SocketManager(); 49 }; 50 51 protected: 52 static SocketManager socketManager; 53 SOCKET sock; 54 55 public: 56 Socket(SOCKET sock); 57 Socket(); 58 ~Socket(); 59 60 int getDataToRead(); 61 int send(const void *data, int dataSize); 62 int receive(void *data, int dataSize); 63 int peek(void *data, int dataSize); 64 65 void setBlock(bool block); 66 bool isReadable(); 67 bool isWritable(); 68 bool isConnected(); 69 70 string getHostName() const; 71 string getIp() const; 72 73 protected: 74 static void throwException(const string &str); 75 }; 76 77 // ===================================================== 78 // class ClientSocket 79 // ===================================================== 80 81 class ClientSocket: public Socket{ 82 public: 83 void connect(const Ip &ip, int port); 84 }; 85 86 // ===================================================== 87 // class ServerSocket 88 // ===================================================== 89 90 class ServerSocket: public Socket{ 91 public: 92 void bind(int port); 93 void listen(int connectionQueueSize= SOMAXCONN); 94 Socket *accept(); 95 }; 96 97 }}//end namespace 98 99 #endif 100