File: MNServerSocket.cc

package info (click to toggle)
mysql-query-browser 1.2.5beta-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 63,792 kB
  • ctags: 46,485
  • sloc: pascal: 249,299; ansic: 80,111; cpp: 72,467; sh: 25,271; objc: 20,015; yacc: 10,755; java: 9,917; xml: 4,580; php: 2,806; python: 1,566; sql: 1,563; makefile: 1,452; perl: 3
file content (126 lines) | stat: -rw-r--r-- 2,834 bytes parent folder | download | duplicates (4)
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
121
122
123
124
125
126
/* Copyright (c) 2004, 2005 MySQL AB
  
   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 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., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
 */

#include "MNSocket.h"
#include "MNServerSocket.h"

#if defined(__WIN__) || defined(_WIN32) || defined(_WIN64)
# include <winsock2.h>
# define socklen_t int
# define MSG_DONTWAIT 0
#else
# include <sys/types.h>
# include <sys/time.h>
# include <sys/socket.h>
# include <netinet/in.h>
# include <arpa/inet.h>
# include <sys/fcntl.h>
# include <netdb.h>
# include <unistd.h>
# define closesocket close
#endif
#include <glib.h>

#include <fcntl.h>
#include <errno.h>
#include <string.h>




MNServerSocket::MNServerSocket()
  : _socket(-1)
{
  MNSocket::initialize();
}


MNServerSocket::~MNServerSocket()
{
  if (_socket >= 0)
    closesocket(_socket);
}

    
bool MNServerSocket::bind(int port)
{
  struct sockaddr_in addr;
  int one= 1;
  int res;

  /* get a socket */
  _socket= socket(PF_INET, SOCK_STREAM, 0);
  if (_socket == (MYX_SOCKET)-1)
  {
    g_warning("could not create socket: %s", g_strerror(errno));
    return false;
  }

  // ignore fails of setsockopt
  setsockopt(_socket, SOL_SOCKET, SO_REUSEADDR, (const char *)&one, sizeof(one));

  memset(&addr, 0, sizeof(struct sockaddr_in));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  memset(&addr.sin_addr, 0, sizeof(addr.sin_addr));
  
  res = ::bind(_socket, (struct sockaddr*)&addr, sizeof(addr));
  if (res < 0)
  {
    g_warning("could not bind socket: %s", g_strerror(errno));
    closesocket(_socket);
    _socket= -1;
    return false;
  }

  return true;
}


bool MNServerSocket::listen()
{
  if (::listen(_socket, 5) < 0)
  {
    g_warning("could not listen to socket: %s", g_strerror(errno));
    return false;
  }
  return true;
}


MNSocket *MNServerSocket::accept()
{
  struct sockaddr_in addr;
  socklen_t addrlen= sizeof(addr);
  MYX_SOCKET s;
  MNSocket *sok;

  s= ::accept(_socket, (struct sockaddr*)&addr, &addrlen);
  if (s == (MYX_SOCKET)-1)
  {
    g_warning("error accepting connection: %s", g_strerror(errno));
    return NULL;
  }

  sok= new MNSocket();
  sok->wrap_socket(s, inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));

  return sok;
}