Skip to content
Newer
Older
100644 85 lines (70 sloc) 1.92 KB
8c793c8 @LB-- Making some progress
LB-- authored Jan 19, 2013
1 #define NOMINMAX /*for Windows.h*/
2
3e82f4a @LB-- make Phi's stuff at least look like a library
LB-- authored Jan 18, 2013
3 #include <iostream>
8c793c8 @LB-- Making some progress
LB-- authored Jan 20, 2013
4 #include <limits>
5
b448294 @SortaCore General cleanup
authored May 9, 2013
6 // Keep console window open at end of application until user presses enter key
7 struct KeepOpen
8c793c8 @LB-- Making some progress
LB-- authored Jan 20, 2013
8 {
b448294 @SortaCore General cleanup
authored May 9, 2013
9 ~KeepOpen()
10 {
11 std::cin.sync();
12 std::cout << std::endl << "End of application, press Enter..." << std::flush;
13 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
14 }
15 } kr;
38094eb @LB-- baby steps
LB-- authored Jan 18, 2013
16
be46c99 @LB-- Fix inconsistent file extensions
LB-- authored Feb 8, 2013
17 #include "Relay.hh"
08c8e19 @LB-- Add support for C++11 range-based for-loops
LB-- authored Mar 19, 2013
18 using Deny = LwRelay::Server::Deny;
3e82f4a @LB-- make Phi's stuff at least look like a library
LB-- authored Jan 19, 2013
19
f0ac796 @LB-- Update example (wip)
LB-- authored Jan 30, 2013
20 struct Main
3e82f4a @LB-- make Phi's stuff at least look like a library
LB-- authored Jan 19, 2013
21 {
f0ac796 @LB-- Update example (wip)
LB-- authored Jan 30, 2013
22 lacewing::eventpump Pump;
23 LwRelay::Server Server;
76cf4f2 @LB-- Examples compile
LB-- authored Jan 30, 2013
24
b448294 @SortaCore General cleanup
authored May 9, 2013
25 Main (int nargs, const char * const * const args) : Pump(lacewing::eventpump_new()), Server(Pump)
f0ac796 @LB-- Update example (wip)
LB-- authored Jan 30, 2013
26 {
27 Server.Tag = static_cast<void *>(this);
28 Server.onError(OnError);
29 Server.onConnect(OnConnect);
30 }
b448294 @SortaCore General cleanup
authored May 9, 2013
31
32 ~Main ()
f0ac796 @LB-- Update example (wip)
LB-- authored Jan 30, 2013
33 {
60b2ad8 @LB-- Starting work on impl
LB-- authored Jan 31, 2013
34 Server.Tag = nullptr;
35 lacewing::pump_delete(Pump), Pump = nullptr;
f0ac796 @LB-- Update example (wip)
LB-- authored Jan 30, 2013
36 }
3e82f4a @LB-- make Phi's stuff at least look like a library
LB-- authored Jan 19, 2013
37
b448294 @SortaCore General cleanup
authored May 9, 2013
38 int Run ()
8c793c8 @LB-- Making some progress
LB-- authored Jan 20, 2013
39 {
b448294 @SortaCore General cleanup
authored May 9, 2013
40 Server.Host(); // NB: Default host port is 6121
41
42 // Successfully hosting
43 if (Server.Hosting())
f0ac796 @LB-- Update example (wip)
LB-- authored Jan 30, 2013
44 {
45 std::clog << "Server hosting on port: " << Server.Port() << std::endl;
b448294 @SortaCore General cleanup
authored May 9, 2013
46
f0ac796 @LB-- Update example (wip)
LB-- authored Jan 30, 2013
47 lacewing::error e = Pump->start_eventloop();
b448294 @SortaCore General cleanup
authored May 9, 2013
48
49 // Error starting/ending the event pump
50 if (e)
f0ac796 @LB-- Update example (wip)
LB-- authored Jan 30, 2013
51 {
52 std::cerr << e->tostring() << std::endl;
60b2ad8 @LB-- Starting work on impl
LB-- authored Feb 1, 2013
53 lacewing::error_delete(e), e = nullptr;
f0ac796 @LB-- Update example (wip)
LB-- authored Jan 30, 2013
54 return -1;
55 }
56 }
57 else
58 {
59 std::cerr << "Hosting failed" << std::endl;
60 return -1;
61 }
b448294 @SortaCore General cleanup
authored May 9, 2013
62
f0ac796 @LB-- Update example (wip)
LB-- authored Jan 30, 2013
63 return 0;
8c793c8 @LB-- Making some progress
LB-- authored Jan 20, 2013
64 }
f0ac796 @LB-- Update example (wip)
LB-- authored Jan 30, 2013
65
b448294 @SortaCore General cleanup
authored May 9, 2013
66 // Callback functions used with liblacewing
4b5fd20 @LB-- Fix compatibility problem with examples
LB-- authored Jan 30, 2013
67 static void (lw_callback OnError)(LwRelay::Server &Server, lacewing::error Error)
f0ac796 @LB-- Update example (wip)
LB-- authored Jan 30, 2013
68 {
69 std::cerr << "Error: \"" << Error->tostring() << '"' << std::endl;
70 static_cast<Main *>(Server.Tag)->Pump->post_eventloop_exit();
71 }
72
4b5fd20 @LB-- Fix compatibility problem with examples
LB-- authored Jan 31, 2013
73 static Deny (lw_callback OnConnect)(LwRelay::Server &Server, LwRelay::Server::Client &Client)
f0ac796 @LB-- Update example (wip)
LB-- authored Jan 30, 2013
74 {
75 std::clog << "Client connected from " << Client.Address()->tostring() << std::endl;
b448294 @SortaCore General cleanup
authored May 9, 2013
76 return true; // allow connection
77 // return "Your IP is banned from this server"; // disallow connection
f0ac796 @LB-- Update example (wip)
LB-- authored Jan 30, 2013
78 }
79 };
3e82f4a @LB-- make Phi's stuff at least look like a library
LB-- authored Jan 19, 2013
80
b448294 @SortaCore General cleanup
authored May 9, 2013
81 int main (int nargs, const char * const * const args)
3e82f4a @LB-- make Phi's stuff at least look like a library
LB-- authored Jan 19, 2013
82 {
b448294 @SortaCore General cleanup
authored May 9, 2013
83 return Main(nargs, args).Run();
3e82f4a @LB-- make Phi's stuff at least look like a library
LB-- authored Jan 19, 2013
84 }
Something went wrong with that request. Please try again.