summaryrefslogtreecommitdiff
path: root/comm/Channel.h
blob: 1a9bd64b52380105a85c4d2f8bdf879996007aa7 (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
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
/*
 *	The Unix Channel
 *
 *	by Michel Beaudouin-Lafon
 *
 *	Copyright 1990-1997
 *	Laboratoire de Recherche en Informatique (LRI)
 *
 *	File descriptors, channels
 *
 *	$Id$
 *	$CurLog$
 *	Removed virtual IvlChannel::Copy ()
 *	Removed virutal IvlChannel::HandleSelect ()
 *	Smart(ies) removed by Stephane Sire
 *	Removed ReadHandler and WriteHandler
 *	NFILE -> 64, en attendant mieux
 */

#ifndef Channel_H_
#define Channel_H_

#ifdef __GNUG__
#pragma interface
#endif

#include "cplus_bugs.h"
#include "ivl/bool.h"
#include "IOS.h"

class IvlMsgBuffer;
class IvlString;

// we need read and write system calls
#include <unistd.h>

#ifndef NFILE
#define NFILE 64
#endif

enum	IOMODE	{ IONone = 0, IORead = 1, IOWrite = 2,
			  IOReadWrite = 3, IOSelect = 4, IOReadSelect = 5,
			  IOWriteSelect = 6, IOAll = 7 };

extern	char*	StrReprBuf;

class IvlFilDes {
private:
static	void	Use (int);
static	void	Unuse (int);

protected:
	int	Fd;
public:
inline		IvlFilDes () : Fd (-1)	{ }
inline		IvlFilDes (const IvlFilDes& fd)	{ Use (Fd = fd.Fd); }
inline		IvlFilDes (int fd)		{ Use (Fd = fd); }
inline		~IvlFilDes ()			{ Unuse (Fd); }

inline	operator int ()	const			{ return Fd; }
inline	IvlFilDes&	operator = (const IvlFilDes& fd)	{ Unuse (Fd); Fd = fd.Fd; Use (Fd); return *this; }
inline	int		FilDes ()			{ return Fd; }
inline	void		Open (int fd)		{ Unuse (Fd); Use (Fd = fd); }
inline	void		Close ()			{ Unuse (Fd); Fd = -1; }
inline	bool		Opened ()		{ return bool (Fd >= 0); }
inline	int		Read (byte* b, int n)	{ return read (Fd, (char*) b, n); }
inline	int		Write (byte* b, int n)	{ return write (Fd, (const char*) b, n); }
	char*		StrRepr (char* = StrReprBuf);
};

//	This class defines channels, ie file descriptors with handlers for read/write.
//	The file descriptor of a channel is immutable: once created, it cannot change;
//	this is crucial for channel sets, for instance.
//	Be careful with Copy and operator= if redefined because this can 
//	potentially bypass immutability.
//

class IvlChannel : public IvlIOS {
friend	class	IvlBaseScheduler;
protected:
	IvlFilDes		Fd;
	IOMODE	Mode;
	IvlBaseScheduler*	Mpx;

virtual	void	Added ();
virtual	void	Removed ();

public:
		IvlChannel ();
		IvlChannel (int, IOMODE = IOReadWrite);
		IvlChannel (const IvlChannel& ch);
		~IvlChannel ();

inline	void		Open (int fd)	{ if (Fd < 0) Fd.Open (fd); }
inline	void		Close ()		{ if (Fd.Opened ())  Fd.Close (); }
inline	IOMODE	IOMode () const			{ return Mode; }
inline	void		SetMode (IOMODE io)		{ Mode = io; }
	int		Read (IvlMsgBuffer&);
	int		Write (IvlMsgBuffer&);
	bool		ReadBuffer (IvlMsgBuffer&);
	bool		WriteBuffer (IvlMsgBuffer&);
	void		Add (IvlBaseScheduler*);
	void		Remove ();
virtual	void		HandleWrite ();
virtual	void		HandleRead ();

inline	int		FilDes () const			{ return Fd; }
inline	int		Read (byte* b, int n)	{ return read (Fd, (char*) b, n); }
inline	int		Write (const byte* b, int n)	{ return write (Fd, (const char*) b, n); }
inline			operator int ()		{ return Fd; }
inline	IvlBaseScheduler*	GetScheduler () const	{ return Mpx; }
inline	char*		StrRepr (char* s = StrReprBuf) { return Fd.StrRepr (s); }

virtual	void	WriteLong (lword);
virtual	void	WriteShort (sword);
virtual	void	WriteByte (byte);
virtual	void	WriteChar (char);
virtual	void	WriteString (const char*);
virtual	void	WriteBuf (const byte*, int);

virtual	bool	ReadLong (lword&);
virtual	bool	ReadShort (sword&);
virtual	bool	ReadByte (byte&);
virtual	bool	ReadChar (char&);
virtual	int	ReadString (char*, int);
virtual	int	ReadString (IvlString&);
virtual	bool	ReadBuf (byte*, int);
};

#endif /* Channel_H_ */