summaryrefslogtreecommitdiff
path: root/gio/src/socket.ccg
blob: e997c947c3393702b3f32f887a0e85c488657a04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* Copyright (C) 2007 The giomm Development Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <gio/gio.h>
#include <glibmm/error.h>
#include <giomm/asyncresult.h>
#include <giomm/socketsource.h>
#include <giomm/slot_async.h>

using Type = Gio::Socket::Type;
using Protocol = Gio::Socket::Protocol;

namespace Gio
{

Socket::Socket(SocketFamily family, Type type, Protocol protocol,
  const Glib::RefPtr<Cancellable>& cancellable)
: _CONSTRUCT("family", int(family), "type", int(type), "protocol", int(protocol))
{
  init(cancellable);
}

Socket::Socket(int fd, const Glib::RefPtr<Cancellable>& cancellable) : _CONSTRUCT("fd", fd)
{
  init(cancellable);
}

// static
Glib::RefPtr<Socket>
Socket::create(SocketFamily family, Type type, Protocol protocol,
  const Glib::RefPtr<Cancellable>& cancellable)
{
  return Glib::make_refptr_for_instance<Socket>(new Socket(family, type, protocol, cancellable));
}

// static
Glib::RefPtr<Socket>
Socket::create_from_fd(int fd, const Glib::RefPtr<Cancellable>& cancellable)
{
  return Glib::make_refptr_for_instance<Socket>(new Socket(fd, cancellable));
}

gssize
Socket::receive_from(Glib::RefPtr<SocketAddress>& address, char* buffer, gsize size,
  const Glib::RefPtr<Cancellable>& cancellable)
{
  GError* gerror = nullptr;
  GSocketAddress* caddr = nullptr;
  auto retvalue = g_socket_receive_from(
    gobj(), &caddr, buffer, size, const_cast<GCancellable*>(Glib::unwrap(cancellable)), &(gerror));
  if (gerror)
    ::Glib::Error::throw_exception(gerror);

  if (caddr)
    address = Glib::wrap(caddr);

  return retvalue;
}

gssize
Socket::receive_from(Glib::RefPtr<SocketAddress>& address, char* buffer, gsize size)
{
  GError* gerror = nullptr;
  GSocketAddress* caddr = nullptr;
  auto retvalue = g_socket_receive_from(gobj(), &caddr, buffer, size, nullptr, &(gerror));
  if (gerror)
    ::Glib::Error::throw_exception(gerror);

  if (caddr)
    address = Glib::wrap(caddr);

  return retvalue;
}

gssize
Socket::receive_with_blocking(
  gchar* buffer, gsize size, bool blocking, const Glib::RefPtr<Cancellable>& cancellable)
{
  GError* gerror = nullptr;
  const auto retvalue = g_socket_receive_with_blocking(
    gobj(), buffer, size, blocking, Glib::unwrap(cancellable), &(gerror));
  if (gerror)
    ::Glib::Error::throw_exception(gerror);

  return retvalue;
}

gssize
Socket::send_with_blocking(
  gchar* buffer, gsize size, bool blocking, const Glib::RefPtr<Cancellable>& cancellable)
{
  GError* gerror = nullptr;
  const auto retvalue = g_socket_send_with_blocking(
    gobj(), buffer, size, blocking, Glib::unwrap(cancellable), &(gerror));
  if (gerror)
    ::Glib::Error::throw_exception(gerror);

  return retvalue;
}

Glib::RefPtr<SocketSource>
Socket::create_source(Glib::IOCondition condition, const Glib::RefPtr<Cancellable>& cancellable)
{
  return SocketSource::create(gobj(), condition, cancellable);
}

} // namespace Gio