Samchon Framework for CPP  1.0.0
samchon::protocol::IWebServer Class Reference

An interface for a physical server following web-socket. More...

#include <IWebServer.hpp>

Collaboration diagram for samchon::protocol::IWebServer:

Public Member Functions

 IWebServer ()
 Default Constructor. More...
 
virtual void open () override
 Open the web-socket server. More...
 
- Public Member Functions inherited from samchon::protocol::IServer
 IServer ()
 Default Constructor. More...
 
virtual void close ()
 Close the server. More...
 

Additional Inherited Members

- Protected Member Functions inherited from samchon::protocol::IServer
virtual auto MY_IP () const -> std::string
 (optional) Server's IP More...
 
virtual auto PORT () const -> int=NULL
 Port number of the server. More...
 
virtual void addClient (Socket *)=0
 Handling connection of a physical client. More...
 
- Protected Attributes inherited from samchon::protocol::IServer
Acceptor * acceptor
 An acceptor for clients. More...
 

Detailed Description

An interface for a physical server following web-socket.

IWebServer is a IServer following web-socket protocol.

[Inherited]

IServer provides methods for opening a server.

IServer is one of the basic 3 + 1 components that can make any type of network system in Samchon Framework with IProtocol and IClient. Looking around classes in Samchon Framework, you can see all servers are implemented from the IServer.

When a client connects to the server, the abstract method IServer::addClient() is called with a new thread. If you want to accept only a client at a time, use OneToOneServer instead.

protocol_interface.png

Example source

A simple chat server running on console

example/console_chat_server/ChatServer.hpp
1 #pragma once
2 #include <samchon/protocol/IServer.hpp>
3 #include <samchon/protocol/IProtocol.hpp>
4 
5 #include <set>
6 #include <samchon/library/RWMutex.hpp>
7 
8 namespace samchon
9 {
10  namespace example
11  {
21  namespace console_chat_server
22  {
23  class ChatClient;
24 
30  class ChatServer
31  : public protocol::IServer,
32  public protocol::IProtocol
33  {
34  private:
35  typedef protocol::IServer super;
36 
40  std::set<ChatClient*> clientSet;
41 
45  library::RWMutex rwMutex;
46 
47  public:
51  ChatServer();
52  virtual ~ChatServer() = default;
53 
57  void eraseClient(ChatClient *client);
58 
59  protected:
63  virtual auto PORT() const -> int override
64  {
65  return 37749;
66  };
67 
71  virtual void addClient(protocol::Socket *socket) override;
72 
73  public:
80  virtual void replyData(std::shared_ptr<protocol::Invoke>) override;
81 
88  virtual void sendData(std::shared_ptr<protocol::Invoke>) override;
89  };
90  };
91  };
92 };
virtual auto PORT() const -> int=NULL
Port number of the server.
virtual void addClient(Socket *)=0
Handling connection of a physical client.
Top level namespace of products built from samchon.
Definition: ByteArray.hpp:7
example/console_chat_server/ChatServer.cpp
1 #include <samchon/examples/console_chat_server/ChatServer.hpp>
2 #include <samchon/examples/console_chat_server/ChatClient.hpp>
3 
4 #include <thread>
5 #include <samchon/protocol/Invoke.hpp>
6 
7 using namespace std;
8 using namespace samchon::library;
9 using namespace samchon::protocol;
11 
12 ChatServer::ChatServer()
13  : super()
14 {
15 }
16 void ChatServer::addClient(Socket *socket)
17 {
18  ChatClient *client = new ChatClient(this, socket);
19 
20  UniqueWriteLock uk(rwMutex);
21  clientSet.insert(client);
22 
23  thread(&ChatClient::listen, client).detach();
24 }
25 void ChatServer::eraseClient(ChatClient *client)
26 {
27  UniqueWriteLock uk(rwMutex);
28  clientSet.erase(client);
29 }
30 
31 void ChatServer::replyData(shared_ptr<Invoke> invoke)
32 {
33  sendData(invoke);
34 }
35 void ChatServer::sendData(shared_ptr<Invoke> invoke)
36 {
37  UniqueReadLock uk(rwMutex);
38 
39  for(auto it = clientSet.begin(); it != clientSet.end(); it++)
40  (*it)->sendData(invoke);
41 }
Definition: RWMutex.hpp:4
Package of libraries.
Definition: library.hpp:84
Unique lock for reading.
Package of network protocol and libraries.
Definition: protocol.hpp:185
Unique lock for writing.
example/console_chat_server/ChatClient.hpp
1 #pragma once
2 #include <samchon/protocol/IClient.hpp>
3 
4 namespace samchon
5 {
6  namespace example
7  {
8  namespace console_chat_server
9  {
10  class ChatServer;
11 
21  class ChatClient
22  : public protocol::IClient
23  {
24  private:
25  typedef protocol::IClient super;
26 
30  ChatServer *server;
31 
32  public:
39  ChatClient(ChatServer*, protocol::Socket*);
40 
45  virtual ~ChatClient();
46 
51  virtual void replyData(std::shared_ptr<protocol::Invoke>) override;
52  };
53  };
54  };
55 };
Top level namespace of products built from samchon.
Definition: ByteArray.hpp:7
example/console_chat_server/ChatClient.cpp
1 #include <samchon/examples/console_chat_server/ChatClient.hpp>
2 #include <samchon/examples/console_chat_server/ChatServer.hpp>
3 
4 #include <samchon/protocol/Invoke.hpp>
5 
6 using namespace std;
7 using namespace samchon::library;
8 using namespace samchon::protocol;
10 
11 ChatClient::ChatClient(ChatServer *server, Socket *socket)
12  : super()
13 {
14  this->server = server;
15  this->socket = socket;
16 }
17 ChatClient::~ChatClient()
18 {
19  server->eraseClient(this);
20 }
21 
22 void ChatClient::replyData(shared_ptr<Invoke> invoke)
23 {
24  server->replyData(invoke);
25 }
Definition: RWMutex.hpp:4
Package of libraries.
Definition: library.hpp:84
Package of network protocol and libraries.
Definition: protocol.hpp:185
example/console_chat_server/main.cpp
1 #include <iostream>
2 #include <samchon/examples/console_chat_server/ChatServer.hpp>
3 
4 #ifdef _WIN64
5 # ifdef _DEBUG
6 # pragma comment(lib, "x64/Debug/SamchonFramework.lib")
7 # else
8 # pragma comment(lib, "x64/Release/SamchonFramework.lib")
9 # endif
10 #else
11 # ifdef _DEBUG
12 # pragma comment(lib, "Debug/SamchonFramework.lib")
13 # else
14 # pragma comment(lib, "Release/SamchonFramework.lib")
15 # endif
16 #endif
17 
19 
20 void main()
21 {
22  ChatServer chatServer;
23  chatServer.open();
24 
25  system("pause");
26 }
Author
Jeongho Nam http://samchon.org

Definition at line 19 of file IWebServer.hpp.

Constructor & Destructor Documentation

IWebServer::IWebServer ( )

Default Constructor.

Definition at line 21 of file IWebServer.cpp.

Member Function Documentation

void IWebServer::open ( )
overridevirtual

Open the web-socket server.

Accepts clients and handshake with them following web-socket protocol.

Note
It monopolies a thread.

Reimplemented from samchon::protocol::IServer.

Definition at line 25 of file IWebServer.cpp.

References samchon::protocol::IServer::acceptor, samchon::protocol::IServer::addClient(), samchon::WeakString::between(), samchon::protocol::IServer::MY_IP(), samchon::protocol::IServer::PORT(), samchon::ByteArray::read(), and samchon::ByteArray::writeReversely().

Here is the call graph for this function:


The documentation for this class was generated from the following files: