Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

CSPServer, State-based Protocol Server Class

, 11 Mar 2003
Class framework for creating client/server protocol servers
cspserver_src.zip
spserver
file_id.diz
SampleServer.dsp
SampleServer.dsw
//***********************************************************************
// (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

Share

About the Author

hector santos

United States United States
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 12 Mar 2003
Article Copyright 2003 by hector santos
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid