summaryrefslogtreecommitdiff
path: root/comm/MsgStream.h
blob: 0ed867f9eef14b2d7ad6a5f391c27ed8fa480f68 (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
/*
 *	The Unix Channel
 *
 *	by Michel Beaudouin-Lafon
 *
 *	Copyright 1990-1997
 *	Laboratoire de Recherche en Informatique (LRI)
 *
 *	Message streams
 *
 *	$Id$
 *	$CurLog$
 */

#ifndef MsgStream_H_
#define MsgStream_H_

#include "Stream.h"
#include "ccu/SmartPointer.h"
#include "MsgBuffer.h"
class UchMessage;

class UchBufStream : public UchStream {
protected:
	UchMsgBuffer	InBuffer;
	UchMsgBuffer	OutBuffer;
	int		OutSize;
	bool		Sync;

		UchBufStream (const UchBufStream&);
	int		ReadInput ();
	void		HandleWrite ();

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

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

public:
		UchBufStream (UchAddress* = 0, UchAddress* = 0);
		~UchBufStream ();

	void		InputBuffer (int min, int grow, int max);
	void		OutputBuffer (int min, int grow, int max);
inline	bool		GetSyncMode ()		{ return Sync; }
inline	void		SetSyncMode (bool s)	{ Sync = s; Flush (); }
inline	void		FlushSize (int n)		{ OutSize = n; }
virtual	void		Flush ();

virtual	void		Closing (bool);
};

class UchMsgStream : public UchBufStream {
protected:
enum	STATE		{ WAITING, GOT_TYPE, GOT_LENGTH, DONE};
enum	TYPE		{ MSG = 1, ASK, ANS, SYNC, ASYNC, OK };
	STATE		State;
	bool		BufferedMessages;
	UchMsgBuffer		Buffered;
	bool		WaitingReply;
	int		InLength;
	byte		InType;

	UchMessage*		Process (UchMsgBuffer&, bool);
		UchMsgStream (const UchMsgStream&);
	void	WriteMsg (UchMessage&);
	bool	ReadMsg (UchMessage&);

public:
		UchMsgStream (UchAddress* = 0, UchAddress* = 0);
		~UchMsgStream ();

	void		HandleRead ();
virtual	UchMessage*		DecodeMessage (UchMsgBuffer&);
virtual	UchMessage*		DecodeAnswer (UchMsgBuffer&);
	void		Send (UchMessage&, bool = false);
	UchMessage*		Ask (UchMessage&);
	void		Reply (UchMessage&);
	void		Send (UchMsgBuffer&, bool = false);
};

#endif /* MsgStream_H_ */