Skip to content

Commit e17e5e2

Browse files
author
Paul Bienkowski
committed
PlayerAServer: use string and string_view for commands
1 parent 7399f91 commit e17e5e2

2 files changed

Lines changed: 28 additions & 49 deletions

File tree

src/OpenCOVER/audio/PlayerAServer.cpp

Lines changed: 26 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ using namespace opencover::audio;
3232

3333
#define MAX_BUFLEN 1024
3434

35-
namespace {
35+
namespace
36+
{
3637

3738
int errno_sys()
3839
{
@@ -53,7 +54,7 @@ int close(int fd)
5354
}
5455

5556
PlayerAServer::PlayerAServer(const Listener *listener, const std::string &host, int port, bool isMaster)
56-
: Player(listener,isMaster)
57+
: Player(listener, isMaster)
5758
, asFd(-1)
5859
, asHost(host)
5960
, asPort(port)
@@ -72,7 +73,7 @@ void PlayerAServer::connect()
7273
if (!isMaster)
7374
{
7475
asFd = -1;
75-
cerr << "AServer::connect() not Master" << endl;
76+
cerr << "AServer::connect() not Master" << endl;
7677
return;
7778
}
7879
cerr << "AServer::connect()2" << endl;
@@ -151,7 +152,8 @@ void PlayerAServer::connect()
151152

152153
getsockopt(asFd, SOL_SOCKET, SO_ERROR, &so_error, &len);
153154

154-
if (so_error != 0) {
155+
if (so_error != 0)
156+
{
155157
CERR << "connecting socket failed: " << strerror(errno_sys()) << endl;
156158
close(asFd);
157159
asFd = -1;
@@ -184,39 +186,21 @@ bool PlayerAServer::isConnected() const
184186
return asFd != -1;
185187
}
186188

187-
int PlayerAServer::send_cmd(const char *cmd) const
189+
int PlayerAServer::sendCommand(std::string_view cmd) const
188190
{
189-
// fprintf(stderr, "AudioServer: %s\n", cmd);
190-
size_t len = strlen(cmd);
191-
char *buf = new char[len + 1];
192-
memcpy(buf, cmd, len);
193-
buf[len] = '\n';
194-
return send_data(buf, (int)len + 1, false);
191+
std::string buf = std::string(cmd) + "\n";
192+
std::cout << "OOOOO " << cmd << std::endl;
193+
return sendData(buf.c_str(), buf.length());
195194
}
196195

197-
int PlayerAServer::send_data(const char *data, int size, bool swapped) const
196+
int PlayerAServer::sendData(const char *data, int size) const
198197
{
199198
if (asFd == -1)
200199
{
201200
return 0;
202201
}
203202

204203
char *buf = NULL;
205-
if (swapped)
206-
{
207-
if (size % 2)
208-
{
209-
CERR << "odd: odd number of bytes to swap" << endl;
210-
return -1;
211-
}
212-
buf = new char[size];
213-
for (int i = 0; i < size; i += 2)
214-
{
215-
buf[i] = data[i + 1];
216-
buf[i + 1] = data[i];
217-
}
218-
data = buf;
219-
}
220204

221205
int written = 0;
222206
do
@@ -268,11 +252,6 @@ int PlayerAServer::send_data(const char *data, int size, bool swapped) const
268252
written += n;
269253
} while (written < size);
270254

271-
if (swapped)
272-
{
273-
delete[] buf;
274-
}
275-
276255
if (written < size)
277256
{
278257
CERR << "write failed: " << strerror(errno_sys()) << endl;
@@ -375,7 +354,7 @@ void PlayerAServer::Source::loadAudio(const Audio *audio)
375354

376355
char msg[MAX_BUFLEN];
377356
snprintf(msg, sizeof(msg), "GHDL %s", path.filename().c_str());
378-
if (player->send_cmd(msg) < 0)
357+
if (player->sendCommand(msg) < 0)
379358
{
380359
CERR << "writing GHDL command failed" << endl;
381360
asHandle = -1;
@@ -400,7 +379,7 @@ void PlayerAServer::Source::loadAudio(const Audio *audio)
400379

401380
size_t file_size = std::filesystem::file_size(path);
402381
snprintf(msg, sizeof(msg), "PTFI %s %lu\r", path.filename().c_str(), file_size);
403-
if (player->send_cmd(msg) < 0)
382+
if (player->sendCommand(msg) < 0)
404383
{
405384
CERR << "writing command failed" << endl;
406385
}
@@ -411,15 +390,15 @@ void PlayerAServer::Source::loadAudio(const Audio *audio)
411390
for (size_t i = 0; i < file_size; i += 4096)
412391
{
413392
size_t s = fread(buf, 1, 4096, fd);
414-
if (player->send_data(buf, s) < 0)
393+
if (player->sendData(buf, s) < 0)
415394
{
416395
CERR << "writing data failed" << endl;
417396
}
418397
}
419398

420399
// Try again to get handle
421400
snprintf(msg, sizeof(msg), "GHDL %s", path.filename().c_str());
422-
if (player->send_cmd(msg) != 0)
401+
if (player->sendCommand(msg) != 0)
423402
{
424403
CERR << "writing command failed" << endl;
425404
asHandle = -1;
@@ -438,10 +417,10 @@ void PlayerAServer::Source::loadAudio(const Audio *audio)
438417
if (asHandle >= 0)
439418
{
440419
snprintf(msg, sizeof(msg), "SSVO %d 0.0", asHandle);
441-
player->send_cmd(msg);
420+
player->sendCommand(msg);
442421

443422
snprintf(msg, sizeof(msg), "SSPO %d 0.0 0.0 0.0", asHandle);
444-
player->send_cmd(msg);
423+
player->sendCommand(msg);
445424
}
446425
}
447426

@@ -453,7 +432,7 @@ void PlayerAServer::Source::setAudio(const Audio *audio)
453432
{
454433
snprintf(msg, sizeof(msg), "RHDL %d", asHandle);
455434
const PlayerAServer *player = (PlayerAServer *)this->player;
456-
player->send_cmd(msg);
435+
player->sendCommand(msg);
457436
}
458437

459438
loadAudio(audio);
@@ -471,7 +450,7 @@ PlayerAServer::Source::~Source()
471450
{
472451
snprintf(msg, sizeof(msg), "RHDL %d", asHandle);
473452
const PlayerAServer *player = (PlayerAServer *)this->player;
474-
player->send_cmd(msg);
453+
player->sendCommand(msg);
475454
}
476455
}
477456

@@ -483,7 +462,7 @@ void PlayerAServer::Source::play(double start)
483462
{
484463
snprintf(msg, sizeof(msg), "PLAY %d %f", asHandle, start);
485464
const PlayerAServer *player = (PlayerAServer *)this->player;
486-
if (player->send_cmd(msg) < 0)
465+
if (player->sendCommand(msg) < 0)
487466
{
488467
CERR << "playing handle " << asHandle << " failed" << endl;
489468
}
@@ -507,7 +486,7 @@ void PlayerAServer::Source::stop()
507486
{
508487
snprintf(msg, sizeof(msg), "STOP %d", asHandle);
509488
const PlayerAServer *player = (PlayerAServer *)this->player;
510-
player->send_cmd(msg);
489+
player->sendCommand(msg);
511490
}
512491
}
513492

@@ -534,7 +513,7 @@ void PlayerAServer::Source::update(const Player *genericPlayer)
534513
if (odirection != direction || ointensity != intensity)
535514
{
536515
snprintf(msg, sizeof(msg), "SSDV %d %f %f", asHandle, direction, intensity);
537-
player->send_cmd(msg);
516+
player->sendCommand(msg);
538517
ointensity = intensity;
539518
odirection = direction;
540519
}
@@ -544,22 +523,22 @@ void PlayerAServer::Source::update(const Player *genericPlayer)
544523
if (ointensity != intensity)
545524
{
546525
snprintf(msg, sizeof(msg), "SSVO %d %f", asHandle, intensity);
547-
player->send_cmd(msg);
526+
player->sendCommand(msg);
548527
}
549528
}
550529

551530
if (v != ov)
552531
{
553532
// snprintf(msg, sizeof(msg), "SSVE %d %f %f %f", asHandle, v.x, v.y, v.z);
554533
snprintf(msg, sizeof(msg), "SSVE %d %f", asHandle, glm::length(v));
555-
player->send_cmd(msg);
534+
player->sendCommand(msg);
556535
ov = v;
557536
}
558537

559538
if (opitch != pitch)
560539
{
561540
snprintf(msg, sizeof(msg), "SSPI %d %f", asHandle, pitch);
562-
player->send_cmd(msg);
541+
player->sendCommand(msg);
563542
opitch = pitch;
564543
}
565544
}
@@ -574,7 +553,7 @@ void PlayerAServer::Source::setLoop(bool loop)
574553
{
575554
snprintf(msg, sizeof(msg), "SSLP %d %d", asHandle, loop ? 1 : 0);
576555
const PlayerAServer *player = (PlayerAServer *)this->player;
577-
player->send_cmd(msg);
556+
player->sendCommand(msg);
578557
}
579558
}
580559

src/OpenCOVER/audio/PlayerAServer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ class COVRAUDIOEXPORT PlayerAServer : public Player
2222
virtual ~PlayerAServer();
2323
virtual std::unique_ptr<opencover::audio::Source> makeSource(const Audio *audio) override;
2424

25-
virtual int send_cmd(const char *cmd) const;
26-
virtual int send_data(const char *data, int size, bool swapped = false) const;
25+
virtual int sendCommand(std::string_view cmd) const;
26+
virtual int sendData(const char *data, int size) const;
2727
virtual int read_answer(char *buf, int maxsize) const;
2828
bool isConnected() const;
2929

0 commit comments

Comments
 (0)