MLIB
Loading...
Searching...
No Matches
sockstream.h
Go to the documentation of this file.
1/*
2 Copyright (c) Mircea Neacsu (2014-2025) Licensed under MIT License.
3 This file is part of MLIB project. See LICENSE file for full license terms.
4*/
5
7
8#pragma once
9
10#if __has_include("defs.h")
11#include "defs.h"
12#endif
13
14#include "safe_winsock.h"
15#include <iostream>
16
17
18#include <assert.h>
19#include "sockbuf.h"
20
21namespace mlib {
22
23// clang-format on
24
25
33
34template <typename T>
35class generic_sockstream : public std::basic_iostream<T>
36{
37public:
40 : std::basic_iostream<T> (new sockbuf<T> ()){};
41
43 explicit generic_sockstream (const sockbuf<T>& sb)
44 : std::basic_iostream<T> (new sockbuf<T> (sb)){};
45
47 explicit generic_sockstream (const sock& s)
48 : std::basic_iostream<T> (new sockbuf<T> (s)){};
49
51 explicit generic_sockstream (sock::type t, int domain = AF_INET, int proto = 0)
52 : std::basic_iostream<T> (new sockbuf<T> (t, domain, proto)){};
53
55 explicit generic_sockstream (const inaddr& remote, sock::type t = sock::stream);
56
58
61 {
62 return (sockbuf<T>*)std::basic_ios<T>::rdbuf ();
63 }
64
66 {
67 return rdbuf ();
68 }
69};
70
71template <typename T>
73 : std::basic_iostream<T> (new sockbuf<T> ())
74{
75 rdbuf ()->open (t);
76 rdbuf ()->connect (remote);
77}
78
79template <typename T>
80generic_sockstream< T >::~generic_sockstream ()
81{
82 delete std::basic_ios<T>::rdbuf ();
83}
84
88
92
96
97
98} // namespace mlib
An IO stream using a sockbuf object as the underlying streambuf.
Definition sockstream.h:36
generic_sockstream(const sock &s)
Create from an existing mlib::sock.
Definition sockstream.h:47
sockbuf< T > * operator->()
Return the buffer used by this stream.
Definition sockstream.h:65
generic_sockstream(const sockbuf< T > &sb)
Create from an existing mlib::sockbuf.
Definition sockstream.h:43
generic_sockstream()
Default constructor.
Definition sockstream.h:39
sockbuf< T > * rdbuf()
Return the buffer used by this stream.
Definition sockstream.h:60
generic_sockstream(const inaddr &remote, sock::type t=sock::stream)
Create a SOCK_STREAM connected to a remote peer.
Definition sockstream.h:72
generic_sockstream(sock::type t, int domain=AF_INET, int proto=0)
Create a SOCK_STREAM or SOCK_DGRAM stream.
Definition sockstream.h:51
Wrapper for sockaddr structure.
Definition inaddr.h:25
We keep a reference counter associated with each sock object because it is more expensive to duplicat...
Definition sock.h:19
type
socket types
Definition sock.h:23
Provide functions required by streambuf interface using an underlying socket.
Definition sockbuf.h:58
generic_sockstream< char > sockstream
Definition sockstream.h:95
generic_sockstream< char > osockstream
Definition sockstream.h:91
generic_sockstream< char > isockstream
Definition sockstream.h:87
Definition of sockbuf class.