TcpServer Class Reference
Collaboration diagram for TcpServer:

Public Member Functions

 TcpServer ()
 
 ~TcpServer ()
 
bool Listen (int32 port)
 
int32 Accept ()
 
 TcpServer (int read_timeout)
 
 ~TcpServer ()
 
bool Listen (int32 port)
 
int32 Accept ()
 
bool ReadChunk (size_t len)
 
Vector< BaseFloatGetChunk ()
 
bool Write (const std::string &msg)
 
bool WriteLn (const std::string &msg, const std::string &eol="\)
 
void Disconnect ()
 

Private Attributes

struct sockaddr_in h_addr_
 
int32 server_desc_
 
struct ::sockaddr_in h_addr_
 
int32 client_desc_
 
int16 * samp_buf_
 
size_t buf_len_
 
size_t has_read_
 
pollfd client_set_ [1]
 
int read_timeout_
 

Detailed Description

Definition at line 48 of file online-audio-server-decode-faster.cc.

Constructor & Destructor Documentation

◆ TcpServer() [1/2]

TcpServer ( )

Definition at line 340 of file online-audio-server-decode-faster.cc.

References TcpServer::server_desc_.

Referenced by main().

340  {
341  server_desc_ = -1;
342 }

◆ ~TcpServer() [1/2]

~TcpServer ( )

Definition at line 379 of file online-audio-server-decode-faster.cc.

References TcpServer::server_desc_.

Referenced by TcpServer::TcpServer().

379  {
380  if (server_desc_ != -1)
381  close(server_desc_);
382 }

◆ TcpServer() [2/2]

◆ ~TcpServer() [2/2]

~TcpServer ( )

Member Function Documentation

◆ Accept() [1/2]

int32 Accept ( )

◆ Accept() [2/2]

int32 Accept ( )

Definition at line 384 of file online-audio-server-decode-faster.cc.

References TcpServer::h_addr_, and TcpServer::server_desc_.

Referenced by main(), and TcpServer::TcpServer().

384  {
385  std::cout << "Waiting for client..." << std::endl;
386 
387  socklen_t len;
388 
389  len = sizeof(struct sockaddr);
390  int32 client_desc = accept(server_desc_, (struct sockaddr*) &h_addr_, &len);
391 
392  struct sockaddr_storage addr;
393  char ipstr[20];
394 
395  len = sizeof addr;
396  getpeername(client_desc, (struct sockaddr*) &addr, &len);
397 
398  struct sockaddr_in *s = (struct sockaddr_in *) &addr;
399  inet_ntop(AF_INET, &s->sin_addr, ipstr, sizeof ipstr);
400 
401  std::cout << "TcpServer: Accepted connection from: " << ipstr << std::endl;
402 
403  return client_desc;
404 }
kaldi::int32 int32

◆ Disconnect()

void Disconnect ( )

Definition at line 484 of file online2-tcp-nnet3-decode-faster.cc.

References TcpServer::client_desc_.

Referenced by TcpServer::TcpServer().

484  {
485  if (client_desc_ != -1) {
486  close(client_desc_);
487  client_desc_ = -1;
488  }
489 }

◆ GetChunk()

Vector< BaseFloat > GetChunk ( )

Definition at line 450 of file online2-tcp-nnet3-decode-faster.cc.

References TcpServer::has_read_, rnnlm::i, Vector< Real >::Resize(), and TcpServer::samp_buf_.

450  {
451  Vector<BaseFloat> buf;
452 
453  buf.Resize(static_cast<MatrixIndexT>(has_read_));
454 
455  for (int i = 0; i < has_read_; i++)
456  buf(i) = static_cast<BaseFloat>(samp_buf_[i]);
457 
458  return buf;
459 }
float BaseFloat
Definition: kaldi-types.h:29

◆ Listen() [1/2]

bool Listen ( int32  port)

◆ Listen() [2/2]

bool Listen ( int32  port)

Definition at line 344 of file online-audio-server-decode-faster.cc.

References TcpServer::h_addr_, KALDI_ERR, and TcpServer::server_desc_.

Referenced by main(), and TcpServer::TcpServer().

344  {
345  h_addr_.sin_addr.s_addr = INADDR_ANY;
346  h_addr_.sin_port = htons(port);
347  h_addr_.sin_family = AF_INET;
348 
349  server_desc_ = socket(AF_INET, SOCK_STREAM, 0);
350 
351  if (server_desc_ == -1) {
352  KALDI_ERR << "Cannot create TCP socket!";
353  return false;
354  }
355 
356  int32 flag = 1;
357  int32 len = sizeof(int32);
358  if( setsockopt(server_desc_, SOL_SOCKET, SO_REUSEADDR, &flag, len) == -1){
359  KALDI_ERR << "Cannot set socket options!\n";
360  return false;
361  }
362 
363  if (bind(server_desc_, (struct sockaddr*) &h_addr_, sizeof(h_addr_)) == -1) {
364  KALDI_ERR << "Cannot bind to port: " << port << " (is it taken?)";
365  return false;
366  }
367 
368  if (listen(server_desc_, 1) == -1) {
369  KALDI_ERR << "Cannot listen on port!";
370  return false;
371  }
372 
373  std::cout << "TcpServer: Listening on port: " << port << std::endl;
374 
375  return true;
376 
377 }
kaldi::int32 int32
#define KALDI_ERR
Definition: kaldi-error.h:147

◆ ReadChunk()

bool ReadChunk ( size_t  len)

Definition at line 417 of file online2-tcp-nnet3-decode-faster.cc.

References TcpServer::buf_len_, TcpServer::client_desc_, TcpServer::client_set_, TcpServer::has_read_, KALDI_WARN, TcpServer::read_timeout_, and TcpServer::samp_buf_.

417  {
418  if (buf_len_ != len) {
419  buf_len_ = len;
420  delete[] samp_buf_;
421  samp_buf_ = new int16[len];
422  }
423 
424  ssize_t ret;
425  int poll_ret;
426  size_t to_read = len;
427  has_read_ = 0;
428  while (to_read > 0) {
429  poll_ret = poll(client_set_, 1, read_timeout_);
430  if (poll_ret == 0) {
431  KALDI_WARN << "Socket timeout! Disconnecting...";
432  break;
433  }
434  if (poll_ret < 0) {
435  KALDI_WARN << "Socket error! Disconnecting...";
436  break;
437  }
438  ret = read(client_desc_, static_cast<void *>(samp_buf_ + has_read_), to_read * sizeof(int16));
439  if (ret <= 0) {
440  KALDI_WARN << "Stream over...";
441  break;
442  }
443  to_read -= ret / sizeof(int16);
444  has_read_ += ret / sizeof(int16);
445  }
446 
447  return has_read_ > 0;
448 }
#define KALDI_WARN
Definition: kaldi-error.h:150

◆ Write()

bool Write ( const std::string &  msg)

Definition at line 461 of file online2-tcp-nnet3-decode-faster.cc.

References TcpServer::client_desc_.

Referenced by TcpServer::WriteLn().

461  {
462 
463  const char *p = msg.c_str();
464  size_t to_write = msg.size();
465  size_t wrote = 0;
466  while (to_write > 0) {
467  ssize_t ret = write(client_desc_, static_cast<const void *>(p + wrote), to_write);
468  if (ret <= 0)
469  return false;
470 
471  to_write -= ret;
472  wrote += ret;
473  }
474 
475  return true;
476 }

◆ WriteLn()

bool WriteLn ( const std::string &  msg,
const std::string &  eol = "\n" 
)

Definition at line 478 of file online2-tcp-nnet3-decode-faster.cc.

References TcpServer::Write().

478  {
479  if (Write(msg))
480  return Write(eol);
481  else return false;
482 }
bool Write(const std::string &msg)

Member Data Documentation

◆ buf_len_

size_t buf_len_
private

◆ client_desc_

◆ client_set_

pollfd client_set_[1]
private

◆ h_addr_ [1/2]

struct sockaddr_in h_addr_
private

◆ h_addr_ [2/2]

struct ::sockaddr_in h_addr_
private

Definition at line 62 of file online2-tcp-nnet3-decode-faster.cc.

◆ has_read_

size_t has_read_
private

Definition at line 65 of file online2-tcp-nnet3-decode-faster.cc.

Referenced by TcpServer::GetChunk(), and TcpServer::ReadChunk().

◆ read_timeout_

int read_timeout_
private

◆ samp_buf_

int16* samp_buf_
private

◆ server_desc_


The documentation for this class was generated from the following files: