source: fact/FADctrl/FADBoard.cc@ 10291

Last change on this file since 10291 was 10291, checked in by ogrimm, 10 years ago
Bug fix to internal pipe communication
File size: 19.5 KB
Line 
1/********************************************************************\
2
3 Class interfacing to FAD board
4
5\********************************************************************/
6
7#include "FADBoard.h"
8using namespace std;
9
10//
11// Constructor
12//
13FADBoard::FADBoard(string Server, unsigned short ServerPort, class FAD *Parent, unsigned int Num) {
14
15 int Ret;
16
17 // Initialization
18 m = Parent;
19 Active = false;
20 Continue = true;
21 CommOK = false;
22 ACalib.Time = -1;
23 Status.Update.tv_sec = -1;
24 Port = ServerPort;
25 Status.Frequency = 0;
26 Status.Rate = 0;
27 Status.BoardID = 0;
28
29 Name = new char [Server.size()+1]; // Name in permanent memory for DIM service
30 strcpy(Name, Server.c_str());
31
32 // Initialise mutex for synchronization
33 pthread_mutexattr_t Attr;
34
35 if ((Ret = pthread_mutexattr_settype(&Attr, PTHREAD_MUTEX_ERRORCHECK)) != 0) {
36 m->Message(m->ERROR, "pthread_mutex_settype() failed (%s)", strerror(Ret));
37 }
38 if ((Ret = pthread_mutex_init(&Mutex, &Attr)) != 0) {
39 m->Message(m->FATAL, "pthread_mutex_init() failed (%s)", strerror(Ret));
40 }
41
42 // Initialise condition variable for synchronization
43 if ((Ret = pthread_cond_init(&CondVar, NULL)) != 0) {
44 m->Message(m->FATAL, "pthread_cond_init() failed (%s)", strerror(Ret));
45 }
46
47 // Construct DIM service name prefix
48 stringstream ID;
49 ID << SERVER_NAME"/Board" << setfill('0') << setw(2) << Num << "/";
50
51 DIM_Name = new DimService((ID.str()+"Server").c_str(), Name);
52 DIM_Status = new DimService((ID.str()+"Status").c_str(), (char *) "");
53 DIM_ID = new DimService((ID.str()+"BoardID").c_str(), (char *) "S", NULL, 0);
54 DIM_Rate = new DimService((ID.str()+"RateHz").c_str(), Status.Rate);
55 DIM_Frequency = new DimService((ID.str()+"Frequency").c_str(), Status.Frequency);
56 DIM_Temp = new DimService((ID.str()+"Temperature").c_str(), (char *) "F", NULL, 0);
57 DIM_DAC = new DimService((ID.str()+"DAC").c_str(), (char *) "S", NULL, 0);
58 DIM_ROI = new DimService((ID.str()+"ROI").c_str(), (char *) "S", NULL, 0);
59
60 // Create thread that connects and receives data
61 SetStatus("Trying to connect...");
62
63 if ((Ret = pthread_create(&Thread, NULL, (void * (*)(void *)) LaunchThread,(void *) this)) != 0) {
64 m->Message(m->FATAL, "pthread_create() failed in FADBoard() (%s)", strerror(Ret));
65 }
66
67 // Start thread to connect to other sockets
68 DimThread::start();
69}
70
71//
72// Destructor
73//
74FADBoard::~FADBoard() {
75
76 int Ret;
77
78 // Cancel thread (if it did not quit already) and wait for it to quit
79 if ((Ret = pthread_cancel(Thread)) != 0 && Ret != ESRCH) {
80 m->Message(m->ERROR, "pthread_cancel() failed in ~FADBoard() (%s)", strerror(Ret));
81 }
82 if ((Ret = pthread_join(Thread, NULL)) != 0) {
83 m->Message(m->ERROR, "pthread_join() failed in ~FADBoard (%s)", strerror(Ret));
84 }
85
86 // Delete condition variable
87 if ((Ret = pthread_cond_destroy(&CondVar)) != 0) {
88 m->Message(m->ERROR, "pthread_cond_destroy() failed for %s in ~FADBoard (%s)", Name, strerror(Ret));
89 }
90
91 // Delete mutex
92 if ((Ret = pthread_mutex_destroy(&Mutex)) != 0) {
93 m->Message(m->ERROR, "pthread_mutex_destroy() failed for %s in ~FADBoard (%s)", Name, strerror(Ret));
94 }
95
96 delete DIM_Name;
97 delete DIM_Status;
98 delete DIM_ID;
99 delete DIM_Rate;
100 delete DIM_Frequency;
101 delete DIM_Temp;
102 delete DIM_DAC;
103 delete DIM_ROI;
104 delete[] Name;
105}
106
107
108//
109// Send data to board
110//
111void FADBoard::Send(const void *Data, size_t Bytes) {
112
113 // Do not send if not active or communication problem
114 if (!Active || !CommOK) return;
115
116 // Write data
117 ssize_t Result = write(Socket, Data, Bytes);
118
119 // Check result
120 if (Result == -1) m->PrintMessage("Error: Could not write to socket (%s)\n", strerror(errno));
121 else if ((size_t) Result < Bytes) m->PrintMessage("Error: Could only write %d bytes out of %d to socket\n", Result, Bytes);
122}
123
124void FADBoard::Send(unsigned short Data) {
125
126 unsigned short Buffer = htons(Data);
127
128 Send(&Buffer, sizeof(unsigned short));
129}
130
131
132//
133// Get board status (mutex protected to avoid concurrent access in ReadLoop)
134//
135struct FADBoard::BoardStatus FADBoard::GetStatus() {
136
137 int Ret;
138 struct BoardStatus S;
139
140 // Lock
141 if ((Ret = pthread_mutex_lock(&Mutex)) != 0) {
142 m->Message(m->FATAL, "pthread_mutex_lock() failed in ReadLoop() (%s)", strerror(Ret));
143 }
144
145 S = Status;
146
147 // Unlock
148 if ((Ret = pthread_mutex_unlock(&Mutex)) != 0) {
149 m->Message(m->FATAL, "pthread_mutex_unlock() failed in Unlock() (%s)", strerror(Ret));
150 }
151
152 return S;
153}
154
155
156//
157// Perform amplitude calibration in steps
158//
159// The steps are intended to assure that up to date data is available
160void FADBoard::AmplitudeCalibration() {
161
162 vector<unsigned short> ROICmd;
163 unsigned short DACCmd[] = {htons(CMD_Write | (BADDR_DAC + 1)), 0, htons(CMD_Write | (BADDR_DAC + 2)), 0, htons(CMD_Write | (BADDR_DAC + 3)), 0};
164 string Message = string("ACALIBDONE")+Name+"\n";
165
166 switch (State) {
167 // ====== Part A: Check if amplitude calibration should start and initialise =====
168 case standbye:
169 if (m->Mode != m->acalib) break;
170
171 // Invalidate current calibration
172 ACalib.Time = -1;
173 Count = 0;
174
175 // Save initial board status, set all ROIs to 1024 and set DAC values
176 InitialStatus = GetStatus();
177
178 for (unsigned int i=0; i<NChips*NChannels; i++) {
179 ROICmd.push_back(htons(CMD_Write | (BADDR_ROI + i)));
180 ROICmd.push_back(htons(NBins));
181 }
182 Send(&ROICmd[0], ROICmd.size()*sizeof(unsigned short));
183
184 DACCmd[1] = htons(0);
185 DACCmd[3] = htons(0);
186 DACCmd[5] = htons(0);
187 Send(DACCmd, sizeof(DACCmd));
188
189 // Clear sum vector and set state to accumulate
190 memset(Sum, 0, sizeof(Sum));
191 State = baseline;
192 break;
193
194 // ====== Part B: Baseline calibration =====
195 case baseline:
196 // Check for stopping
197 if (m->Mode != m->acalib) {
198 State = cleanup;
199 break;
200 }
201
202 // Average
203 for (unsigned int Chip=0; Chip<NChips; Chip++) {
204 for (unsigned int Chan=0; Chan<NChannels; Chan++) {
205 for (int i=0; i<Status.ROI[Chip][Chan]; i++) {
206 Sum[Chip][Chan][(i+Status.TriggerCell[Chip]) % NBins] += Data[Chip][Chan][i];
207 }
208 }
209 }
210 Count++;
211
212 // Determine baseline if integration finished
213 if (Count < m->NumEventsRequested) break;
214
215 for (unsigned int i=0; i<NChips; i++) {
216 for (unsigned int j=0; j<NChannels; j++) {
217 for (unsigned int k=0; k<NBins; k++) {
218 ACalib.Baseline[i][j][k] = Sum[i][j][k] / m->NumEventsRequested;
219 }
220 }
221 }
222
223 // Set new DAC values and start accumulation
224 DACCmd[1] = htons(50000);
225 DACCmd[3] = htons(50000);
226 DACCmd[5] = htons(50000);
227 Send(DACCmd, sizeof(DACCmd));
228
229 // Clear sum vector and set state to accumulate
230 memset(Sum, 0, sizeof(Sum));
231 Count = 0;
232 State = gain;
233 break;
234
235 // ====== Part C: Gain calibration =====
236 case gain:
237 // Check for stopping
238 if (m->Mode != m->acalib) {
239 State = cleanup;
240 break;
241 }
242
243 // Average
244 for (unsigned int Chip=0; Chip<NChips; Chip++) {
245 for (unsigned int Chan=0; Chan<NChannels; Chan++) {
246 for (int i=0; i<Status.ROI[Chip][Chan]; i++) {
247 Sum[Chip][Chan][(i+Status.TriggerCell[Chip]) % NBins] += Data[Chip][Chan][i];
248 }
249 }
250 }
251 Count++;
252
253 // Determine gain if integration finished
254 if (Count < m->NumEventsRequested) break;
255
256 for (unsigned int i=0; i<NChips; i++) {
257 for (unsigned int j=0; j<NChannels; j++) {
258 for (unsigned int k=0; k<NBins; k++) {
259 ACalib.Gain[i][j][k] = (Sum[i][j][k] / m->NumEventsRequested) - ACalib.Baseline[i][j][k];
260 }
261 }
262 }
263
264 // Set new DAC values and start accumulation
265 DACCmd[1] = htons(0);
266 DACCmd[3] = htons(0);
267 DACCmd[5] = htons(0);
268 Send(DACCmd, sizeof(DACCmd));
269
270 // Clear sum vector and set state to accumulate
271 memset(Sum, 0, sizeof(Sum));
272 Count = 0;
273 State = secondary;
274 break;
275
276 // ====== Part D: Secondary calibration =====
277 case secondary:
278 // Check for stopping
279 if (m->Mode != m->acalib) {
280 State = cleanup;
281 break;
282 }
283
284 // Average
285 for (unsigned int Chip=0; Chip<NChips; Chip++) {
286 for (unsigned int Chan=0; Chan<NChannels; Chan++) {
287 for (int i=0; i<Status.ROI[Chip][Chan]; i++) {
288 Sum[Chip][Chan][i] = Data[Chip][Chan][i] - ACalib.Baseline[Chip][Chan][(i-Status.TriggerCell[Chip]) % NBins];
289 }
290 }
291 }
292 Count++;
293
294 // Determine secondary baseline if integration finished
295 if (Count < m->NumEventsRequested) break;
296
297 for (unsigned int i=0; i<NChips; i++) {
298 for (unsigned int j=0; j<NChannels; j++) {
299 for (unsigned int k=0; k<NBins; k++) {
300 ACalib.Secondary[i][j][k] = Sum[i][j][k] / (double) m->NumEventsRequested;
301 }
302 }
303 }
304
305 // Store calibration time and temperature
306 ACalib.DNA = Status.DNA;
307 ACalib.Frequency = Status.Frequency;
308 ACalib.Time = time(NULL);
309 ACalib.Temp = 0;
310 for (unsigned int i=0; i<NTemp; i++) ACalib.Temp += Status.Temp[i] / NTemp;
311
312 // Inform event thread that calibration is finished for this board
313 if (write(m->Pipe[1], Message.data(), Message.size()) == -1) {
314 m->Message(m->ERROR, "write() to Pipe[1] failed in class FADBoard::AmplitudeCalibration (%s)", strerror(errno));
315 }
316
317 State = cleanup;
318 break;
319
320 // ====== Part E: Write back original ROI and DAC settings =====
321 case cleanup:
322 // ROI values
323 ROICmd.clear();
324 for (unsigned int i=0; i<NChips*NChannels; i++) {
325 ROICmd.push_back(htons(CMD_Write | (BADDR_ROI + i)));
326 ROICmd.push_back(htons(InitialStatus.ROI[i/NChannels][i%NChannels]));
327 }
328 Send(&ROICmd[0], ROICmd.size()*sizeof(unsigned short));
329
330 // DAC values
331 DACCmd[1] = htons(InitialStatus.DAC[1]);
332 DACCmd[3] = htons(InitialStatus.DAC[2]);
333 DACCmd[5] = htons(InitialStatus.DAC[3]);
334 Send(DACCmd, sizeof(DACCmd));
335
336 State = wait;
337 break;
338
339 // ====== Wait for Mode not being idle =====
340 case wait:
341 if (m->Mode == m->idle) State = standbye;
342 break;
343 }
344}
345
346//
347// Connect to board and read data
348//
349void FADBoard::ReadLoop() {
350
351 char Buffer[READ_BUFFER_SIZE];
352 unsigned int Pos = 0, Count = 0;
353 const PEVNT_HEADER *Header = (PEVNT_HEADER *) Buffer;
354 ssize_t Result;
355 struct sockaddr_in SocketAddress;
356 struct BoardStatus PrevStatus;
357 int Ret;
358
359 // Resolve hostname
360 struct hostent *Host = gethostbyname(Name);
361 if (Host == 0) {
362 SetStatus("Could not resolve host name '%s'", Name);
363 return;
364 }
365
366 SocketAddress.sin_family = PF_INET;
367 SocketAddress.sin_port = htons(Port);
368 SocketAddress.sin_addr = *(struct in_addr*) Host->h_addr;
369
370 // Open socket descriptor
371 if ((Socket = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
372 m->Message(m->ERROR, "Could not open socket for %s (%s)\n", Name, strerror(errno));
373 return;
374 }
375
376 // Connect to server
377 if (connect(Socket, (struct sockaddr *) &SocketAddress, sizeof(SocketAddress)) == -1) {
378 SetStatus("Could not connect to port %hu (%s)", Port, strerror(errno));
379 }
380 else {
381 CommOK = true;
382 Active = true;
383 SetStatus("Connected");
384 }
385
386 memset(&PrevStatus, 0, sizeof(PrevStatus));
387
388 // Leave loop if program termination requested or board communication not OK
389 while (!m->ExitRequest && CommOK) {
390 // Read data from socket
391 Result = read(Socket, Buffer + Pos, sizeof(Buffer)-Pos);
392
393 // Check result of read
394 if (Result == -1) {
395 m->Message(m->ERROR, "Could not read from socket for %s, exiting read loop (%s)\n", Name, strerror(errno));
396 CommOK = false;
397 break;
398 }
399 else if (Result == 0) {
400 SetStatus("Server not existing anymore, exiting read loop");
401 CommOK = false;
402 break;
403 }
404
405 // If not active, discard incoming data
406 if (!Active) continue;
407
408 // Advance write pointer
409 Pos += Result;
410
411 // Check if internal buffer full
412 if (Pos == sizeof(Buffer)) {
413 SetStatus("Internal buffer full, deleting all data in buffer");
414 Pos = 0;
415 continue;
416 }
417
418 // Check if buffer starts with start_package_flag, remove data if not
419 unsigned int Temp = 0;
420 while (ntohs(*((unsigned short *) (Buffer+Temp))) != 0xfb01 && Temp<Pos) Temp++;
421 if (Temp != 0) {
422 memmove(Buffer, Buffer+Temp, Pos-Temp);
423 Pos -= Temp;
424 SetStatus("Removed %d bytes because of start_package_flag not found", Temp);
425 continue;
426 }
427
428 // Wait until the buffer contains at least enough bytes to potentially hold a PEVNT_HEADER
429 if (Pos < sizeof(PEVNT_HEADER)) continue;
430
431 unsigned int Length = ntohs(Header->package_length)*2*sizeof(char);
432 if (Pos < Length) continue;
433
434 // Extract data if event end package flag correct
435 if (ntohs(*(unsigned short *) (Buffer+Length-sizeof(unsigned short))) == 0x04FE) {
436
437 // Prepare pointers to channel data (channels stored in order 0,9,18,27 - 1,10,19,28 - ... - 8,17,26,35)
438 PCHANNEL *Channel[NChips*NChannels], *Pnt=(PCHANNEL *) (Header+1);
439 for(unsigned int i=0; i<NChips*NChannels; i++) {
440 Channel[i] = Pnt;
441 Pnt = (PCHANNEL *) ((short *) (Channel[i] + 1) + ntohs(Channel[i]->roi));
442 }
443
444 // Wait until event thread processed the previous data and lock to avoid concurrent access in GetStatus()
445 Lock();
446 while (!Continue) {
447 if ((Ret = pthread_cond_wait(&CondVar, &Mutex)) != 0) {
448 m->Message(m->ERROR, "pthread_cond_wait() failed (%s)", strerror(Ret));
449 }
450 }
451 gettimeofday(&Status.Update, NULL);
452
453 // Extract board and trigger information
454 Status.BoardID = ntohs(Header->board_id);
455 Status.FirmwareRevision = ntohs(Header->version_no);
456 Status.BoardTime = ntohl(Header->time);
457 Status.EventCounter = ntohl(Header->fad_evt_counter);
458 Status.TriggerID = ntohl(Header->trigger_id);
459 Status.TriggerType = ntohs(Header->trigger_type);
460 Status.TriggerCRC = ntohs(Header->trigger_crc);
461 Status.DNA = Header->DNA;
462
463 // Extract frequency related information
464 Status.Frequency = ntohl(Header->REFCLK_frequency)/1.0e3*2.048;
465 Status.PhaseShift = Header->adc_clock_phase_shift;
466 for (unsigned int i=0; i<NChips; i++) {
467 if ((ntohs(Header->PLLLCK)>>12 & (1<<i)) != 0) Status.Lock[i] = true;
468 else Status.Lock[i] = false;
469 }
470
471 // Extract Firmware status info
472 Status.denable = (bool) ( ntohs(Header->PLLLCK) & (1<<11) );
473 Status.dwrite = (bool) ( ntohs(Header->PLLLCK) & (1<<10) );
474 Status.DCM_lock = (bool) ( ntohs(Header->PLLLCK) & (1<<9) );
475 Status.DCM_ready = (bool) ( ntohs(Header->PLLLCK) & (1<<8) );
476 Status.spi_clk = (bool) ( ntohs(Header->PLLLCK) & (1<<7) );
477
478 // Extract temperatures (MSB indicates if temperature is positive or negative)
479 for (unsigned int i=0; i<NTemp; i++) {
480 if ((ntohs(Header->drs_temperature[i]) & 0x8000) == 0) Status.Temp[i] = float(ntohs(Header->drs_temperature[i]) >> 3)/16;
481 else Status.Temp[i] = float(0xE000 | (ntohs(Header->drs_temperature[i])) >> 3)/16;
482 }
483
484 // Extract DAC channels
485 for (unsigned int i=0; i<NDAC; i++) Status.DAC[i] = ntohs(Header->dac[i]);
486
487 short Buf;
488 for (unsigned int Chip=0; Chip<NChips; Chip++) {
489 // Extract trigger cells
490 Status.TriggerCell[Chip] = (int) ntohs(Channel[Chip]->start_cell);
491
492 for (unsigned int Chan=0; Chan<NChannels; Chan++) {
493 // Extract ROI
494 Status.ROI[Chip][Chan] = ntohs(Channel[Chip+NChips*Chan]->roi);
495
496 // Extract ADC data (stored in 12 bit signed twis complement with out-of-range-bit and leading zeroes)
497 for (int i=0; i<Status.ROI[Chip][Chan]; i++) {
498 Buf = ntohs(Channel[Chip+NChips*Chan]->adc_data[i]);
499 (Buf <<= 4) >>= 4; //delete the sign-bit by shifting left and shift back
500 Data[Chip][Chan][i] = Buf;
501 }
502 }
503 }
504
505 // Prepare predicate for condition variable
506 Continue = false;
507 Count++;
508 Unlock();
509
510 // Amplitude calibration (will check if Mode is acalib)
511 AmplitudeCalibration();
512
513 // Update DIM services if necessary
514 if (Status.Update.tv_sec - PrevStatus.Update.tv_sec > m->EventUpdateDelay) {
515
516 // Determine event rate
517 Status.Rate =
518 Count / (double(Status.Update.tv_sec-PrevStatus.Update.tv_sec) + (Status.Update.tv_usec-PrevStatus.Update.tv_usec)/1000000.0);
519 Count = 0;
520
521 if (PrevStatus.Frequency != Status.Frequency) DIM_Frequency->updateService();
522
523 if (PrevStatus.Rate != Status.Rate) DIM_Rate->updateService();
524
525 if (memcmp(PrevStatus.Temp, Status.Temp, sizeof(Status.Temp)) != 0) {
526 DIM_Temp->updateService(Status.Temp, sizeof(Status.Temp));
527 }
528 if (memcmp(PrevStatus.DAC, Status.DAC, sizeof(Status.DAC)) != 0) {
529 DIM_DAC->updateService(Status.DAC, sizeof(Status.DAC));
530 }
531 if (memcmp(PrevStatus.ROI, Status.ROI, sizeof(Status.ROI)) != 0) {
532 DIM_ROI->updateService(Status.ROI, sizeof(Status.ROI));
533 }
534 if (PrevStatus.BoardID != Status.BoardID) {
535 DIM_ID->updateService(&Status.BoardID, sizeof(Status.BoardID));
536 }
537
538 PrevStatus = Status;
539 }
540
541 // Inform event thread of new data
542 string Message = string("EVENT")+Name+"\n";
543 if (write(m->Pipe[1], Message.data(), Message.size()) == -1) {
544 m->Message(m->ERROR, "write() to Pipe[1] failed in class FADBoard (%s)", strerror(errno));
545 break;
546 }
547 }
548 else SetStatus("End package flag incorrect, removing corrupt event");
549
550 // Remove event data from internal buffer
551 memmove(Buffer, Buffer+Length, Pos-Length);
552 Pos = Pos-Length;
553 } // while()
554
555 // Set inactive and close socket descriptor
556 Active = false;
557
558 if (close(Socket) == -1) {
559 m->Message(m->ERROR, "Could not close socket descriptor for board %s (%s)", Name, strerror(errno));
560 }
561
562}
563
564//
565// Launch read thread inside class
566//
567void FADBoard::LaunchThread(class FADBoard *m) {
568
569 m->ReadLoop();
570}
571
572
573//
574// Set status message
575//
576void FADBoard::SetStatus(const char *Format, ...) {
577
578 int Ret;
579
580 // Assemble message
581 va_list ArgumentPointer;
582 va_start(ArgumentPointer, Format);
583 Lock();
584 Ret = vsnprintf(Status.Message, sizeof(Status.Message), Format, ArgumentPointer);
585 Unlock();
586 va_end(ArgumentPointer);
587
588 if (Ret == -1) m->Message(m->FATAL, "snprintf() in FADBoard::SetStatus() failed (%s)", strerror(errno));
589
590 // Update status service
591 DIM_Status->updateService(Status.Message);
592}
593
594
595//
596// Lock and unlock mutex
597//
598void FADBoard::Lock() {
599
600 int Ret;
601
602 if ((Ret = pthread_mutex_lock(&Mutex)) != 0) {
603 m->Message(m->FATAL, "pthread_mutex_lock() failed in class FADBoard (%s)", strerror(Ret));
604 }
605}
606
607void FADBoard::Unlock() {
608
609 int Ret;
610
611 if ((Ret = pthread_mutex_unlock(&Mutex)) != 0) {
612 m->Message(m->FATAL, "pthread_mutex_unlock() failed in class FADBoard (%s)", strerror(Ret));
613 }
614}
615
616
617//
618// Open other sockets
619//
620// Error reporting is limited as this function is expected to be removed when firmware allows single socket
621//
622void FADBoard::threadHandler() {
623
624 int List[] = {5001, 5002, 5003, 5004, 5005, 5006, 5007};
625 int Socket[sizeof(List)/sizeof(int)], MaxSocketNum, Ret;
626 fd_set DescriptorList;
627 char Buffer[1000000];
628
629 // Resolve hostname
630 struct hostent *Host = gethostbyname(Name);
631 if (Host == 0) return;
632
633 // Connect to server
634 struct sockaddr_in SocketAddress;
635 SocketAddress.sin_family = PF_INET;
636 SocketAddress.sin_addr = *(struct in_addr*) Host->h_addr;
637
638 for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) {
639 // Open socket descriptor
640 if ((Socket[i] = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
641 m->Message(m->ERROR, "OtherSockets: Could not open socket for port %d (%s)\n", List[i], strerror(errno));
642 return;
643 }
644 MaxSocketNum = *max_element(Socket, Socket+sizeof(List)/sizeof(int));
645
646 // Connect to server
647 SocketAddress.sin_port = htons((unsigned short) List[i]);
648 if (connect(Socket[i], (struct sockaddr *) &SocketAddress, sizeof(SocketAddress)) == -1) return;
649 }
650
651 while(true) {
652 // Wait for data from sockets
653 FD_ZERO(&DescriptorList);
654 for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) FD_SET(Socket[i], &DescriptorList);
655 if (select(MaxSocketNum+1, &DescriptorList, NULL, NULL, NULL) == -1) {
656 m->Message(m->ERROR, "OtherSockets: Error with select() (%s)\n", strerror(errno));
657 break;
658 }
659
660 // Data from socket
661 for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) if (FD_ISSET(Socket[i], &DescriptorList)) {
662 Ret = read(Socket[i], Buffer, sizeof(Buffer));
663 if (Ret == -1) m->Message(m->ERROR, "OtherSockets: Error reading from port %d (%s)\n", List[i], strerror(errno));
664 }
665 }
666
667 // Close all sockets
668 for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) {
669 if ((Socket[i] != -1) && (close(Socket[i]) == -1)) {
670 m->Message(m->ERROR, "OtherSockets: Could not close socket of port %d (%s)", List[i], strerror(errno));
671 }
672 }
673}
Note: See TracBrowser for help on using the repository browser.