2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-2016 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"
21 #include <sys/types.h>
22 #include <sys/socket.h>
27 #define SOCKET_ERROR(sock, msg, errn) \
28 sock->setError(errn); \
29 if (errn != EAGAIN && errn != EWOULDBLOCK && errn != EINPROGRESS) { \
30 raise_warning("%s [%d]: %s", msg, errn, \
31 folly::errnoStr(errn).c_str()); \
35 ///////////////////////////////////////////////////////////////////////////////
37 struct SocketData
: FileData
{
39 SocketData(int port
, int type
);
41 virtual bool closeImpl();
46 std::string m_address
;
49 int64_t m_bytesSent
{0};
51 int m_timeout
{0}; // in micro-seconds;
52 bool m_timedOut
{false};
59 struct Socket
: File
{
60 DECLARE_RESOURCE_ALLOCATION(Socket
);
63 Socket(int sockfd
, int type
, const char *address
= nullptr, int port
= 0,
64 double timeout
= 0, const StaticString
& streamType
= empty_string_ref
);
67 // overriding ResourceData
68 const String
& o_getClassNameHook() const override
{ return classnameof(); }
71 bool open(const String
& filename
, const String
& mode
) override
;
72 bool close() override
;
73 int64_t readImpl(char *buffer
, int64_t length
) override
;
74 int64_t writeImpl(const char *buffer
, int64_t length
) override
;
76 Array
getMetaData() override
;
77 int64_t tell() override
;
79 // check if the socket is still open
80 virtual bool checkLiveness();
82 void setError(int err
);
83 int getError() const { return m_data
->m_error
;}
84 static int getLastError() { return s_lastErrno
; }
85 static void clearLastError() { s_lastErrno
= 0; }
86 int getType() const { return m_data
->m_type
;}
88 // This is only for updating a local copy of timeouts set by setsockopt()
89 // outside of this class.
90 void setTimeout(struct timeval
&tv
);
92 bool setBlocking(bool blocking
);
94 std::string
getAddress() const { return m_data
->m_address
; }
95 int getPort() const { return m_data
->m_port
; }
97 explicit Socket(std::shared_ptr
<SocketData
> data
);
98 std::shared_ptr
<SocketData
> getData() const {
99 return std::static_pointer_cast
<SocketData
>(File::getData());
103 bool timedOut() const { return m_data
->m_timedOut
; }
105 Socket(std::shared_ptr
<SocketData
> data
,
108 const char *address
= nullptr,
111 const StaticString
& streamType
= empty_string_ref
);
114 SocketData
* getSocketData() { return m_data
; }
115 const SocketData
* getSocketData() const { return m_data
; }
118 void inferStreamType();
120 static __thread
int s_lastErrno
;
123 ///////////////////////////////////////////////////////////////////////////////
126 #endif // incl_HPHP_SOCKET_H_