#ifndef MSGHND_H #define MSGHND_H ///////////////////////////////////////////////////////////////////////// // msgHnd.h - message handler, intended for communication // // ver 1.0 between sockets // // // // Language: Visual C++, ver 6.0 // // Platform: Dell Dimension 8100, Windows 2000 Pro, SP1 // // Application: CSE691/791 help // // Author: Jim Fawcett, CST 2-187, Syracuse University // // (315) 443-3948, jfawcett@twcny.rr.com // ///////////////////////////////////////////////////////////////////////// /* Module Operations ================= This module provides a message class that supports the use of messages in a sockets-based, message-passing communication system. Public Interface ================ std::string aBody = "some very important message"; msgType type = message::text; message myMsg(type,body); // construct message message fromForeign(str); // promote string int size = myMsg.size(); // total size of message std::string myBody = myMsg.body(); // body string msgType myType = myMsg.type(); // type enumeration std::string typeStr = myMsg.typeStr(); // string version of type std::string strRep = myMsg.serialize(); // string version of msg int moreBytes = message::bytesToRead(); // # of bytes after head */ ///////////////////////////////////////////////////////////////////////// // Build Process // ///////////////////////////////////////////////////////////////////////// // Required Files: // // msgHnd.h, msgHnd.cpp, xmlTran.h, xmlTran.cpp, // // sysUtils.h, sysUtils.cpp // // // // Compiler command: // // cl /GX /DTEST_MSGHND msgHnd.cpp, xmlTran.cpp // // // ///////////////////////////////////////////////////////////////////////// /* Maintenance History =================== ver 1.0 : 12 Nov 10 */ #include #include "xmlTran.h" // ///////////////////////////////////////////////////////////////////////// // message class declaration // ///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////// // message format: // /////////////////////////////////////////////////////// // // // <#M type="_" size="_______">xxx....xxx // // // // <-- headSize = 27 bytes ---> // // // /////////////////////////////////////////////////////// class message { public: enum msgType { id, // client id text, // text for server to save command, // command, e.g., shutdown request, // client request as text file // file name }; enum { // number of bytes a socket receive thread will headSize=28 // read to detect the message type and discover }; // the number of remaining bytes to read message(msgType type, const std::string &myBody); message(); explicit message(const std::string &s); ~message(); msgType type(); std::string typeStr(); std::string body(); unsigned int size(); std::string serialize(); static unsigned int bytesToRead(const std::string &s); private: msgType _type; xmlElem _msg; unsigned int _size; msgType StrToType(const std::string &s); unsigned int StrToSize(const std::string &s); }; inline message::msgType message::type() { return _type; } inline std::string message::serialize() { return (std::string)_msg; } inline unsigned int message::size() { return _size; } inline std::string message::body() { return _msg.body(); } #endif