00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00033 #ifndef S3_SOCKET_TCP_H
00034 #define S3_SOCKET_TCP_H
00035
00036 #include <string>
00037 #include <iostream>
00038
00039 #include <s3fc/s3_macros.h>
00040
00041 #ifdef _WIN32
00042
00043 #define NOMINMAX
00044 #include <winsock.h>
00045
00046 typedef unsigned long in_addr_t;
00047 typedef unsigned int in_port_t;
00048 #else
00049 #include <sys/types.h>
00050 #include <sys/socket.h>
00051 #include <netinet/in.h>
00052 #include <arpa/inet.h>
00053 #include <errno.h>
00054 #include <unistd.h>
00055 #include <netdb.h>
00056 #endif
00057
00058 #include <s3fc/s3_semaphore.h>
00059 #include <s3fc/s3_exception.h>
00060
00061 class s3_socket_tcp
00062 {
00063 private:
00064 bool initialize();
00065 protected:
00066 bool set_reuseaddr;
00067 int sock, my_errno, my_h_errno;
00068 void set_errnos(int new_errno=0, int new_h_errno=0);
00069 void change_socket(int newsock);
00070 public:
00072 s3_socket_tcp(int newsock=INVALID_SOCKET, bool new_set_reuseaddr=true)
00073 throw(s3_generic_exception);
00074
00076 s3_socket_tcp(const s3_socket_tcp& template_sock)
00077 throw(s3_generic_exception);
00078
00079 virtual ~s3_socket_tcp();
00080
00082 bool bind(in_addr_t addr, in_port_t port);
00083
00085 bool listen(int backlog);
00086
00088 bool accept(s3_socket_tcp& newsock, sockaddr_in* clientname=0);
00089
00091 bool connect(const std::string& IP_address, in_port_t port);
00092
00094 bool close(bool reinit=true) throw(s3_generic_exception);
00095
00097 std::string get_error() const;
00098
00100 int get_errno() const
00101 {
00102 return my_errno;
00103 }
00104
00106 int get_h_errno() const
00107 {
00108 return my_h_errno;
00109 }
00110
00112 bool is_closed() const
00113 {
00114 return (sock < 0);
00115 }
00116
00118 bool is_valid() const
00119 {
00120 return (sock != INVALID_SOCKET);
00121 }
00122
00135 virtual bool read(void *data, int size, s3_semaphore *term=0);
00136
00151 virtual bool write(void *data, int size, int max_packet=-1,
00152 s3_semaphore *term=0);
00153
00164 int select_rd(int timeout=1);
00165
00176 int select_wr(int timeout=1);
00177
00178
00180 friend bool accept(s3_socket_tcp& newsock, sockaddr_in* clientname);
00181
00182
00183 };
00184
00185 #endif