Drizzled Public API Documentation

http_server.cc
1 /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3  *
4  * Copyright (C) 2012 Mohit Srivastava
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
24 #include <plugin/json_server/http_server.h>
25 
26 namespace drizzle_plugin{
27  namespace json_server{
28 
29 int HTTPServer::BindSocket(const char* address,int port)
30 {
31  int r;
32  int nfd;
33  nfd = socket(AF_INET, SOCK_STREAM, 0);
34  if (nfd < 0) return -1;
35 
36  int one = 1;
37  r = setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(int));
38 
39  struct sockaddr_in addr;
40  memset(&addr, 0, sizeof(addr));
41  addr.sin_family = AF_INET;
42  addr.sin_addr.s_addr = inet_addr(address);
43  addr.sin_port = htons(port);
44 
45  r = bind(nfd, (struct sockaddr*)&addr, sizeof(addr));
46  if (r < 0) return -1;
47  r = listen(nfd, 10240);
48  if (r < 0) return -1;
49 
50  int flags;
51  if ((flags = fcntl(nfd, F_GETFL, 0)) < 0
52  || fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
53  return -1;
54 
55  return nfd;
56 
57 }
58 }
59 }
60