Click here to Skip to main content
15,886,046 members
Articles / Desktop Programming / MFC

CSPServer, State-based Protocol Server Class

Rate me:
Please Sign up or sign in to vote.
4.88/5 (14 votes)
11 Mar 20038 min read 145.1K   1.4K   71  
Class framework for creating client/server protocol servers
//***********************************************************************
// (c) Copyright 1999-2003 Santronics Software, Inc. All Rights Reserved.
//***********************************************************************
// File Name : spserver.h
// Subsystem : Protocal Server
// Date      : 03/03/2003
// Author    : Hector Santos, Santronics Software, Inc.
// VERSION   : 1.00P
//
// Revision History:
// Version  Date      Author  Comments
// -------  --------  ------  -------------------------------------------
// v1.00P   03/03/03  HLS     Public Release version (non-SSL version)
//***********************************************************************

#ifndef __SPSERVER_H
#define __SPSERVER_H

#include <afx.h>
#include <process.h>
#include <tchar.h>
#include "thread.h"
#include "socketio.h"


////////////////////////////////////////////////////////////////////
#define MAX_SEND_BUFFER_LEN 4096
#define MAX_SEND_OUTPUT     4096
#define SPCMD(cls, cmd, func) \
         {cmd, (BOOL (CSPServer::*)(char *))(cls::func)}
////////////////////////////////////////////////////////////////////

/**
 * This class implements a state-based simple protocol server.
 *
 * A simple protocol server is essential for protocols such as SMTP, POP3,
 * and more.  This class handles all the socket related communications
 * So that one may focus on the implementation of the protocol itself.
 *
 * Usage requires the creation of a child class that implements the STATE
 * functions for the protocol.
 */

class CSPServer: public CThread {
public:
  /**
   * Used internally in CSPServer
   * Structure maintains list of protocol commands used
   * in simple protocol servers.
   */
  struct TSPDispatch {
    char *cmd;
    BOOL (CSPServer::*f)(char *args);
  };
  CSPServer(CSocketIO *s, TSPDispatch *dispatch, long maxconnect = 25);
  ~CSPServer();
  void ProcessCommand(char *cmdline);
  BOOL GetSubCommand(char *s, DWORD len){return GetCommand(s, len);}
  void SetTimeoutSeconds(const DWORD secs) { dwTimeoutSeconds = secs; }
  DWORD GetTimeoutSeconds() { return dwTimeoutSeconds; }
  virtual void DumpReceivedByte(int b) {}
protected:
  virtual void Go();
protected:
  CSocketIO *Control;

  virtual void SendWelcome() {}
  virtual BOOL CheckAbort();
  virtual BOOL PreprocessLine(char *s) { return FALSE; }
  virtual void SessionTrace(const char *szTag, const char *s) {}
  virtual void SendCommandError(const char *cmdline);
  virtual void Cleanup() {}
  virtual BOOL QueryAllowTimeoutDisconnect() { return TRUE; }
  virtual void OnWaitReceivedDataAbandoned() {}
  virtual void OnReceiveByteError(const int error) {};

  void SetDispatchTable(TSPDispatch *dispatch) { Dispatch = dispatch; }
  const char *GetCurrentCommandName() { return commandname; }
  BOOL Send(const TCHAR *format, ...);

  BOOL Done;
  DWORD LastCommandTime;
  const char *commandname;
  DWORD dwTimeoutSeconds;

private:
  TSPDispatch *Dispatch;
  BOOL GetCommand(char *s, DWORD len);

  long MaxConnections;
};

#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions