Newsgroups : Borland : borland.public.delphi.internet.winsock : 2006 Nov : How can I find out what type of record I'm receiving prior to cracking it open a

www.cryer.info
Managed Newsgroup Archive

How can I find out what type of record I'm receiving prior to cracking it open a

Subject:How can I find out what type of record I'm receiving prior to cracking it open a
Posted by:"Clay Shannon" (blac..@redgreen.com)
Date:Fri, 10 Nov 2006 18:37:59

My app is going to receive various messages from a realtime (Linux) system
over a socket.

Based on the message, different record structures with different amounts and
types of data members will be passed.


For example, some record types my Delphi app will recieve are:

type
  PActivateBOFSort = ^TActivateBOFSort;

  TActivateBOFSort = packed record
    OpCode: Integer;
    Carrier: Integer;
    BOFSortLabel: string[64]; //Would there be an advantage in making this
array[0..63] of Char?
    WorkstationOffset: Integer;
  end;

type
  PGenericMsg = ^TGenericMsg;

  TGenericMsg = packed record
    OpCode: Integer;
    MsgTableID: Integer;
    Param1: String[64];
    Param2: String[64];
    Param3: String[64];
  end;

type
  PItemConfirmed = ^TItemConfirmed;

  TItemConfirmed = packed record
    OpCode: Integer;
    Sort: Integer;
    Drop: Integer;
    Carrier: Integer;
    SequenceInSort: Integer;
    WorkstationOffset: Integer;
    CtrlNum: String[9];
    StoreID: Integer;
    ChuteID: Integer;
  end;

type
  PItemDetectDiagnosticTest = ^TItemDetectDiagnosticTest;

  TItemDetectDiagnosticTest = packed record
    OpCode: Integer;
    WorkstationOffset: Integer;
    CarrierID: Integer;
    Param1: Integer;
    Param2: Integer;
    Param3: Integer;
    Param4: Integer;
    Param5: Integer;
    Param6: Integer;
    Param7: Integer;
    Param8: Integer;
    Param9: Integer;
  end;

The only data member all records have in common is the first one, OpCode,
which, like taking the path less traveled, makes all the difference as to
how to deal with the code.

My question/problem is, How do I receive any record (regardless of record
type), and "unpack" its OpCode, so that I can identify it and then send it
off to the right "place" for processing? I understand that once I know which
type of record I'm dealing with I can do something like this:

var
  X: PItemDetectDiagnosticTest;
  iWorkstationOffset: Integer;
begin
  X := AllocMem(SizeOf(TItemDetectDiagnosticTest));
  Socket.ReceiveBuf(X^, SizeOf(TItemDetectDiagnosticTest));
  iWorkstationOffset := X^.WorkstationOffset;
  {...}
end;

Here is my test code mimicking the RealTime sending me the records:

procedure TFormFollowsFunction.SendActiv8BOFSortRecord;
var
  X: PActivateBOFSort;
begin
  X := AllocMem(SizeOf(TActivateBOFSort));
  X^.OpCode := ACTIVATE_BOF_SORT;
  X^.Carrier := StrToIntDef(edt1.Text, 0);
  X^.BOFSortLabel := edt2.Text;
  X^.WorkstationOffset := StrToIntDef(edt3.Text, 0);
  ClientSocket.Socket.SendBuf(X^, SizeOf(TActivateBOFSort));
end;

BTW, I am using the deprecated TClientSocket component w. BDS2006
--
Download my historical nonfiction and/or satirical (etc.) fiction books free
from here:
http://www.lulu.com/blackbirdcraven

Replies:

www.cryer.info
Managed Newsgroup Archive