EvalEmitDVArray: varray
[hiphop-php.git] / hphp / runtime / base / socket.h
blob84d3c1c7ba834c1f61dc0cd029c1750fd17108b8
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #ifndef incl_HPHP_SOCKET_H_
18 #define incl_HPHP_SOCKET_H_
20 #include "hphp/runtime/base/file.h"
22 #include <folly/portability/Sockets.h>
24 #include <sys/types.h>
26 #ifdef SOCKET_ERROR
27 # undef SOCKET_ERROR
28 #endif
29 #define SOCKET_ERROR(sock, msg, errn) \
30 sock->setError(errn); \
31 if (errn != EAGAIN && errn != EWOULDBLOCK && errn != EINPROGRESS) { \
32 raise_warning("%s [%d]: %s", msg, errn, \
33 folly::errnoStr(errn).c_str()); \
34 } \
36 namespace HPHP {
37 ///////////////////////////////////////////////////////////////////////////////
39 struct SocketData : FileData {
40 explicit SocketData(bool nonblocking) : FileData(nonblocking) { }
41 SocketData(int port, int type, bool nonblocking);
43 bool closeImpl() override;
44 ~SocketData() override;
46 private:
47 friend struct Socket;
48 std::string m_address;
49 int m_port{0};
50 int m_type{-1};
51 int64_t m_bytesSent{0};
52 int m_error{0};
53 int m_timeout{0}; // in micro-seconds;
54 bool m_timedOut{false};
58 /**
59 * TCP/UDP sockets.
61 struct Socket : File {
62 DECLARE_RESOURCE_ALLOCATION(Socket);
64 ~Socket() override;
66 // overriding ResourceData
67 const String& o_getClassNameHook() const override { return classnameof(); }
69 // implementing File
70 bool open(const String& filename, const String& mode) override;
71 bool close() override;
72 int64_t readImpl(char *buffer, int64_t length) override;
73 int64_t writeImpl(const char *buffer, int64_t length) override;
74 bool eof() override;
75 Array getMetaData() override;
76 int64_t tell() override;
78 // check if the socket is still open
79 virtual bool checkLiveness();
81 void setError(int err);
82 int getError() const { return m_data->m_error;}
83 static int getLastError() { return *s_lastErrno; }
84 static void clearLastError() {*s_lastErrno = 0; }
85 int getType() const { return m_data->m_type;}
87 // This is only for updating a local copy of timeouts set by setsockopt()
88 // outside of this class.
89 void internalSetTimeout(struct timeval &tv);
91 std::string getAddress() const { return m_data->m_address; }
92 int getPort() const { return m_data->m_port; }
94 std::shared_ptr<SocketData> getData() const {
95 return std::static_pointer_cast<SocketData>(File::getData());
98 protected:
99 bool waitForData();
100 bool timedOut() const { return m_data->m_timedOut; }
102 explicit Socket(bool nonblocking = true);
103 Socket(int sockfd, int type, const char *address = nullptr, int port = 0,
104 double timeout = 0, const StaticString& streamType = empty_string_ref,
105 bool nonblocking = true);
106 Socket(std::shared_ptr<SocketData> data,
107 int sockfd,
108 int type,
109 const char *address = nullptr,
110 int port = 0,
111 double timeout = 0,
112 const StaticString& streamType = empty_string_ref);
113 explicit Socket(std::shared_ptr<SocketData> data);
115 // make private?
116 SocketData* getSocketData() { return m_data; }
117 const SocketData* getSocketData() const { return m_data; }
119 private:
120 void inferStreamType();
121 SocketData* m_data;
122 static RDS_LOCAL(int, s_lastErrno);
125 // This class provides exactly the same functionality as Socket but reports as a
126 // class/resource of 'Socket' instead of 'stream'.
127 struct ConcreteSocket final : Socket {
128 CLASSNAME_IS("Socket");
129 RESOURCENAME_IS("Socket");
131 explicit ConcreteSocket(bool nonblocking = true)
132 : Socket(nonblocking)
134 ConcreteSocket(int sockfd, int type, const char *address = nullptr,
135 int port = 0, double timeout = 0,
136 const StaticString& streamType = empty_string_ref,
137 bool nonblocking = true)
138 : Socket(sockfd, type, address, port, timeout, streamType, nonblocking)
140 explicit ConcreteSocket(std::shared_ptr<SocketData> data) : Socket(data) { }
142 // overriding ResourceData
143 const String& o_getClassNameHook() const override { return classnameof(); }
144 const String& o_getResourceName() const override { return resourcenameof(); }
147 // This class provides exactly the same functionality as ConcreteSocket but
148 // reports the default behavior for File.
149 struct StreamSocket final : Socket {
150 explicit StreamSocket(bool nonblocking = true)
151 : Socket(nonblocking)
153 StreamSocket(int sockfd, int type, const char *address = nullptr,
154 int port = 0, double timeout = 0,
155 const StaticString& streamType = empty_string_ref,
156 bool nonblocking = true)
157 : Socket(sockfd, type, address, port, timeout, streamType, nonblocking)
159 explicit StreamSocket(std::shared_ptr<SocketData> data) : Socket(data) { }
162 ///////////////////////////////////////////////////////////////////////////////
165 #endif // incl_HPHP_SOCKET_H_