1 | // Copyright (c) 2013 by István Váradi
|
---|
2 |
|
---|
3 | // This file is part of VSCPL, a simple cross-platform utility library
|
---|
4 |
|
---|
5 | // Redistribution and use in source and binary forms, with or without
|
---|
6 | // modification, are permitted provided that the following conditions are met:
|
---|
7 |
|
---|
8 | // 1. Redistributions of source code must retain the above copyright notice, this
|
---|
9 | // list of conditions and the following disclaimer.
|
---|
10 | // 2. Redistributions in binary form must reproduce the above copyright notice,
|
---|
11 | // this list of conditions and the following disclaimer in the documentation
|
---|
12 | // and/or other materials provided with the distribution.
|
---|
13 |
|
---|
14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
---|
15 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
---|
16 | // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
---|
17 | // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
---|
18 | // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
---|
19 | // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
---|
20 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
---|
21 | // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
22 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
---|
23 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
24 |
|
---|
25 | // The views and conclusions contained in the software and documentation are those
|
---|
26 | // of the authors and should not be interpreted as representing official policies,
|
---|
27 | // either expressed or implied, of the FreeBSD Project.
|
---|
28 |
|
---|
29 | //------------------------------------------------------------------------------
|
---|
30 |
|
---|
31 | #include <hu/varadiistvan/scpl/io/Waiter.h>
|
---|
32 | #include <hu/varadiistvan/scpl/io/LocalServerSocket.h>
|
---|
33 | #include <hu/varadiistvan/scpl/io/LocalAcceptor.h>
|
---|
34 | #include <hu/varadiistvan/scpl/io/BufferedStream.h>
|
---|
35 | #include <hu/varadiistvan/scpl/io/DataStream.h>
|
---|
36 |
|
---|
37 | #include <hu/varadiistvan/scpl/Thread.h>
|
---|
38 |
|
---|
39 | #include <cstdio>
|
---|
40 |
|
---|
41 | //------------------------------------------------------------------------------
|
---|
42 |
|
---|
43 | using hu::varadiistvan::scpl::io::Waiter;
|
---|
44 | using hu::varadiistvan::scpl::io::LocalServerSocket;
|
---|
45 | using hu::varadiistvan::scpl::io::LocalAcceptor;
|
---|
46 | using hu::varadiistvan::scpl::io::BufferedStream;
|
---|
47 | using hu::varadiistvan::scpl::io::DataStream;
|
---|
48 |
|
---|
49 | using hu::varadiistvan::scpl::Thread;
|
---|
50 |
|
---|
51 | //------------------------------------------------------------------------------
|
---|
52 |
|
---|
53 | class ServerThread : public Thread
|
---|
54 | {
|
---|
55 | hu::varadiistvan::scpl::io::Waiter waiter;
|
---|
56 |
|
---|
57 | hu::varadiistvan::scpl::io::BufferedStream* bufferedStream;
|
---|
58 |
|
---|
59 | hu::varadiistvan::scpl::io::DataStream stream;
|
---|
60 |
|
---|
61 | public:
|
---|
62 | ServerThread(hu::varadiistvan::scpl::io::LocalAcceptor& acceptor);
|
---|
63 |
|
---|
64 | virtual void run();
|
---|
65 | };
|
---|
66 |
|
---|
67 | //------------------------------------------------------------------------------
|
---|
68 |
|
---|
69 | ServerThread::ServerThread(LocalAcceptor& acceptor) :
|
---|
70 | bufferedStream(acceptor.getSocket(&waiter)),
|
---|
71 | stream(*bufferedStream)
|
---|
72 | {
|
---|
73 | printf("Created ServerThread[%p]\n", this);
|
---|
74 | }
|
---|
75 |
|
---|
76 | //------------------------------------------------------------------------------
|
---|
77 |
|
---|
78 | void ServerThread::run()
|
---|
79 | {
|
---|
80 | while(stream) {
|
---|
81 | uint8_t value = stream.readU8();
|
---|
82 | if (!stream) continue;
|
---|
83 |
|
---|
84 | printf("ServerThread[%p]: value=0x%02x\n", this, value);
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | //------------------------------------------------------------------------------
|
---|
89 | //------------------------------------------------------------------------------
|
---|
90 |
|
---|
91 | class ListenThread : public Thread
|
---|
92 | {
|
---|
93 | public:
|
---|
94 | virtual void run();
|
---|
95 | };
|
---|
96 |
|
---|
97 |
|
---|
98 | //------------------------------------------------------------------------------
|
---|
99 |
|
---|
100 | void ListenThread::run()
|
---|
101 | {
|
---|
102 | printf("ListenThread::run\n");
|
---|
103 |
|
---|
104 | Waiter waiter;
|
---|
105 |
|
---|
106 | LocalServerSocket socket("testmc", &waiter);
|
---|
107 |
|
---|
108 | LocalAcceptor& acceptor = socket.getAcceptor();
|
---|
109 | while(!acceptor.failed()) {
|
---|
110 | printf("ListenThread::run: accepting...\n");
|
---|
111 | while (acceptor.accept()) {
|
---|
112 | printf("ListenThread::run: connection accepted\n");
|
---|
113 | ServerThread* serverThread = new ServerThread(acceptor);
|
---|
114 | serverThread->start();
|
---|
115 | }
|
---|
116 | printf("ListenThread::run: waiting...\n");
|
---|
117 | waiter.wait();
|
---|
118 | printf("ListenThread::run: waited...\n");
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | //------------------------------------------------------------------------------
|
---|
123 |
|
---|
124 | int main()
|
---|
125 | {
|
---|
126 | ListenThread listenThread;
|
---|
127 | listenThread.start();
|
---|
128 |
|
---|
129 | while(true) {
|
---|
130 | Thread::sleep(3600*1000);
|
---|
131 | };
|
---|
132 | }
|
---|
133 |
|
---|
134 | //------------------------------------------------------------------------------
|
---|
135 |
|
---|
136 | // Local Variables:
|
---|
137 | // mode: C++
|
---|
138 | // c-basic-offset: 4
|
---|
139 | // indent-tabs-mode: nil
|
---|
140 | // End:
|
---|