summaryrefslogtreecommitdiff
path: root/apps/drwho/CM_Server.cpp
blob: 647e74fede88d986dba7e54395e9a0c518da08ed (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
// $Id$

#include "global.h"
#include "Options.h"
#include "CM_Server.h"
#include "ace/ACE.h"
#include "ace/Log_Msg.h"

// Creates and binds a UDP socket...

int
CM_Server::open (short port_number)
{
  int max_packet_size = UDP_PACKET_SIZE;

  this->sokfd_ = socket (PF_INET, SOCK_DGRAM, 0);

  if (this->sokfd_ < 0)
    return -1;

  ACE_OS::memset (&this->sin_, sizeof this->sin_, 0);
  this->sin_.sin_family = AF_INET;
  this->sin_.sin_port = htons (port_number);
  this->sin_.sin_addr.s_addr = INADDR_ANY;

  // This call fails if an rflo daemon is already running.
  if (ACE_OS::bind (this->sokfd_,
                    (sockaddr *) &this->sin_,
                    sizeof this->sin_) < 0)
    return -1;

  if (ACE_OS::setsockopt (this->sokfd_,
                          SOL_SOCKET,
                          SO_SNDBUF,
                          (char *) &max_packet_size,
                          sizeof max_packet_size) < 0)
    return -1;

  return 1;
}

int
CM_Server::receive (int)
{
  int sin_len = sizeof this->sin_;

  if (Options::get_opt (Options::DEBUG) != 0)
    ACE_DEBUG ((LM_DEBUG, "waiting for client to send...\n"));

  int n = ACE_OS::recvfrom (this->sokfd_,
                            this->recv_packet_,
                            UDP_PACKET_SIZE,
                            0,
                            (sockaddr *) &this->sin_,
                            (int *) &sin_len);
  if (n == -1)
    return -1;

  if (Options::get_opt (Options::DEBUG) != 0)
    ACE_DEBUG ((LM_DEBUG,
                "receiving from client host %s\n",
                ACE_OS::inet_ntoa (this->sin_.sin_addr)));

  if (this->demux (this->recv_packet_, n) < 0)
    return -1;

  return 1;
}

int
CM_Server::send (void)
{
  int  packet_length = 0;

  if (this->mux (this->send_packet_,
                 packet_length) < 0)
    return -1;

  if (Options::get_opt (Options::DEBUG) != 0)
    ACE_DEBUG ((LM_DEBUG,
                "sending to client host %s\n",
                ACE_OS::inet_ntoa (this->sin_.sin_addr)));

  if (sendto (this->sokfd_,
              this->send_packet_,
              packet_length,
              0,
              (sockaddr *) &this->sin_,
              sizeof this->sin_) < 0)
    return -1;

  return 1;
}

CM_Server::CM_Server (void)
{
}

CM_Server::~CM_Server (void)
{
  if (Options::get_opt (Options::DEBUG))
    ACE_DEBUG ((LM_DEBUG,
                "CM_Server\n"));

  ACE_OS::closesocket (this->sokfd_);
}