1 
2 /*-
3  *
4  * New BSD License 2006
5  *
6  * Copyright (c) 2006, Jorgen Lundman
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are
11  * met:
12  *
13  * 1 Redistributions of source code must retain the above copyright
14  *   notice, this list of conditions and the following disclaimer.
15  * 2 Redistributions in binary form must reproduce the above
16  *   copyright notice, this list of conditions and the following
17  *   disclaimer in the documentation and/or other materials provided
18  *   with the distribution.
19  * 3 Neither the name of the stuff nor the names of its contributors
20  *   may be used to endorse or promote products derived from this
21  *   software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  *
35  */
36 
37 /*
38  * template example
39  *
40  * by Jorgen Lundman <lundman@lundman.net>
41  *
42  * template that can be used for more serious application. Creates new
43  * nodes for each connection etc. Listens on a socket, and just relays
44  * traffic, like a simple chat room.
45  *
46  * 24/01/2003 - epoch
47  *
48  */
49 
50 #include <stdio.h>
51 #include <fcntl.h>
52 #include <signal.h>
53 
54 #include "lion.h"
55 
56 #include "object.h"
57 
58 
59 
60 static int master_switch = 0;
61 static int server_port   = 57777;  // Set to 0 for any port.
62 
63 
64 // The master listening socket. This could be a node as well, but
65 // since we only have one we have it here. We will only get _NEW from
66 // this one socket.
67 static object_t *listen = NULL;
68 
69 
70 
71 
72 
exit_interrupt(void)73 void exit_interrupt(void)
74 {
75 
76 	master_switch = 1;
77 
78 }
79 
80 
81 
82 
83 
84 
main(int argc,char ** argv)85 int main(int argc, char **argv)
86 {
87 
88 	signal(SIGINT, exit_interrupt);
89 #ifndef WIN32
90 	signal(SIGHUP, exit_interrupt);
91 #endif
92 
93 
94 	printf("Initialising Network...\n");
95 
96 	lion_init();
97 	lion_compress_level( 0 );
98 
99 	printf("Network Initialised.\n");
100 
101 
102 
103 	// Create an initial game
104 
105 	printf("Initialising Socket...\n");
106 
107 	listen = object_new();
108 	listen->type = OBJECT_TYPE_LISTEN;
109 
110 	listen->handle = lion_listen(&server_port, NULL, LION_FLAG_NONE,
111 								 (void *) listen);
112 
113 
114 	if (!listen) {
115 
116 		printf("Socket Failed...\n");
117 		master_switch = 1;
118 
119 	}
120 
121 
122 	printf("Listening on port %d...\n", server_port);
123 
124 
125 	while( !master_switch ) {
126 
127 		lion_poll(0, 1);     // This blocks. (by choice, FYI).
128 
129 	}
130 	printf("\n");
131 
132 
133 	printf("Releasing Socket...\n");
134 	if (listen) {
135 
136 		if (listen->handle) {
137 			lion_disconnect(listen->handle);
138 			listen->handle = NULL;
139 		}
140 
141 		object_free(listen);
142 		listen = NULL;
143 	}
144 	printf("Socket Released.\n");
145 
146 
147 	object_free_all();
148 
149 	lion_free();
150 
151 	printf("Network Released.\n");
152 
153 	printf("Done\n");
154 
155 	return 0; // Avoid warning
156 
157 }
158