Labyrinth
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
client.hpp
1 //////////////////////////////
2 //
3 // File: lxs/network/client.hpp
4 // Author: Maciej Kaminski
5 // E-mail: maciej.a.kaminski@gmail.com
6 // Website: maciejkaminski.pl
7 //
8 // File creation date: Feb 15 2013
9 // This file is part of Project Labyrinth
10 // This file is going to be released under terms of GNU GPL in near future
11 //
12 //////////////////////////////
13 
14 #ifndef __LXS_CLIENT_H__
15 #define __LXS_CLIENT_H__
16 #include <sys/socket.h>
17 #include <string>
18 #include <queue>
19 namespace LxS
20 {
21  namespace System
22  {
23  class ServiceThread;
24  class MyMutex;
25  }
26  //!@brief namespace for network input-output, client, server, reactive listening
27  namespace Network
28  {
29  class Command;
30  //! generalized network client
31  class Client
32  {
33  bool active;
34  bool stop_ordered;
35  int sockfd;
36  int port;
37  std::string address;
40  void StopClientReal();
41  void StartClientReal();
42  void PerformIOReal();
43  Command * out_command;
44  Command * in_command;
45  std::queue<std::string> errors;
46  // non blocking
47  void SetError(const char * error);
48  public:
49  //! constuctor
50  Client();
51  //! clear error record (internal for this client class object)
52  void ClearError();
53  //! if internal error record has any error message recorded
54  bool HasError(); // if error happened
55  //! \brief get last error
56  //! Note: you can get more than one error by using
57  //! while (client::HasError)
58  //! { client::GetLastError }
59  std::string GetLastError();
60 
61  //! set TCP port on which to connect to remote server
62  void SetTCPPort(int p) {port = p;}
63  //! get tcp port
64  int GetTCPPort() {return port;}
65  //! stop client thread
66  void StopClient();
67  //! retrieve configured IP address
68  std::string GetAddress() { return address;}
69  //! configure address
70  void SetAddress(std::string addr) {address.assign(addr);}
71  //! start client thread
72  void StartClient();
73  //! send message over the protocol
74  void SendCommand(Command * cmd);
75  //! has received message?
76  bool CommandReceived();
77  //! get received message
78  Command * RecvCommand();
79 
80  //! is activated
81  bool IsConnected() {return active;}
82  //! is actively trying to connect or sending or receiving something
83  bool IsWorking();
84  virtual ~Client();
85  };
86  }
87 }
88 
89 
90 #endif