Automatic forward-decl fixup
[hiphop-php.git] / hphp / runtime / base / socket.h
blob8e7460afd56ab86c28401869845d390b8059e3b3
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
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>
24 #ifdef SOCKET_ERROR
25 # undef SOCKET_ERROR
26 #endif
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()); \
32 } \
34 namespace HPHP {
35 ///////////////////////////////////////////////////////////////////////////////
37 struct SocketData : FileData {
38 SocketData() { }
39 SocketData(int port, int type);
41 virtual bool closeImpl();
42 ~SocketData();
44 private:
45 friend struct Socket;
46 std::string m_address;
47 int m_port{0};
48 int m_type{-1};
49 int64_t m_bytesSent{0};
50 int m_error{0};
51 int m_timeout{0}; // in micro-seconds;
52 bool m_timedOut{false};
56 /**
57 * TCP/UDP sockets.
59 struct Socket : File {
60 DECLARE_RESOURCE_ALLOCATION(Socket);
62 Socket();
63 Socket(int sockfd, int type, const char *address = nullptr, int port = 0,
64 double timeout = 0, const StaticString& streamType = empty_string_ref);
65 virtual ~Socket();
67 // overriding ResourceData
68 const String& o_getClassNameHook() const override { return classnameof(); }
70 // implementing File
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;
75 bool eof() 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());
101 protected:
102 bool waitForData();
103 bool timedOut() const { return m_data->m_timedOut; }
105 Socket(std::shared_ptr<SocketData> data,
106 int sockfd,
107 int type,
108 const char *address = nullptr,
109 int port = 0,
110 double timeout = 0,
111 const StaticString& streamType = empty_string_ref);
113 // make private?
114 SocketData* getSocketData() { return m_data; }
115 const SocketData* getSocketData() const { return m_data; }
117 private:
118 void inferStreamType();
119 SocketData* m_data;
120 static __thread int s_lastErrno;
123 ///////////////////////////////////////////////////////////////////////////////
126 #endif // incl_HPHP_SOCKET_H_