]> git.vomp.tv Git - vompserver.git/blob - mvpserver.c
Initial import
[vompserver.git] / mvpserver.c
1 /*
2     Copyright 2004-2005 Chris Tallon
3
4     This file is part of VOMP.
5
6     VOMP 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     VOMP 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 VOMP; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "mvpserver.h"
22
23 // undeclared function
24 void MVPServerStartThread(void *arg)
25 {
26   MVPServer *m = (MVPServer *)arg;
27   m->run2();
28 }
29
30
31 MVPServer::MVPServer()
32 {
33   runThread = 0;
34   running = 0;
35 }
36
37 MVPServer::~MVPServer()
38 {
39   if (running) stop();
40 }
41
42 int MVPServer::stop()
43 {
44   if (!running) return 0;
45
46   udpr.stop();
47
48   pthread_cancel(runThread);
49   pthread_join(runThread, NULL);
50
51   close(listeningSocket);
52
53   return 1;
54 }
55
56 int MVPServer::run()
57 {
58   if (running) return 1;
59
60 //  logger.init(Log::DEBUG, "/tmp/vompserver.log");
61
62   if (udpr.run() == 0) return 0;
63
64   if (pthread_create(&runThread, NULL, (void*(*)(void*))MVPServerStartThread, (void *)this) == -1) return 0;
65   printf("MVPServer run success\n");
66   return 1;
67 }
68
69 void MVPServer::run2()
70 {
71   // Thread stuff
72   // I don't want signals and I want to die as soon as I am cancelled because I'll be in accept()
73   sigset_t sigset;
74   sigfillset(&sigset);
75   pthread_sigmask(SIG_BLOCK, &sigset, NULL);
76   pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
77   pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
78
79   struct sockaddr_in address;
80   address.sin_family = AF_INET;
81   address.sin_port = htons(3024);
82   address.sin_addr.s_addr = INADDR_ANY;
83   socklen_t length = sizeof(address);
84
85   listeningSocket = socket(AF_INET, SOCK_STREAM, 0);
86   if (listeningSocket < 0)
87   {
88     printf("Could not get TCP socket in vompserver\n");
89     return;
90   }
91
92   int value=1;
93   setsockopt(listeningSocket,SOL_SOCKET,SO_REUSEADDR,&value,sizeof(value));
94
95   if (bind(listeningSocket,(struct sockaddr *)&address,sizeof(address)) < 0)
96   {
97     printf("Could not bind to socket in vompserver\n");
98     close(listeningSocket);
99     return;
100   }
101
102   listen(listeningSocket, 5);
103
104   int clientSocket;
105
106   while(1)
107   {
108     clientSocket = accept(listeningSocket,(struct sockaddr *)&address, &length);
109     MVPClient* m = new MVPClient(clientSocket);
110     m->run();
111   }
112 }
113
114
115 ULLONG ntohll(ULLONG a)
116 {
117   return htonll(a);
118 }
119
120 ULLONG htonll(ULLONG a)
121 {
122   #if BYTE_ORDER == BIG_ENDIAN
123     return a;
124   #else
125     ULLONG b = 0;
126
127     b = ((a << 56) & 0xFF00000000000000ULL)
128       | ((a << 40) & 0x00FF000000000000ULL)
129       | ((a << 24) & 0x0000FF0000000000ULL)
130       | ((a <<  8) & 0x000000FF00000000ULL)
131       | ((a >>  8) & 0x00000000FF000000ULL)
132       | ((a >> 24) & 0x0000000000FF0000ULL)
133       | ((a >> 40) & 0x000000000000FF00ULL)
134       | ((a >> 56) & 0x00000000000000FFULL) ;
135
136     return b;
137   #endif
138 }