forked from sPHENIX-Collaboration/rcdaq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaqBuffer.cc
214 lines (162 loc) · 4.18 KB
/
daqBuffer.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <daqBuffer.h>
#include <daqONCSEvent.h>
#include <daqPRDFEvent.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
using namespace std;
int readn (int fd, char *ptr, const int nbytes);
int writen (int fd, char *ptr, const int nbytes);
// the constructor first ----------------
daqBuffer::daqBuffer (const int irun, const int length
, const int iseq)
{
int *b = new int [length];
bptr = ( buffer_ptr ) b;
data_ptr = &(bptr->data[0]);
max_length = length; // in 32bit units
max_size = max_length;
current_event = 0;
current_etype = -1;
// preset everything to ONCS format
format=DAQONCSFORMAT;
currentBufferID = ONCSBUFFERHEADER;
prepare_next (iseq, irun);
}
// the destructor... ----------------
daqBuffer::~daqBuffer ()
{
int *b = (int *) bptr;
delete [] b;
}
int daqBuffer::prepare_next( const int iseq
, const int irun)
{
// re-initialize the event header length
bptr->Length = BUFFERHEADERLENGTH*4;
bptr->ID = currentBufferID;
bptr->Bufseq = iseq;
if (irun>0) bptr->Runnr = irun;
current_index = 0;
left = max_size - BUFFERHEADERLENGTH - EOBLENGTH;
has_end = 0;
return 0;
}
// ---------------------------------------------------------
int daqBuffer::nextEvent(const int etype, const int evtseq, const int evtsize)
{
if (current_event) delete current_event;
current_event = 0;
current_etype = -1;
if (evtsize > left-EOBLENGTH) return -1;
if ( format)
{
current_event = new daqPRDFEvent(&(bptr->data[current_index]), evtsize
,bptr->Runnr, etype, evtseq);
left -= (EVTHEADERLENGTH + 8);
current_index += EVTHEADERLENGTH+8;
bptr->Length += (EVTHEADERLENGTH+8)*4;
}
else
{
current_event = new daqONCSEvent(&(bptr->data[current_index]), evtsize
,bptr->Runnr, etype, evtseq);
left -= EVTHEADERLENGTH;
current_index += EVTHEADERLENGTH;
bptr->Length += EVTHEADERLENGTH*4;
}
current_etype = etype;
return 0;
}
// ----------------------------------------------------------
int daqBuffer::addSubevent( daq_device *dev)
{
int len;
len = current_event->addSubevent(current_etype, dev);
left -= len;
current_index += len;
bptr->Length += len*4;
return len;
}
// ----------------------------------------------------------
int daqBuffer::addEoB()
{
if (has_end) return -1;
bptr->data[current_index++] = 2;
bptr->data[current_index++] = 0;
bptr->Length += 2*4;
has_end = 1;
return 0;
}
// ----------------------------------------------------------
// int daqBuffer::transfer(dataProtocol * protocol)
// {
// if (protocol)
// return protocol->transfer((char *) bptr, bptr->Length);
// else
// return 0;
// }
unsigned int daqBuffer::writeout ( int fd)
{
if (!has_end) addEoB();
unsigned int bytes;
int blockcount = ( getLength() + 8192 -1)/8192;
int bytecount = blockcount*8192;
bytes = writen ( fd, (char *) bptr , bytecount );
return bytes;
}
#define ACKVALUE 101
unsigned int daqBuffer::sendData ( int fd, const int max_length)
{
if (!has_end) addEoB();
int total = getLength();
if ( total > max_length)
{
cout << "Monitoring: data size exceeds limit -- " << total << " limit: " << max_length << endl;
total = max_length;
}
int ntotal = htonl(total);
int status = writen(fd, (char *) &ntotal, 4);
char *p = (char *) bptr;
int sent = writen(fd,p,total);
return sent;
}
// ----------------------------------------------------------
int daqBuffer::setMaxSize(const int size)
{
if (size < 0) return -1;
if (size == 0) max_size = max_length;
else
{
max_size = (size + 8191)/8192;
max_size *= 2048;
if (max_size > max_length)
{
max_size = max_length;
return -2;
}
}
return 0;
}
// ----------------------------------------------------------
int daqBuffer::getMaxSize() const
{
return max_size*4;
}
int daqBuffer::setEventFormat(const int f)
{
if (f)
{
format = DAQPRDFFORMAT;
currentBufferID = PRDFBUFFERHEADER;
}
else
{
format = DAQONCSFORMAT;
currentBufferID = ONCSBUFFERHEADER;
}
bptr->ID = currentBufferID;
return 0;
}