socket.h

00001 /*
00002  * socket.h
00003  * This file is part of dbPager Classes Library (DCL)
00004  *
00005  * Copyright (c) 2008 Dennis Prochko <wolfsoft@mail.ru>
00006  *
00007  * DCL is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation version 3.
00010  *
00011  * DCL is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with DCL; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor,
00019  * Boston, MA  02110-1301  USA
00020  */
00021 
00022 #ifndef _SOCKET_H_
00023 #define _SOCKET_H_
00024 
00025 #include <vector>
00026 #include <string>
00027 
00028 #include <dcl/exception.h>
00029 
00030 namespace dbp {
00031 
00033 
00037 class socket_exception: public exception {
00038 public:
00040         socket_exception(const std::string &msg = "") throw(): exception(msg) { }
00041 };
00042 
00044 class socket_address: public std::vector<std::string> {
00045 public:
00046         int port;
00047         std::string host;
00049         socket_address(): port(0), host("") {};
00051 
00054         socket_address(const std::string &addr) { parse(addr); };
00056 
00060         socket_address(const std::string &address, int port): port(port),
00061           host(address) { };
00063 
00066         void parse(const std::string &addr);
00067 };
00068 
00070 
00073 class socket {
00074 public:
00075         typedef enum {
00076                 data_not_ready = -1,
00077                 io_error = -2
00078         } error;
00080         socket();
00082         socket(const socket &src) {
00083                 socket_fd = src.socket_fd;
00084                 _port = src._port;
00085                 _address = src._address;
00086                 const_cast<socket&>(src).socket_fd = -1;
00087         }
00089         socket& operator=(const socket &src) {
00090                 socket_fd = src.socket_fd;
00091                 _port = src._port;
00092                 _address = src._address;
00093                 const_cast<socket&>(src).socket_fd = -1;
00094                 return *this;
00095         }
00097         virtual ~socket();
00099         virtual int handle() const {
00100                 return socket_fd;
00101         }
00103 
00106         bool connect(const std::string &address, int port);
00108 
00111         void bind(const socket_address &address);
00113 
00117         void listen();
00119 
00125         socket accept();
00127 
00133         virtual int read(int bytes_to_read, char *buffer);
00135 
00141         virtual int write(int bytes_to_write, const char *buffer);
00143 
00146         virtual void shutdown();
00148 
00153         const std::string& address() const {
00154                 return _address;
00155         }
00157 
00162         int port() const {
00163                 return _port;
00164         }
00165 protected:
00166         int socket_fd;
00167         std::string _address;
00168         int _port;
00169 private:
00170         void set_blocked(bool state);
00171 };
00172 
00173 class tcp_socket: public socket {
00174 public:
00176         tcp_socket();
00177 };
00178 
00179 class udp_socket: public socket {
00180 public:
00182         udp_socket();
00183 };
00184 
00185 class unix_socket: public socket {
00186 public:
00188         unix_socket();
00189 };
00190 
00191 }
00192 
00193 #endif /*_SOCKET_H_*/

 
Support This Project
SourceForge.net Logo