1 // Module: Log4CPLUS
2 // File: socket.h
3 // Created: 4/2003
4 // Author: Tad E. Smith
5 //
6 //
7 // Copyright (C) Tad E. Smith All rights reserved.
8 //
9 // This software is published under the terms of the Apache Software
10 // License version 1.1, a copy of which has been included with this
11 // distribution in the LICENSE.APL file.
12 //
13
14 /** @file */
15
16 #ifndef LOG4CPLUS_HELPERS_SOCKET_HEADER_
17 #define LOG4CPLUS_HELPERS_SOCKET_HEADER_
18
19 #include <log4cplus/config.h>
20 #include <log4cplus/tstring.h>
21 #include <log4cplus/helpers/socketbuffer.h>
22 #if defined(_WIN32)
23 #include <winsock.h>
24 #endif
25
26 namespace log4cplus {
27 namespace helpers {
28
29 enum SocketState { ok,
30 not_opened,
31 bad_address,
32 connection_failed,
33 broken_pipe,
34 invalid_access_mode,
35 message_truncated
36 };
37
38 #if !defined(_WIN32)
39 typedef int SOCKET_TYPE;
40 #define INVALID_SOCKET -1
41 #else
42 typedef SOCKET SOCKET_TYPE;
43 #endif
44
45 class LOG4CPLUS_EXPORT AbstractSocket {
46 public:
47 // ctor and dtor
48 AbstractSocket();
49 AbstractSocket(SOCKET_TYPE sock, SocketState state, int err);
50 AbstractSocket(const AbstractSocket&);
51 virtual ~AbstractSocket() = 0;
52
53 // methods
54 /// Close socket
55 virtual void close();
56 virtual bool isOpen() const;
57
58 AbstractSocket& operator=(const AbstractSocket& rhs);
59
60 protected:
61 // Methods
62 virtual void copy(const AbstractSocket& rhs);
63
64 // Data
65 SOCKET_TYPE sock;
66 SocketState state;
67 int err;
68 };
69
70
71
72 /**
73 * This class implements client sockets (also called just "sockets").
74 * A socket is an endpoint for communication between two machines.
75 */
76 class LOG4CPLUS_EXPORT Socket : public AbstractSocket {
77 public:
78 // ctor and dtor
79 Socket();
80 Socket(SOCKET_TYPE sock, SocketState state, int err);
81 Socket(const tstring& address, int port);
82 virtual ~Socket();
83
84 // methods
85 virtual bool read(SocketBuffer& buffer);
86 virtual bool write(const SocketBuffer& buffer);
87 };
88
89
90
91 /**
92 * This class implements server sockets. A server socket waits for
93 * requests to come in over the network. It performs some operation
94 * based on that request, and then possibly returns a result to the
95 * requester.
96 */
97 class LOG4CPLUS_EXPORT ServerSocket : public AbstractSocket {
98 public:
99 // ctor and dtor
100 ServerSocket(int port);
101 virtual ~ServerSocket();
102
103 Socket accept();
104 };
105
106
107 LOG4CPLUS_EXPORT SOCKET_TYPE openSocket(unsigned short port, SocketState& state);
108 LOG4CPLUS_EXPORT SOCKET_TYPE connectSocket(const log4cplus::tstring& hostn,
109 unsigned short port, SocketState& state);
110 LOG4CPLUS_EXPORT SOCKET_TYPE acceptSocket(SOCKET_TYPE sock, SocketState& state);
111 LOG4CPLUS_EXPORT int closeSocket(SOCKET_TYPE sock);
112
113 LOG4CPLUS_EXPORT size_t read(SOCKET_TYPE sock, SocketBuffer& buffer);
114 LOG4CPLUS_EXPORT size_t write(SOCKET_TYPE sock, const SocketBuffer& buffer);
115
116 } // end namespace helpers
117 } // end namespace log4cplus
118
119 #endif // LOG4CPLUS_HELPERS_SOCKET_HEADER_
syntax highlighted by Code2HTML, v. 0.9.1