-marshall &
-operator<<(marshall &m, uint8_t x) {
- m.rawbyte(x);
- return m;
-}
-
-marshall &
-operator<<(marshall &m, uint16_t x) {
- x = htons(x);
- m.rawbytes((char *)&x, 2);
- return m;
-}
-
-marshall &
-operator<<(marshall &m, uint32_t x) {
- x = htonl(x);
- m.rawbytes((char *)&x, 4);
- return m;
-}
-
-marshall & operator<<(marshall &m, int x) { return m << (unsigned int) x; }
-marshall & operator<<(marshall &m, char x) { return m << (uint8_t)x; }
-marshall & operator<<(marshall &m, bool x) { return m << (uint8_t)x; }
-marshall & operator<<(marshall &m, short x) { return m << (unsigned short) x; }
-marshall & operator<<(marshall &m, uint64_t x) { return m << (uint32_t)(x>>32) << (uint32_t)x; }
-
-marshall &
-operator<<(marshall &m, const std::string &s) {
- m << (unsigned int) s.size();
- m.rawbytes(s.data(), s.size());
- return m;
-}
-
-void marshall::pack_req_header(const request_header &h) {
- int saved_sz = index_;
- //leave the first 4-byte empty for channel to fill size of pdu
- index_ = sizeof(rpc_sz_t);
- *this << h.xid << h.proc << h.clt_nonce << h.srv_nonce << h.xid_rep;
- index_ = saved_sz;
-}
-
-void marshall::pack_reply_header(const reply_header &h) {
- int saved_sz = index_;
- //leave the first 4-byte empty for channel to fill size of pdu
- index_ = sizeof(rpc_sz_t);
- *this << h.xid << h.ret;
- index_ = saved_sz;
-}
-
-void
-unmarshall::unpack(int *x)
-{
- (*x) = (rawbyte() & 0xff) << 24;
- (*x) |= (rawbyte() & 0xff) << 16;
- (*x) |= (rawbyte() & 0xff) << 8;
- (*x) |= rawbyte() & 0xff;
-}
-
-// take the contents from another unmarshall object
-void
-unmarshall::take_in(unmarshall &another)
-{
- if(buf_)
- free(buf_);
- another.take_buf(&buf_, &sz_);
- index_ = RPC_HEADER_SZ;
- ok_ = sz_ >= RPC_HEADER_SZ?true:false;
-}
-
-inline bool
-unmarshall::ensure(size_t n) {
- if (index_+n > sz_)
- ok_ = false;
- return ok_;
-}
-
-unsigned int
-unmarshall::rawbyte()
-{
- if (!ensure(1))
- return 0;
- return buf_[index_++];
-}
-
-void
-unmarshall::rawbytes(std::string &ss, size_t n)
-{
- VERIFY(ensure(n));
- ss.assign(buf_+index_, n);
- index_ += n;
-}
-
-unmarshall &
-operator>>(unmarshall &u, bool &x)
-{
- x = (bool)u.rawbyte();
- return u;
-}
-
-unmarshall &
-operator>>(unmarshall &u, unsigned char &x)
-{
- x = (unsigned char)u.rawbyte();
- return u;
-}
-
-unmarshall &
-operator>>(unmarshall &u, char &x)
-{
- x = (char)u.rawbyte();
- return u;
-}
-
-unmarshall &
-operator>>(unmarshall &u, unsigned short &x)
-{
- x = (u.rawbyte() & 0xff) << 8;
- x |= u.rawbyte() & 0xff;
- return u;
-}
-
-unmarshall &
-operator>>(unmarshall &u, short &x)
-{
- x = (u.rawbyte() & 0xff) << 8;
- x |= u.rawbyte() & 0xff;
- return u;
-}
-
-unmarshall &
-operator>>(unmarshall &u, unsigned int &x)
-{
- x = (u.rawbyte() & 0xff) << 24;
- x |= (u.rawbyte() & 0xff) << 16;
- x |= (u.rawbyte() & 0xff) << 8;
- x |= u.rawbyte() & 0xff;
- return u;
-}
-
-unmarshall &
-operator>>(unmarshall &u, int &x)
-{
- x = (u.rawbyte() & 0xff) << 24;
- x |= (u.rawbyte() & 0xff) << 16;
- x |= (u.rawbyte() & 0xff) << 8;
- x |= u.rawbyte() & 0xff;
- return u;
-}
-
-unmarshall &
-operator>>(unmarshall &u, unsigned long long &x)
-{
- unsigned int h, l;
- u >> h;
- u >> l;
- x = l | ((unsigned long long) h << 32);
- return u;
-}
-
-unmarshall &
-operator>>(unmarshall &u, std::string &s)
-{
- unsigned sz;
- u >> sz;
- if(u.ok())
- u.rawbytes(s, sz);
- return u;
-}
-
-bool operator<(const sockaddr_in &a, const sockaddr_in &b){
- return ((a.sin_addr.s_addr < b.sin_addr.s_addr) ||
- ((a.sin_addr.s_addr == b.sin_addr.s_addr) &&
- ((a.sin_port < b.sin_port))));
-}
-
-/*---------------auxilary function--------------*/
-void
-make_sockaddr(const char *hostandport, struct sockaddr_in *dst){
-
- char host[200];
- const char *localhost = "127.0.0.1";
- const char *port = index(hostandport, ':');
- if(port == NULL){
- memcpy(host, localhost, strlen(localhost)+1);
- port = hostandport;
- } else {
- memcpy(host, hostandport, port-hostandport);
- host[port-hostandport] = '\0';
- port++;
- }
-
- make_sockaddr(host, port, dst);