| 1 |
// protocol.h -*- C++ -*- socket library |
| 2 |
// Copyright (C) 1992,1993,1994 Gnanasekaran Swaminathan <gs4t@virginia.edu> |
| 3 |
// |
| 4 |
// Permission is granted to use at your own risk and distribute this software |
| 5 |
// in source and binary forms provided the above copyright |
| 6 |
// notice and this paragraph are preserved on all copies. |
| 7 |
// This software is provided "as is" with no express or implied warranty. |
| 8 |
// |
| 9 |
// Version: 17Oct95 1.10 |
| 10 |
|
| 11 |
#ifndef PROTOCOL_H |
| 12 |
#define PROTOCOL_H |
| 13 |
|
| 14 |
#include <sockinet.h> |
| 15 |
|
| 16 |
class protocol: public iosockstream { |
| 17 |
public: |
| 18 |
enum p_name { |
| 19 |
nil = 0, |
| 20 |
tcp = sockbuf::sock_stream, |
| 21 |
udp = sockbuf::sock_dgram |
| 22 |
}; |
| 23 |
|
| 24 |
class protocolbuf: public sockinetbuf { |
| 25 |
private: |
| 26 |
protocol::p_name pn; |
| 27 |
|
| 28 |
int bind (sockAddr& sa) { return sockbuf::bind (sa); } |
| 29 |
int connect (sockAddr& sa) { return sockbuf::connect (sa); } |
| 30 |
|
| 31 |
public: |
| 32 |
protocolbuf (sockbuf& si): sockinetbuf (si), pn (protocol::nil) {} |
| 33 |
protocolbuf (protocol::p_name pname) |
| 34 |
: sockinetbuf ((sockbuf::type) pname, 0), pn (pname) {} |
| 35 |
|
| 36 |
|
| 37 |
int bind () { serve_clients (); return 0; } |
| 38 |
int connect (); |
| 39 |
int connect (unsigned long addr); |
| 40 |
int connect (const char* host); |
| 41 |
int connect (const char* host, int portno); |
| 42 |
|
| 43 |
const char* protocol_name () const; |
| 44 |
|
| 45 |
virtual void serve_clients (int portno = -1) = 0; |
| 46 |
virtual const char* rfc_name () const = 0; |
| 47 |
virtual const char* rfc_doc () const = 0; |
| 48 |
}; |
| 49 |
|
| 50 |
protocol (): ios (0) {} |
| 51 |
}; |
| 52 |
|
| 53 |
inline const char* protocol::protocolbuf::protocol_name () const |
| 54 |
{ |
| 55 |
char* ret = ""; |
| 56 |
if (pn == protocol::tcp) |
| 57 |
ret = "tcp"; |
| 58 |
if (pn == protocol::udp) |
| 59 |
ret = "udp"; |
| 60 |
return ret; |
| 61 |
} |
| 62 |
|
| 63 |
inline int protocol::protocolbuf::connect () |
| 64 |
{ |
| 65 |
if (pn != protocol::nil) |
| 66 |
return sockinetbuf::connect (localhost (), rfc_name (), protocol_name ()); |
| 67 |
return -1; |
| 68 |
} |
| 69 |
|
| 70 |
inline int protocol::protocolbuf::connect (unsigned long addr) |
| 71 |
// addr is in host byte order |
| 72 |
{ |
| 73 |
if (pn != protocol::nil) |
| 74 |
return sockinetbuf::connect (addr, rfc_name (), protocol_name ()); |
| 75 |
return -1; |
| 76 |
} |
| 77 |
|
| 78 |
inline int protocol::protocolbuf::connect (const char* host) |
| 79 |
{ |
| 80 |
if (pn != protocol::nil) |
| 81 |
return sockinetbuf::connect (host, rfc_name (), protocol_name ()); |
| 82 |
return -1; |
| 83 |
} |
| 84 |
|
| 85 |
inline int protocol::protocolbuf::connect (const char* host, int portno) |
| 86 |
{ |
| 87 |
if (pn != protocol::nil) |
| 88 |
return sockinetbuf::connect (host, portno); |
| 89 |
return -1; |
| 90 |
} |
| 91 |
|
| 92 |
#endif // PROTOCOL_H |