Pol  Revision:3cfda13
wnsckt.h
Go to the documentation of this file.
1 #ifndef CLIB_WNSCKT_H
2 #define CLIB_WNSCKT_H
3 
4 #include <string>
5 
6 #include "Header_Windows.h"
7 #ifndef WINDOWS
8 #include <sys/socket.h>
9 
10 typedef int SOCKET;
11 #ifndef INVALID_SOCKET
12 #define INVALID_SOCKET ( SOCKET )( -1 )
13 #endif
14 #endif
15 
16 namespace Pol
17 {
18 namespace Clib
19 {
20 class Socket
21 {
22 public:
23  enum option
24  {
25  none = 0,
28  };
29 
30  Socket();
31  explicit Socket( SOCKET sock );
32  Socket( Socket& sck ); // takes ownership
33  virtual ~Socket();
34 
35  void write( const std::string& str );
36 
37  bool open( const char* ipaddr, unsigned short port );
38  bool listen( unsigned short port );
39  bool select( unsigned int seconds, unsigned int useconds );
40  bool accept( SOCKET* s, unsigned int mstimeout );
41  bool accept( Socket& newsocket );
42  bool recvbyte( unsigned char* byte, unsigned int waitms );
43  bool recvdata( void* vdest, unsigned len, unsigned int waitms );
44  unsigned peek( void* vdest, unsigned len, unsigned int waitms );
45  void send( const void* data, unsigned length );
46  bool send_nowait( const void* vdata, unsigned datalen, unsigned* nsent );
47  bool connected() const;
48  void close();
49 
50  bool is_local() const;
51 
52  std::string getpeername() const;
53  struct sockaddr peer_address() const;
54 
55  SOCKET handle() const;
57 
58  void setsocket( SOCKET sck );
59  void setpeer( struct sockaddr peer );
60  void takesocket( Socket& sck );
61 
62  void set_options( option opt );
63 
64  void disable_nagle();
65 
66 protected:
67  void apply_socket_options( SOCKET sck );
69 
70 private:
71  void HandleError();
72 
74 
75  int _options;
76  struct sockaddr _peer;
77 };
78 }
79 }
80 #endif // CLIB_WNSCKT_H
void write(const std::string &str)
Definition: wnsckt.cpp:617
virtual ~Socket()
Definition: wnsckt.cpp:71
bool recvdata(void *vdest, unsigned len, unsigned int waitms)
Definition: wnsckt.cpp:417
int SOCKET
Definition: wnsckt.h:10
bool connected() const
Definition: wnsckt.cpp:308
SOCKET handle() const
Definition: wnsckt.cpp:112
void apply_prebind_socket_options(SOCKET sck)
Definition: wnsckt.cpp:199
void disable_nagle()
Definition: wnsckt.cpp:164
bool listen(unsigned short port)
Definition: wnsckt.cpp:216
std::string getpeername() const
Definition: wnsckt.cpp:92
bool open(const char *ipaddr, unsigned short port)
Definition: wnsckt.cpp:125
bool recvbyte(unsigned char *byte, unsigned int waitms)
Definition: wnsckt.cpp:354
void HandleError()
Definition: wnsckt.cpp:314
SOCKET _sck
Definition: wnsckt.h:73
unsigned peek(void *vdest, unsigned len, unsigned int waitms)
Definition: wnsckt.cpp:493
bool send_nowait(const void *vdata, unsigned datalen, unsigned *nsent)
Definition: wnsckt.cpp:583
bool accept(SOCKET *s, unsigned int mstimeout)
Definition: wnsckt.cpp:275
void setsocket(SOCKET sck)
Definition: wnsckt.cpp:76
std::unordered_map< u64, ScriptDiffData > data
Definition: osmod.cpp:966
bool is_local() const
Definition: wnsckt.cpp:638
void takesocket(Socket &sck)
struct sockaddr peer_address() const
Definition: wnsckt.cpp:107
struct sockaddr _peer
Definition: wnsckt.h:76
SOCKET release_handle()
Definition: wnsckt.cpp:117
void setpeer(struct sockaddr peer)
Definition: wnsckt.cpp:87
bool select(unsigned int seconds, unsigned int useconds)
Definition: wnsckt.cpp:250
void apply_socket_options(SOCKET sck)
Definition: wnsckt.cpp:179
void set_options(option opt)
Definition: wnsckt.cpp:82
Definition: berror.cpp:12
void send(const void *data, unsigned length)
Definition: wnsckt.cpp:553