summaryrefslogtreecommitdiff
path: root/comm/OLD/dgram.h
blob: b2f9f77d528bccdde308f83f680729f80c31f4fc (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)
 *
 *	Reliable datagrams - to be updated
 *
 *	$Id$
 *	$CurLog$
 */

#ifndef _DGRAM_H_
#define _DGRAM_H_

#include "global.h"
#include "Datagram.h"
#include "ccu/IdTable.h"
#include "ccu/List.h"
#include "ccu/Timer.h"

class UchDGRAM;
class UchMessage;

class UchDGRAM_TIMER : public CcuBaseTimer {
protected:
	UchDGRAM&	dgram;
	void	Handle (Millisecond);

public:
		UchDGRAM_TIMER (UchDGRAM&);
		~UchDGRAM_TIMER ();
};

class PENDING;

class UchDGRAM : public UchDatagram {
protected:
	CcuIdTableOf <PENDING>	pending;		// pending messages
	sword		npending;	// number of pending messages
	short		retry;		// retry times
	CcuListOf <PENDING>		input;		// input messages
	sword		ninput;		// number of input messages
	pUchAddress	fromAddr;	// address of last acknowledged message
	lword		outId;		// id of message sent
	UchDGRAM_TIMER	timer;		// timer to resend
	Millisecond	timeout;		// timeout for timer
	short		locked;
	bool		resend;
	bool		sync;
	
	void		Init ();
	int		GetInput (int);
	bool		CheckInput ();
	bool		WaitInput ();
	
	void		SendAck (lword, UchAddress&);
	void		Expired ();
	UchMsgBuffer*	PrepareToSend (UchAddress&, int retries);
	int		SendBuffer (UchMsgBuffer&, UchAddress&);
	void		RemovePending (lword);
	bool		Wait (lword);
	void		Lock ()		{ locked++; }
	void		Unlock ()	{ if (--locked == 0 && resend) Resend (); }

public:
		UchDGRAM ();
		UchDGRAM (UchAddress*, UchAddress*);
		~UchDGRAM ();
	
	int	Send (byte*, int, UchAddress&, bool = false, int retries = 0);
	int	Receive (byte*, int);
	int	Reply (byte*, int, bool = false, int retries = 0);
	
	int	Send (UchMsgBuffer&, UchAddress&, bool = false, bool = false, int retries = 0);
	int	Receive (UchMsgBuffer&);
	int	Reply (UchMsgBuffer&, bool = false, bool = false, int retries = 0);
	
	bool	Send (UchMessage&, UchAddress&, bool = false, int retries = 0);
	bool	Receive (UchMessage* msg);
	bool	Reply (UchMessage&, bool = false, int retries = 0);
	
virtual	bool	DiscardNotify (UchMsgBuffer&, UchAddress&);
	
	int	NumPending ()		{ return npending; }
	int	NumInput ()		{ return ninput; }
	void	SetRetry (short r)		{ retry = r; }
	void	SetRetryTime (Millisecond);
	void	SetSync (bool s)		{ sync = s; if (s) Drain (); }
	
	void	Resend ();
	void	Drain ();
	
virtual	bool	NewMessage (UchMsgBuffer&);
	void	HandleRead ();
	bool	HandleSelect ();
	
friend	class	UchDGRAM_TIMER;
};

#endif /* _DGRAM_H_ */