Belle II Software  release-08-01-10
EvtSocket.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 #include "daq/dataflow/EvtSocket.h"
10 
11 using namespace Belle2;
12 
13 // EvtSocketSend
14 
15 EvtSocketSend::EvtSocketSend(std::string host, int port)
16 {
17  m_sock = new SocketSend(host.c_str(), (u_short)port);
18  m_recbuf = new char[MAXEVTSIZE];
19 }
20 
21 EvtSocketSend::~EvtSocketSend()
22 {
23  delete m_sock;
24  delete[] m_recbuf;
25 }
26 
27 int EvtSocketSend::send(EvtMessage* msg)
28 {
29  // printf ( "EvtSocketSend : sending = %d\n", msg->size() );
30  return m_sock->put((char*)msg->buffer(), msg->size());
31 }
32 
33 EvtMessage* EvtSocketSend::recv()
34 {
35  int stat = m_sock->get(m_recbuf, MAXEVTSIZE);
36  if (stat <= 0) return NULL;
37  EvtMessage* evt = new EvtMessage(m_recbuf);
38  // delete [] evtbuf;
39  return evt;
40 }
41 
42 int EvtSocketSend::send_buffer(int nbytes, char* buf)
43 {
44  return m_sock->put(buf, nbytes);
45 }
46 
47 int EvtSocketSend::recv_buffer(char* buf)
48 {
49  int stat = m_sock->get(buf, MAXEVTSIZE);
50  return stat;
51 }
52 
53 int EvtSocketSend::recv_pxd_buffer(char* buf)
54 {
55  int stat = m_sock->get_pxd(buf, MAXEVTSIZE);
56  return stat;
57 }
58 
59 SocketSend* EvtSocketSend::sock(void)
60 {
61  return m_sock;
62 }
63 
64 
65 // EvtSocketRecv
66 
67 EvtSocketRecv::EvtSocketRecv(int port, bool accept_at_init)
68 {
69  m_sock = new SocketRecv((u_short)port);
70  m_recbuf = new char[MAXEVTSIZE];
71  if (accept_at_init)
72  m_sock->accept();
73 }
74 
75 EvtSocketRecv::~EvtSocketRecv()
76 {
77  delete m_sock;
78  delete[] m_recbuf;
79 }
80 
81 EvtMessage* EvtSocketRecv::recv()
82 {
83  int stat = m_sock->get(m_recbuf, MAXEVTSIZE);
84  if (stat <= 0) return NULL;
85  EvtMessage* evt = new EvtMessage(m_recbuf);
86  // delete [] evtbuf;
87  return evt;
88 }
89 
90 int EvtSocketRecv::send(EvtMessage* msg)
91 {
92  return m_sock->put((char*)msg->buffer(), msg->size());
93 }
94 
95 int EvtSocketRecv::send_buffer(int nbytes, char* buf)
96 {
97  return m_sock->put(buf, nbytes);
98 }
99 
100 int EvtSocketRecv::recv_buffer(char* buf)
101 {
102  int stat = m_sock->get(buf, MAXEVTSIZE);
103  return stat;
104 }
105 
106 SocketRecv* EvtSocketRecv::sock(void)
107 {
108  return m_sock;
109 }
110 
Class to manage streamed object.
Definition: EvtMessage.h:59
char * buffer()
Get buffer address.
Definition: EvtMessage.cc:76
int size() const
Get size of message including headers.
Definition: EvtMessage.cc:94
Abstract base class for different kinds of events.