summaryrefslogtreecommitdiff
path: root/ace/SOCK.cpp
blob: 27886121f9094a982bda842747bb8a5923a4fcee (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
// SOCK.cpp
// $Id$

#define ACE_BUILD_DLL
#include "ace/SOCK.h"

ACE_ALLOC_HOOK_DEFINE(ACE_SOCK)

#if defined (ACE_WIN32)
// Static used to initialize WinSock.
ACE_SOCK ACE_SOCK::dummy_;
#endif /* ACE_WIN32 */

void
ACE_SOCK::dump (void) const
{
  ACE_TRACE ("ACE_SOCK::dump");
}

ACE_SOCK::ACE_SOCK (void)
{
  ACE_TRACE ("ACE_SOCK::ACE_SOCK");

  // Make sure that we've initialized the WinSock library before using
  // sockets!
  ACE_OS::socket_init (ACE_WSOCK_VERSION);
}

// Returns information about the remote peer endpoint (if there is
// one).

int
ACE_SOCK::get_remote_addr (ACE_Addr &sa) const
{
  ACE_TRACE ("ACE_SOCK::get_remote_addr");
  int len = sa.get_size ();
  sockaddr *addr = (sockaddr *) sa.get_addr ();

  if (ACE_OS::getpeername (this->get_handle (), addr, &len) == -1)
    return -1;
  
  sa.set_size (len);
  return 0;
}

int
ACE_SOCK::get_local_addr (ACE_Addr &sa) const
{
  ACE_TRACE ("ACE_SOCK::get_local_addr");
  int len = sa.get_size ();
  sockaddr *addr = (sockaddr *) sa.get_addr ();

  if (ACE_OS::getsockname (this->get_handle (), addr, &len) == -1)
    return -1;

  sa.set_size ((int)len);
  return 0;
}

int
ACE_SOCK::open (int type, 
		int protocol_family, 
		int protocol)
{
  ACE_TRACE ("ACE_SOCK::open");
  this->set_handle (ACE_OS::socket (protocol_family, type, protocol));
  return this->get_handle () == ACE_INVALID_HANDLE ? -1 : 0;
}

// Close down a ACE_SOCK.

int
ACE_SOCK::close (void)
{
  ACE_TRACE ("ACE_SOCK::close");
  int result = 0;

  if (this->get_handle () != ACE_INVALID_HANDLE)
    {
      result = ACE_OS::closesocket (this->get_handle ());
      this->set_handle (ACE_INVALID_HANDLE);
    }
  return result;
}

// General purpose constructor for performing server ACE_SOCK
// creation.

ACE_SOCK::ACE_SOCK (int type, 
		    int protocol_family, 
		    int protocol)
{
  ACE_TRACE ("ACE_SOCK::ACE_SOCK");
  if (this->open (type, protocol_family, protocol) == -1)
    ACE_ERROR ((LM_ERROR, "%p\n", "ACE_SOCK::ACE_SOCK"));
}