Samchon Framework for CPP  1.0.0
Client.hpp
1 #pragma once
2 #include <samchon/protocol/ServerConnector.hpp>
3 
4 #include <iostream>
5 #include <fstream>
6 #include <thread>
7 #include <samchon/protocol/Invoke.hpp>
8 
9 namespace samchon
10 {
11  namespace example
12  {
13  namespace invoke_binary
14  {
15 
16 class Client
17  : public protocol::ServerConnector
18 {
19 private:
20  typedef protocol::ServerConnector super;
21 
22 public:
23  Client()
24  : super()
25  {
26  };
27  virtual void listen()
28  {
29  std::thread
30  (
31  &Client::sendData, this,
32  std::make_shared<protocol::Invoke>("getFile")
33  ).detach();
34 
35  super::listen();
36  }
37 
38  virtual auto getIP() const -> std::string override
39  {
40  return "127.0.0.1";
41  };
42  virtual auto getPort() const -> int override
43  {
44  return 11711;
45  };
46 
47  virtual void replyData(std::shared_ptr<protocol::Invoke> invoke) override
48  {
49  if (invoke->getListener() != "saveFile")
50  return;
51 
52  const std::string &name = invoke->at(0)->referValue<std::string>();
53  const std::string &extension = invoke->at(1)->referValue<std::string>();
54 
55  const ByteArray &data = invoke->at(2)->referValue<ByteArray>();
56 
57  std::ofstream file("E:/" + name + "." + extension, std::ios::out | std::ios::binary);
58  file.write((const char*)&data[0], data.size());
59 
60  file.close();
61  };
62 };
63 
64  };
65  };
66 };
virtual void replyData(std::shared_ptr< Invoke >)
Reply a message.
Definition: RWMutex.hpp:4
virtual void listen()
Listens message from a related system.
Definition: IClient.cpp:45
Top level namespace of products built from samchon.
Definition: ByteArray.hpp:7