1 |
/* |
2 |
* LSCP Shell |
3 |
* |
4 |
* Copyright (c) 2014 Christian Schoenebeck |
5 |
* |
6 |
* This program is part of LinuxSampler and released under the same terms. |
7 |
*/ |
8 |
|
9 |
#ifndef LSCPCLIENT_H |
10 |
#define LSCPCLIENT_H |
11 |
|
12 |
#include <stdio.h> |
13 |
#include <stdlib.h> |
14 |
#include <string> |
15 |
#include <list> |
16 |
|
17 |
#if defined(WIN32) |
18 |
#include <windows.h> |
19 |
typedef int socklen_t; |
20 |
#else |
21 |
#include <unistd.h> |
22 |
#include <sys/types.h> |
23 |
#include <sys/socket.h> |
24 |
#include <sys/select.h> |
25 |
#include <sys/time.h> |
26 |
#include <netinet/in.h> |
27 |
#include <netinet/tcp.h> |
28 |
#include <arpa/inet.h> |
29 |
#include <netdb.h> |
30 |
#endif |
31 |
|
32 |
#include "../common/global.h" |
33 |
#include "../common/Thread.h" |
34 |
#include "../common/Mutex.h" |
35 |
#include "../common/Condition.h" |
36 |
#include "../common/optional.h" |
37 |
#include "../network/lscp.h" |
38 |
|
39 |
using namespace LinuxSampler; |
40 |
|
41 |
/** |
42 |
* Implements network communication with LinuxSampler's LSCP server. A separate |
43 |
* thread is spawned which will constantly read on new incoming data coming from |
44 |
* the LSCP server, it can call a callback function whenever new response data |
45 |
* arrived. |
46 |
*/ |
47 |
class LSCPClient : protected Thread { |
48 |
public: |
49 |
typedef void(*Callback_t)(LSCPClient*); |
50 |
|
51 |
LSCPClient(); |
52 |
virtual ~LSCPClient(); |
53 |
bool connect(String host, int port); |
54 |
void disconnect(); |
55 |
bool isConnected() const; |
56 |
bool send(char c); |
57 |
bool send(String s); |
58 |
String sendCommandSync(String s); |
59 |
bool lineAvailable() const; |
60 |
bool messageComplete(); |
61 |
bool multiLine(); |
62 |
optional<String> lookAheadLine(int index); |
63 |
optional<String> popLine(); |
64 |
void setCallback(Callback_t fn); |
65 |
void setErrorCallback(Callback_t fn); |
66 |
protected: |
67 |
int Main() OVERRIDE; |
68 |
optional<String> receiveLine(); |
69 |
private: |
70 |
int hSocket; |
71 |
String m_lineBuffer; |
72 |
std::list<String> m_lines; |
73 |
Mutex m_linesMutex; |
74 |
Callback_t m_callback; |
75 |
Callback_t m_errorCallback; |
76 |
Condition m_sync; |
77 |
bool m_multiLineExpected; |
78 |
bool m_multiLineComplete; |
79 |
}; |
80 |
|
81 |
#endif // LSCPCLIENT_H |