34 |
|
|
35 |
|
|
36 |
//------------------------------------------------------------------------- |
//------------------------------------------------------------------------- |
37 |
// General utility functions. |
// Local client request executive. |
38 |
|
|
39 |
|
// Result buffer internal settler. |
40 |
|
void lscp_client_set_result ( lscp_client_t *pClient, char *pszResult, int iErrno ) |
41 |
|
{ |
42 |
|
if (pClient->pszResult) |
43 |
|
free(pClient->pszResult); |
44 |
|
pClient->pszResult = NULL; |
45 |
|
|
46 |
|
pClient->iErrno = iErrno; |
47 |
|
|
48 |
|
if (pszResult) |
49 |
|
pClient->pszResult = strdup(lscp_ltrim(pszResult)); |
50 |
|
} |
51 |
|
|
52 |
|
// The main client requester call executive. |
53 |
|
lscp_status_t lscp_client_call ( lscp_client_t *pClient, const char *pszQuery ) |
54 |
|
{ |
55 |
|
fd_set fds; // File descriptor list for select(). |
56 |
|
int fd, fdmax; // Maximum file descriptor number. |
57 |
|
struct timeval tv; // For specifying a timeout value. |
58 |
|
int iSelect; // Holds select return status. |
59 |
|
int iTimeout; |
60 |
|
int cchQuery; |
61 |
|
char achResult[LSCP_BUFSIZ]; |
62 |
|
int cchResult; |
63 |
|
const char *pszSeps = ":[]"; |
64 |
|
char *pszResult; |
65 |
|
char *pszToken; |
66 |
|
char *pch; |
67 |
|
int iErrno; |
68 |
|
|
69 |
|
lscp_status_t ret = LSCP_FAILED; |
70 |
|
|
71 |
|
if (pClient == NULL) |
72 |
|
return ret; |
73 |
|
|
74 |
|
pszResult = NULL; |
75 |
|
iErrno = -1; |
76 |
|
|
77 |
|
// Check if command socket socket is still valid. |
78 |
|
if (pClient->cmd.sock == INVALID_SOCKET) { |
79 |
|
pszResult = "Connection closed or no longer valid"; |
80 |
|
lscp_client_set_result(pClient, pszResult, iErrno); |
81 |
|
return ret; |
82 |
|
} |
83 |
|
|
84 |
|
// Send data, and then, wait for the result... |
85 |
|
cchQuery = strlen(pszQuery); |
86 |
|
if (send(pClient->cmd.sock, pszQuery, cchQuery, 0) < cchQuery) { |
87 |
|
lscp_socket_perror("_lscp_client_call: send"); |
88 |
|
pszResult = "Failure during send operation"; |
89 |
|
lscp_client_set_result(pClient, pszResult, iErrno); |
90 |
|
return ret; |
91 |
|
} |
92 |
|
|
93 |
|
// Prepare for waiting on select... |
94 |
|
fd = (int) pClient->cmd.sock; |
95 |
|
FD_ZERO(&fds); |
96 |
|
FD_SET((unsigned int) fd, &fds); |
97 |
|
fdmax = fd; |
98 |
|
|
99 |
|
// Use the timeout select feature... |
100 |
|
iTimeout = pClient->iTimeout; |
101 |
|
if (iTimeout > 1000) { |
102 |
|
tv.tv_sec = iTimeout / 1000; |
103 |
|
iTimeout -= tv.tv_sec * 1000; |
104 |
|
} |
105 |
|
else tv.tv_sec = 0; |
106 |
|
tv.tv_usec = iTimeout * 1000; |
107 |
|
|
108 |
|
// Wait for event... |
109 |
|
iSelect = select(fdmax + 1, &fds, NULL, NULL, &tv); |
110 |
|
if (iSelect > 0 && FD_ISSET(fd, &fds)) { |
111 |
|
// May recv now... |
112 |
|
cchResult = recv(pClient->cmd.sock, achResult, sizeof(achResult), 0); |
113 |
|
if (cchResult > 0) { |
114 |
|
// Assume early success. |
115 |
|
ret = LSCP_OK; |
116 |
|
// Always force the result to be null terminated (and trim trailing CRLFs)! |
117 |
|
while (cchResult > 0 && (achResult[cchResult - 1] == '\n' || achResult[cchResult- 1] == '\r')) |
118 |
|
cchResult--; |
119 |
|
achResult[cchResult] = (char) 0; |
120 |
|
// Check if the response it's an error or warning message. |
121 |
|
if (strncasecmp(achResult, "WRN:", 4) == 0) |
122 |
|
ret = LSCP_WARNING; |
123 |
|
else if (strncasecmp(achResult, "ERR:", 4) == 0) |
124 |
|
ret = LSCP_ERROR; |
125 |
|
// So we got a result... |
126 |
|
if (ret == LSCP_OK) { |
127 |
|
// Reset errno in case of success. |
128 |
|
iErrno = 0; |
129 |
|
// Is it a special successful response? |
130 |
|
if (strncasecmp(achResult, "OK[", 3) == 0) { |
131 |
|
// Parse the OK message, get the return string under brackets... |
132 |
|
pszToken = lscp_strtok(achResult, pszSeps, &(pch)); |
133 |
|
if (pszToken) |
134 |
|
pszResult = lscp_strtok(NULL, pszSeps, &(pch)); |
135 |
|
} |
136 |
|
else pszResult = achResult; |
137 |
|
// The result string is now set to the command response, if any. |
138 |
|
} else { |
139 |
|
// Parse the error/warning message, skip first colon... |
140 |
|
pszToken = lscp_strtok(achResult, pszSeps, &(pch)); |
141 |
|
if (pszToken) { |
142 |
|
// Get the error number... |
143 |
|
pszToken = lscp_strtok(NULL, pszSeps, &(pch)); |
144 |
|
if (pszToken) { |
145 |
|
iErrno = atoi(pszToken); |
146 |
|
// And make the message text our final result. |
147 |
|
pszResult = lscp_strtok(NULL, pszSeps, &(pch)); |
148 |
|
} |
149 |
|
} |
150 |
|
// The result string is set to the error/warning message text. |
151 |
|
} |
152 |
|
} |
153 |
|
else if (cchResult == 0) { |
154 |
|
// Damn, server disconnected, we better free everything down here. |
155 |
|
lscp_socket_agent_free(&(pClient->evt)); |
156 |
|
lscp_socket_agent_free(&(pClient->cmd)); |
157 |
|
// Fake a result message. |
158 |
|
ret = LSCP_QUIT; |
159 |
|
pszResult = "Server terminated the connection"; |
160 |
|
iErrno = (int) ret; |
161 |
|
} else { |
162 |
|
// What's down? |
163 |
|
lscp_socket_perror("_lscp_client_call: recv"); |
164 |
|
pszResult = "Failure during receive operation"; |
165 |
|
} |
166 |
|
} // Check if select has timed out. |
167 |
|
else if (iSelect == 0) { |
168 |
|
// Fake a result message. |
169 |
|
ret = LSCP_TIMEOUT; |
170 |
|
pszResult = "Timeout during receive operation"; |
171 |
|
iErrno = (int) ret; |
172 |
|
} |
173 |
|
else lscp_socket_perror("_lscp_client_call: select"); |
174 |
|
|
175 |
|
// Make the result official... |
176 |
|
lscp_client_set_result(pClient, pszResult, iErrno); |
177 |
|
|
178 |
|
return ret; |
179 |
|
} |
180 |
|
|
181 |
|
|
182 |
|
//------------------------------------------------------------------------- |
183 |
|
// Other general utility functions. |
184 |
|
|
185 |
// Trimming left spaces... |
// Trimming left spaces... |
186 |
char *lscp_ltrim ( char *psz ) |
char *lscp_ltrim ( char *psz ) |
444 |
|
|
445 |
void lscp_channel_info_init ( lscp_channel_info_t *pChannelInfo ) |
void lscp_channel_info_init ( lscp_channel_info_t *pChannelInfo ) |
446 |
{ |
{ |
447 |
pChannelInfo->engine_name = NULL; |
pChannelInfo->engine_name = NULL; |
448 |
pChannelInfo->audio_device = 0; |
pChannelInfo->audio_device = 0; |
449 |
pChannelInfo->audio_channels = 0; |
pChannelInfo->audio_channels = 0; |
450 |
pChannelInfo->audio_routing = NULL; |
pChannelInfo->audio_routing = NULL; |
451 |
pChannelInfo->instrument_file = NULL; |
pChannelInfo->instrument_file = NULL; |
452 |
pChannelInfo->instrument_nr = 0; |
pChannelInfo->instrument_nr = 0; |
453 |
pChannelInfo->midi_device = 0; |
pChannelInfo->instrument_status = 0; |
454 |
pChannelInfo->midi_port = 0; |
pChannelInfo->midi_device = 0; |
455 |
pChannelInfo->midi_channel = 0; |
pChannelInfo->midi_port = 0; |
456 |
pChannelInfo->volume = 0.0; |
pChannelInfo->midi_channel = 0; |
457 |
|
pChannelInfo->volume = 0.0; |
458 |
} |
} |
459 |
|
|
460 |
void lscp_channel_info_reset ( lscp_channel_info_t *pChannelInfo ) |
void lscp_channel_info_reset ( lscp_channel_info_t *pChannelInfo ) |