#ifndef _TECDDECLIENT_H
#define _TECDDECLIENT_H
/*  Project Tec3270

	 Copyright © 1998-2005. All Rights Reserved.

	 SUBSYSTEM:    WHllApi.dll library
	 FILE:         tecwhllclient.h
	 AUTHOR:       Alcindo Ferreira Filho


	 OVERVIEW
	 ========
	 Source file for defining Class TecWHllClient
*/

#include "alffddec.h"   // ALFFDdeClient class
#include "alfflogf.h"   // ALFFLogFile class
#include "whllapi.h"

/* Buffer Layout for exchanging data with server:
 *
 *   Field Name:  Comm  Ret   Extd  Data      Data
 *                Code  Code  Error Length    Buffer Bytes
 *               |LL,HH|LL,HH|LL,HH|LL,HH|___________..........._|
 *               |-----|-----|-----|-----|-----------------------|
 *   Byte Pos.:  0     2     4     6     8                       n
 */

// MAX DATA BUFFER SIZE
#define  MAXBUFFERSIZE       4096

// DDE Buffer Header Length
#define  DDEHDRBUFFERLEN     8
#define  CCODEOFFSET         0
#define  RETCODEOFFSET       2
#define  EXTDERROROFFSET     4
#define  DATALENOFFSET       6
#define  DATAOFFSET          8

// Length of RETURNED PARMS LEN:
#define  RETURNEDPARMSLEN    8

//  Next are used on SETSESSIONPARAMETERS Function
//  TRACE = ON/OFF           [1]
#define  TRACEOFFSET         0     //Related to Data Area
//  PAUSE = FPAUSE/IPAUSE    [1]
#define  PAUSEOFFSET         1	  //Related to Data Area
//  STREOT = STRLEN / STREOT [1]
#define  STREOTOFFSET        2     //Related to Data Area
//  EOTChar = EOT=c          [1]
#define  EOTCHAROFFSET       3     //Related to Data Area
//  TWAIT / LWAIT / NWAIT    [1]
#define  WAITOFFSET          4     //Related to Data Area
//  RESERVED                 [3]

//  Next are used on PAUSE Function
//  PS has changed flag      [1]
#define  PSCHANGEDOFFSET     0
//  RESERVED                 [7]

// CONNECTPS FLAG:
#define CONNECTPSCHECK       0x7FFF
#define CONNECTPSDONE        0x7FFE

// SENDKEY   FLAG:
#define SENDKEYFIRST         0x7EEE
#define SENDKEYNEXT          0x7EED

// WaitPeriod:
#define TWAIT   1
#define LWAIT   2
#define NWAIT   3

// PS Share
#define PS_WRITE_SUPER  1
#define PS_WRITE_WRITE  2
#define PS_WRITE_READ   3
#define PS_WRITE_NONE   4
#define PS_SUPER_WRITE  5
#define PS_READ_WRITE   6
struct TecWHLLAppParms
 {
  TecWHLLAppParms() : appId(NULL), appConnected(0), strEOT(false), eotChar(NULL),
                      searchAll(true), searchFwd(true), noAttrb(true), fPause(true),
                      xmitFileQuiet(true), timeOut('0'), escapePFKChar('@'),
                      autoReset(true), waitPeriod(TWAIT), traceOn(false),
                      noEAB(true), eabXlat(false), conLog(true), newOIA(true),
                      cfgSize(true), noDisplay(false), psShare(PS_WRITE_NONE)
                      { keyWord[0] = NULL; }
  HCONV appId;          // Appd Id of the session owner
  int  appConnected;    // Number of apps connected to this session
  bool strEOT;          // byString parm has an implicit length (NULL terminator is the default)
  unsigned char eotChar;   // terminator of byString if the default is not choosed
  bool searchAll;       // Search Field option (true = all field, false=from position)
  bool searchFwd;       // Search Field direction (true = forward, false = backward)
  bool noAttrb;         // 3270 Attribute (true = translate to space, false = don't translate)
  bool fPause;          // Pause (wait) option (true = wait until end of interval, false = don't wait)
  bool xmitFileQuiet;
  unsigned char timeOut;
  char escapePFKChar;
  bool autoReset;
  unsigned char  waitPeriod;
  bool traceOn;        // Trace option (true = trace every command processed, false = don't trace)
  bool noEAB;
  bool eabXlat;
  bool conLog;         // Tec3270 window position (true = maintain in background, false= bring to top)
  bool newOIA;         // OIA copy option (true = send in ASCII, false = send in PC3270 format)
  bool cfgSize;
  bool noDisplay;
  int  psShare;
  char keyWord[9];
};

class TecWHllClient : public ALFFDdeClient
{
  public:
     TecWHllClient(HINSTANCE dllInstance, ALFFDate::DateFormat fmt);
     ~TecWHllClient();
     WORD WHllCommand(LPWORD command, LPSTR buffer,
                     LPWORD bufferLen, LPWORD returnCode);
	  bool CancelCurrentTransaction();
	  void SetCancelBlockingCall(bool cancel) {  cancelBlocking = cancel; }
     bool IsSetCancelBlocking() { return cancelBlocking; }
     WORD WHllGetExtendedError() {return extendedError;}
	  bool WHllCleanup();
	  WORD GetVersion() { return 0x0101;}
	  char* GetDescription() { return wHllDescription; }

  protected:
     bool  ExecuteOnServer(LPWORD lpwCommand, LPSTR lpbyString,
									LPWORD lpwLength, LPWORD lpwReturnCode);
     bool  GetResultOnServer(LPWORD lpwCommand, LPSTR lpbyString,
                             LPWORD lpwLength, LPWORD lpwReturnCode);
     void  SetExtendedError(WORD error) {extendedError = error;}
     bool  StartAdvisePSChanges();
     void  StopAdvisePSChanges();
     bool  StartHLLTrace(bool traceON);
     void  NotifyDisconnect(HCONV appId);
     char* wHllDescription;
     HSZ   itemWHllCommand;
     bool  hasParms;
     bool  receiveString;
     long  commandCount;
     bool  cancelBlocking;

     //WHLLAPI Session Options & Controls
     ALFFLogFile*       	pTrcFile;
     HINSTANCE    			wHllInstance;
     ALFFDate::DateFormat 	dateFormat;
     TecWHLLAppParms*  		wHllAppOptions;
     WORD  						extendedError;
     WORD               	currentCmd;
     unsigned char* 			hddeBuffer;
};

#endif
