001:
002:
003:
004:
005:
006:
007:
008:
009:
010:
011:
012:
013:
014:
015:
016:
017:
018:
019:
020:
021:
022:
023:
024:
025:
026:
027:
028:
029:
030:
031:
032:
033:
034:
035:
036:
037:
038:
039:
040:
041:
042:
043:
044:
045:
046:
047:
048:
049:
050:
051:
052:
053:
054:
055:
056:
057:
058:
059:
060:
061:
062:
063:
064:
065:
066:
067:
068:
069:
070:
071:
072:
073:
074:
075:
076:
077:
078:
079:
080:
081:
082:
083:
084:
085:
086:
087:
088:
089:
090:
091:
092:
093:
094:
095:
096:
097:
098:
099:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:

/*
* Copyright (c), Zeriph Enterprises
* All rights reserved.
*
* Contributor(s):
* Zechariah Perez, omni (at) zeriph (dot) com
*
* THIS SOFTWARE IS PROVIDED BY ZERIPH AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ZERIPH AND CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#if !defined(OMNI_EXTENSION_NET_SERVER_HPP)
#define OMNI_EXTENSION_NET_SERVER_HPP 1
#include <omni/types/net_t.hpp>
#include <omni/net/socket.hpp>
#include <omni/net/endpoint_descriptor.hpp>
#include <omni/delegate/1.hpp>
#if defined(OMNI_SAFE_EXTENSION_NET_SERVER)
#include <omni/sync/basic_lock.hpp>
#endif
namespace omni {
namespace net {
class server
{
public:
server() :
m_clients(),
m_sock(omni::net::address_family::INET, omni::net::socket_type::STREAM, omni::net::protocol_type::TCP),
m_run(false)
{
if (this->m_sock.bind(12345) == omni::net::socket_error::SUCCESS) {
this->m_sock.listen();
}
}
~server()
{
this->_stop();
}
void swap(omni::net::server& other);
omni::event1<void, omni::net::endpoint_descriptor&> data_received;
bool is_running() const
{
omni::sync::scoped_basic_lock lock(&this->m_mtx);
return this->m_run;
}
void run()
{
this->m_mtx.lock();
if (this->m_sock.is_listening()) {
this->m_run = true;
}
this->m_mtx.unlock();
while (this->is_running()) {
omni::net::endpoint_descriptor* client = new omni::net::endpoint_descriptor();
if (this->m_sock.accept(*client) == omni::net::socket_error::SUCCESS) {
this->m_clients.push_back(client);
}
}
}
void signal(int sig)
{
omni::sync::scoped_basic_lock lock(&this->m_mtx);
uint32_t xfr = 0;
for (std::vector<omni::net::endpoint_descriptor*>::iterator client = this->m_clients.begin();
client != this->m_clients.end();
++client)
{
(*client)->send("END", xfr);
}
this->m_run = false;
}
operator bool()
{
omni::sync::scoped_basic_lock lock(&this->m_mtx);
return this->m_sock.is_listening();
}
private:
std::vector<omni::net::endpoint_descriptor*> m_clients;
omni::net::socket m_sock;
mutable omni::sync::basic_lock m_mtx;
volatile bool m_run;
void _stop()
{
omni::sync::scoped_basic_lock lock(&this->m_mtx);
for (std::vector<omni::net::endpoint_descriptor*>::iterator client = this->m_clients.begin();
client != this->m_clients.end();
++client)
{
(*client)->close();
delete (*client);
}
this->m_clients.clear();
this->m_sock.disconnect(false); // shutdown and do not reuse socket
}
};
}
}
namespace std {
inline void swap(omni::net::server& o1, omni::net::server& o2)
{
o1.swap(o2);
}
}
#endif // OMNI_EXTENSION_NET_SERVER_HPP