OnlineUdpInput Class Reference

#include <online-feat-input.h>

Inheritance diagram for OnlineUdpInput:
Collaboration diagram for OnlineUdpInput:

Public Member Functions

 OnlineUdpInput (int32 port, int32 feature_dim)
 
virtual bool Compute (Matrix< BaseFloat > *output)
 
virtual int32 Dim () const
 
const sockaddr_in & client_addr () const
 
const int32 descriptor () const
 
- Public Member Functions inherited from OnlineFeatInputItf
virtual ~OnlineFeatInputItf ()
 

Private Attributes

int32 feature_dim_
 
int32 sock_desc_
 
sockaddr_in server_addr_
 
sockaddr_in client_addr_
 

Detailed Description

Definition at line 160 of file online-feat-input.h.

Constructor & Destructor Documentation

◆ OnlineUdpInput()

OnlineUdpInput ( int32  port,
int32  feature_dim 
)

Definition at line 152 of file online-feat-input.cc.

References KALDI_ERR, OnlineUdpInput::server_addr_, and OnlineUdpInput::sock_desc_.

152  :
153  feature_dim_(feature_dim) {
154  server_addr_.sin_family = AF_INET; // IPv4
155  server_addr_.sin_addr.s_addr = INADDR_ANY; // listen on all interfaces
156  server_addr_.sin_port = htons(port);
157  sock_desc_ = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
158  if (sock_desc_ == -1)
159  KALDI_ERR << "socket() call failed!";
160  int32 rcvbuf_size = 30000;
161  if (setsockopt(sock_desc_, SOL_SOCKET, SO_RCVBUF,
162  &rcvbuf_size, sizeof(rcvbuf_size)) == -1)
163  KALDI_ERR << "setsockopt() failed to set receive buffer size!";
164  if (bind(sock_desc_,
165  reinterpret_cast<sockaddr*>(&server_addr_),
166  sizeof(server_addr_)) == -1)
167  KALDI_ERR << "bind() call failed!";
168 }
kaldi::int32 int32
#define KALDI_ERR
Definition: kaldi-error.h:147

Member Function Documentation

◆ client_addr()

const sockaddr_in& client_addr ( ) const
inline

Definition at line 168 of file online-feat-input.h.

Referenced by main().

168 { return client_addr_; }

◆ Compute()

bool Compute ( Matrix< BaseFloat > *  output)
virtual

Implements OnlineFeatInputItf.

Definition at line 171 of file online-feat-input.cc.

References OnlineUdpInput::client_addr_, KALDI_WARN, Matrix< Real >::Read(), Matrix< Real >::Resize(), and OnlineUdpInput::sock_desc_.

171  {
172  char buf[65535];
173  socklen_t caddr_len = sizeof(client_addr_);
174  ssize_t nrecv = recvfrom(sock_desc_, buf, sizeof(buf), 0,
175  reinterpret_cast<sockaddr*>(&client_addr_),
176  &caddr_len);
177  if (nrecv == -1) {
178  KALDI_WARN << "recvfrom() call error!";
179  output->Resize(0, 0);
180  return false;
181  }
182  std::stringstream ss(std::stringstream::in | std::stringstream::out);
183  ss.write(buf, nrecv);
184  output->Read(ss, true);
185  return true;
186 }
void Read(std::istream &in, bool binary, bool add=false)
read from stream.
#define KALDI_WARN
Definition: kaldi-error.h:150
void Resize(const MatrixIndexT r, const MatrixIndexT c, MatrixResizeType resize_type=kSetZero, MatrixStrideType stride_type=kDefaultStride)
Sets matrix to a specified size (zero is OK as long as both r and c are zero).

◆ descriptor()

const int32 descriptor ( ) const
inline

Definition at line 170 of file online-feat-input.h.

Referenced by main().

170 { return sock_desc_; }

◆ Dim()

virtual int32 Dim ( ) const
inlinevirtual

Implements OnlineFeatInputItf.

Definition at line 166 of file online-feat-input.h.

166 { return feature_dim_; }

Member Data Documentation

◆ client_addr_

sockaddr_in client_addr_
private

Definition at line 177 of file online-feat-input.h.

Referenced by OnlineUdpInput::Compute().

◆ feature_dim_

int32 feature_dim_
private

Definition at line 173 of file online-feat-input.h.

◆ server_addr_

sockaddr_in server_addr_
private

Definition at line 176 of file online-feat-input.h.

Referenced by OnlineUdpInput::OnlineUdpInput().

◆ sock_desc_

int32 sock_desc_
private

Definition at line 175 of file online-feat-input.h.

Referenced by OnlineUdpInput::Compute(), and OnlineUdpInput::OnlineUdpInput().


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