/*
 * NetMessage.h: interface for sending BMessages across the net (using UDP)
 * 
 * This is unsupported demo code. Use at your own risk.
 * 
 * Some caveats:
 *
 *    1. Since we may restransmit, duplicate messages can arrive
 *       at the server.  If performing your operation twice is harmless,
 *       then there is no problem.  Otherwise, you should use the 
 *       transaction ID's to remember recent transactions and not perform
 *       them again.  You still should reply back to the client though with
 *	 the result you obtained when you originally performed the transaction.
 *
 *    2. UDP buffer space is limited (roughly 8K), so try not to make
 *       your messages too big.
 */
#ifndef _NET_MESSAGE_H
#define _NET_MESSAGE_H

#include <Message.h>
#include <socket.h>

/*
 * Network errors in addition to standard Be error codes
 */
enum {
	NET_CANT_SEND = B_ERRORS_END + 0xea3b000,	/* trouble sending */
	NET_CANT_RECV	/* trouble receiving */
};

/*
 * The client interface
 */
class NetClient {
public:
	NetClient(const char *hostname, int port);
	virtual ~NetClient(void);
	int HostLookupFailed(void) { return (address == 0); }
	long SendMessage(BMessage *request, BMessage **reply);
	void SetTimeout(unsigned seconds) { timeout = seconds; };
	void SetRetransmissions(unsigned nretrans) { retrans = nretrans; }
private:
	int address;
	int port;
	unsigned long tid;
	int timeout;
	int retrans;
	int sock;
	char *replydata;
};

/*
 * The server interface
 */
class NetServer {
public:
	NetServer(int port);
	virtual ~NetServer(void);
	long ReceiveMessage(BMessage **message);
	long SendReply(BMessage *message);
	unsigned long CurrentTransactionID(void) { return (tid); }
private:
	unsigned long tid;
	int sock;
	char *requestdata;
	struct sockaddr_in address;
};

#endif /* _NET_MESSAGE_H */
