00001 #ifndef RXSOCKET_HH_ 00002 # define RXSOCKET_HH_ 00003 00004 # include <winsock2.h> 00005 # include <windows.h> 00006 # include <map> 00007 # include <iostream> 00008 # include <sstream> 00009 00010 # define RXSOCKET_WND "RxSocket" 00011 # define RXSOCKET_EVENTMSG 0x7FFF 00012 # define RXSOCKET_EVENTS (FD_READ | FD_WRITE | FD_ACCEPT | FD_CONNECT | FD_CLOSE) 00013 # define RXSOCKET_BUFFER_SIZE 1024 00014 00015 namespace rxsocket 00016 { 00017 class RxSocketInit 00018 { 00019 public: 00020 enum Status 00021 { 00022 status_uninitialized, 00023 status_ok 00024 }; 00025 00026 public: 00027 RxSocketInit(); 00028 ~RxSocketInit(); 00029 00030 private: 00031 Status _status; 00032 WSADATA _wsa_data; 00033 Status init(); 00034 }; 00035 00036 class RxSocket 00037 { 00038 public: 00039 ~RxSocket(); 00040 00041 virtual void process_event(int code, int err); 00042 virtual void open() = 0; 00043 virtual void close(); 00044 00045 virtual int data_available() = 0; 00046 virtual bool is_connected(); 00047 void print(std::ostream &os); 00048 00049 static void set_instance(HINSTANCE instance); 00050 protected: 00051 RxSocket(); 00052 00053 protected: 00054 HWND _wnd; 00055 SOCKET _socket; 00056 00057 virtual void event_connect(); 00058 virtual void event_close(); 00059 virtual void can_accept(); 00060 virtual void can_write() = 0; 00061 virtual void can_read() = 0; 00062 00063 public: 00064 }; 00065 00066 class RxTCPClient : public RxSocket 00067 { 00068 public: 00069 RxTCPClient(const std::string &host, u_short port, 00070 int buffer_size = RXSOCKET_BUFFER_SIZE); 00071 00072 virtual void open(); 00073 virtual int data_available() const; 00074 virtual bool can_send() const; 00075 virtual void send(const std::string &str); 00076 virtual void receive(std::string &str, int size); 00077 00078 virtual const std::string &get_send_buf() const; 00079 virtual const std::string &get_receive_buf() const; 00080 00081 protected: 00082 std::string _host; 00083 u_short _port; 00084 std::string _send_buf; 00085 std::string _receive_buf; 00086 bool _can_send; 00087 int _buffer_size; 00088 00089 void send_now(); 00090 void def_can_write(); 00091 void def_can_read(); 00092 virtual void can_write(); 00093 virtual void can_read(); 00094 }; 00095 } 00096 00097 #endif