Mitsuba Renderer  0.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sstream.h
Go to the documentation of this file.
1 /*
2  This file is part of Mitsuba, a physically based rendering system.
3 
4  Copyright (c) 2007-2014 by Wenzel Jakob and others.
5 
6  Mitsuba is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License Version 3
8  as published by the Free Software Foundation.
9 
10  Mitsuba is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 #if !defined(__MITSUBA_CORE_SSTREAM_H_)
21 #define __MITSUBA_CORE_SSTREAM_H_
22 
23 #include <mitsuba/mitsuba.h>
24 #include <mitsuba/core/stream.h>
25 
27 
28 /** \brief Portable %Stream implementation, which encapsulates a socket
29  * for IPv4/IPv6 network communications.
30  *
31  * By default, this type of stream is configured to use network byte
32  * order (= big endian).
33  *
34  * \ingroup libcore
35  * \ingroup libpython
36  */
38 public:
39  /// Socket typedef. For Windows it is based on the code in WinSock2.h
40 #if defined(_WIN64)
41  typedef uint64_t socket_t;
42 #elif defined(_WIN32)
43  typedef uint32_t socket_t;
44 #else
45  typedef int socket_t;
46 #endif
47 
48  // =============================================================
49  //! @{ \name Constructors
50  // =============================================================
51 
52  /**
53  * \brief Create a stream from an existing socket
54  * \remark This function is not exposed in the Python bindings
55  */
56  SocketStream(socket_t socket);
57 
58  /// Connect to the given host/port
59  SocketStream(const std::string &host, int port);
60 
61  //! @}
62  // =============================================================
63 
64  // =============================================================
65  //! @{ \name Socket stream-specific features
66  // =============================================================
67 
68  /// Return the peer's name
69  inline const std::string &getPeer() const { return m_peer; }
70 
71  /// Return the number of received bytes
72  inline size_t getReceivedBytes() const { return m_received; }
73 
74  /// Return the number of sent bytes
75  inline size_t getSentBytes() const { return m_sent; }
76 
77  /// Return a string representation
78  std::string toString() const;
79 
80  /// Handle the last socket-specific error (looks up the appropriate OS description)
81  inline bool handleError(const std::string &cmd, ELogLevel level = EError) { return SocketStream::handleError(m_peer, cmd, level); }
82 
83  /// Handle the last socket-specific error (looks up the appropriate OS description)
84  static bool handleError(const std::string &peer, const std::string &cmd, ELogLevel level = EError);
85 
86  //! @}
87  // =============================================================
88 
89  // =============================================================
90  //! @{ \name Implementation of the Stream interface
91  // =============================================================
92 
93  void read(void *ptr, size_t size);
94  void write(const void *ptr, size_t size);
95  void seek(size_t pos);
96  size_t getPos() const;
97  size_t getSize() const;
98  void truncate(size_t size);
99  void flush();
100  bool canWrite() const;
101  bool canRead() const;
102 
103  //! @}
104  // =============================================================
105 
107 protected:
108  /** \brief Virtual destructor
109  *
110  * The destructor frees all resources and closes
111  * the socket if it is still open
112  */
113  virtual ~SocketStream();
114 protected:
115  socket_t m_socket;
116  size_t m_received, m_sent;
117  std::string m_peer;
118 };
119 
121 
122 #endif /* __MITSUBA_CORE_SSTREAM_H_ */
size_t getReceivedBytes() const
Return the number of received bytes.
Definition: sstream.h:72
virtual void flush()=0
Flush the stream&#39;s buffers.
virtual bool canRead() const =0
Can we read from the stream?
#define MTS_EXPORT_CORE
Definition: getopt.h:29
virtual bool canWrite() const =0
Can we write to the stream?
#define MTS_NAMESPACE_BEGIN
Definition: platform.h:137
virtual void read(void *ptr, size_t size)=0
Read a specified amount of data from the stream.
int socket_t
Socket typedef. For Windows it is based on the code in WinSock2.h.
Definition: sstream.h:45
Abstract seekable stream class.
Definition: stream.h:58
#define MTS_DECLARE_CLASS()
This macro must be used in the initial definition in classes that derive from Object.
Definition: class.h:158
ELogLevel
Available Log message types.
Definition: formatter.h:28
Error message, causes an exception to be thrown.
Definition: formatter.h:33
const std::string & getPeer() const
Return the peer&#39;s name.
Definition: sstream.h:69
virtual void write(const void *ptr, size_t size)=0
Write a specified amount of data into the stream.
virtual size_t getPos() const =0
Get the current position inside the stream.
size_t getSentBytes() const
Return the number of sent bytes.
Definition: sstream.h:75
#define MTS_NAMESPACE_END
Definition: platform.h:138
bool handleError(const std::string &cmd, ELogLevel level=EError)
Handle the last socket-specific error (looks up the appropriate OS description)
Definition: sstream.h:81
virtual std::string toString() const
Return a string representation.
virtual void seek(size_t pos)=0
Seek to a position inside the stream.
Portable Stream implementation, which encapsulates a socket for IPv4/IPv6 network communications...
Definition: sstream.h:37
virtual size_t getSize() const =0
Return the size of the stream.
virtual void truncate(size_t size)=0
Truncate the stream to a given size.