summaryrefslogtreecommitdiff
path: root/gio/src/socket.ccg
blob: 7a4a211d00ea727ee710d73c9ceddf2638c43068 (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
// -*- Mode: C++; indent-tabs-mode: nil; c-basic-offset: 2 -*-

/* 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, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

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

namespace Gio
{

Socket::Socket(SocketFamily family, SocketType type, SocketProtocol 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, SocketType type, SocketProtocol protocol,
                                    const Glib::RefPtr<Cancellable>& cancellable)
{
  return Glib::RefPtr<Socket>(new Socket(family, type, protocol, cancellable));
}

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

gssize Socket::receive_from(Glib::RefPtr<SocketAddress>& address, char* buffer, gsize size, const Glib::RefPtr<Cancellable>& cancellable)
{
  GError* gerror = 0;
  GSocketAddress* caddr = 0;
  gssize 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 = 0;
  GSocketAddress* caddr = 0;
  gssize retvalue = g_socket_receive_from(gobj(), &caddr, buffer, size, 0, &(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 = 0;
  gssize const 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 = 0;
  gssize const 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)
{
  // The corresponding unreference() takes place in the dtor
  // of the Glib::RefPtr<Socket> object below.
  reference();
  return SocketSource::create(Glib::RefPtr<Socket>(this), condition, cancellable);
}

} // namespace Gio