summaryrefslogtreecommitdiff
path: root/comm/Multiplexer.h
blob: c1b86e75700547141fd1d87b16b4f03da085b04b (plain)
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
/*
 *	The Unix Channel
 *
 *	by Michel Beaudouin-Lafon
 *
 *	Copyright 1990-1993
 *	Laboratoire de Recherche en Informatique (LRI)
 *
 *	Channel sets, or multiplexers
 *
 *	$Id$
 *	$CurLog$
 */

#ifndef Multiplexer_H_
#define Multiplexer_H_

#include "Channel.h"
#include <sys/types.h>
#include "ccu/Timer.h"

extern	char*	StrReprBuf;

//	This class defines channel sets for multiplexing i/o
//	Because of the coercion defined for FILDES -> int,
//	most arguments of type ints can be FILDES, UchChannel, ...
//	An array of pointers to channels is kept in the object;
//	we use smart pointers to handle deletion cleanly.
//	The masks for reading/writing are kept consistent with the IOMode of
//	the channels, as long as it is not changed by a direct access to the channel
//	operator[] returns a pointer and cannot be used as an lhs of an assignment.
//

enum	MPX_RES { isMpxEmpty, isMpxTerminated, isMpxError, isMpxAborted };

class UchBaseMultiplexer : public CcuSmartData {
friend	class	UchBaseTimeOut;
friend	class	UchBaseSignalHandler;


protected:
	pUchChannel*	Channels;
	CcuTimerSet	Timers;
	short		ReadCount;
	short		WriteCount;
	short		SelectCount;
	bool		Looping;
	bool	SigFired;
	UchBaseSignalHandler**	Handlers;
	int*	NbSignals;

	void	HandleSignal (UchBaseSignalHandler&);
	void	HandleDeferredSignals ();
inline	CcuTimerSet*	GetTimerSet ()	{ return &Timers; }

virtual	void		SetMasks (int, IOMODE) = 0;
virtual	void	SetTimeOut (Millisecond) = 0;
virtual	void	SuppressTimeOut () = 0;
virtual	void	AddSignalHook ();
virtual	MPX_RES	Loop () = 0;

public:
		UchBaseMultiplexer ();
		~UchBaseMultiplexer ();
	
inline	pUchChannel	operator [] (int fd)		{ if (fd < 0) return pUchChannel (0); else return Channels [fd]; }
	void	Add (UchChannel*);
inline	void	Add (const UchChannel& ch)		{ Add (ch.Copy ()); }
	void	Remove (int);
	void	RemoveAll ();
	void	SetMode (int, IOMODE);
	MPX_RES	Run ();
	void	Abort ();
	void	Close ();
};

PointerClass (pUchBaseMultiplexer, UchBaseMultiplexer);

class UchMultiplexer : public UchBaseMultiplexer {
protected:
	fd_set		ReadMask;
	fd_set		WriteMask;
	fd_set		SelectMask;
	Millisecond	TimeOut;
	int		fd0;	// used by HandleSelect and LoopScan

	void	SetMasks (int, IOMODE);
	void	SetTimeOut (Millisecond);
	void	SuppressTimeOut ();
	MPX_RES	Loop ();


public:
		UchMultiplexer ();
		~UchMultiplexer ();
	
	bool	HandleSelect ();
	int	Scan (bool nointr = TRUE, bool poll = FALSE);
	MPX_RES	LoopScan (bool nointr = TRUE);
inline	void	LoopEnd ()		{ Looping = FALSE; }
	char*	StrRepr (char* = StrReprBuf);
};

#endif /* Multiplexer_H_ */