一
一只菜鸟
Unregistered / Unconfirmed
GUEST, unregistred user!
在一个单元中的定义是这样的
type
TINNER_OBJ = class(TObject)
private
protected
public
FSocket: TCustomWinSocket;
GateId: Cardinal;
WindowSize: integer;
GateName: string;
locPacketIn: TInnerPacket;
netPacketIn: TInnerPacket;
locPacketOut: TInnerPacket;
netPacketOUt: TInnerPacket;
isHead: boolean;
isReg: boolean;
LastActiveTime: TDateTime;
ConnectTime: TDateTime;
RecBuffer: TRecBuffer;
constructor Create(Socket:TCustomWinSocket);
destructor destroy ; override ;
function CreatePacket(const CommandId:cardinal):TInnerPacket;
procedure SendPacket(const rTInnerPacket:TInnerPacket);
function WriteMoToBuff(const rTInnerPacket: TInnerPacket):boolean;
procedure DelMtFromBuff(const SeqId:Cardinal);
procedure SendMt(const Mtb: TMtBuffer);
Function SBCToDBC(Str : String) : String;
end;
然后这样用法
procedure TMainForm.InnerServerSocketClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
AddMsgToMemo('内部接口连接成功');
try
Socket.Data := TINNER_OBJ.Create(Socket);
AddMsgToMemo('内部接口对象创建成功');
except
on E:exception do
begin
AddMsgToMemo('内部接口对象创建失败:' + e.Message);
end;
end;
end;
procedure TMainForm.InnerServerSocketClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
rInnerObj : TINNER_OBJ;
begin
if not Assigned(Socket.Data) then
begin
AddMsgToMemo('未创建内部接口对象');
Socket.Close;
Exit;
end;
try
rInnerObj := TINNER_OBJ(Socket.Data);
if rInnerObj.isHead then
begin
if Socket.ReceiveLength < sizeof(TInnerHead) then
exit;
ZeroMemory(@rInnerObj.netPacketIn,sizeof(TInnerPacket));
Socket.ReceiveBuf(rInnerObj.netPacketIn.Head,sizeof(TInnerHead));
rInnerObj.locPacketIn.Head.PackLen := ntohl(rInnerObj.netPacketIn.Head.PackLen);
rInnerObj.locPacketIn.Head.CmdId := ntohl(rInnerObj.netPacketIn.Head.CmdId);
rInnerObj.locPacketIn.Head.SeqId := ntohl(rInnerObj.netPacketIn.Head.SeqId);
…………
下面就不贴上来了
问题
rInnerObj 为何不用Create就可以用了,rInnerObj := TINNER_OBJ(Socket.Data);这句话是什么意思?Socket.Data这个属性怎么用法?
type
TINNER_OBJ = class(TObject)
private
protected
public
FSocket: TCustomWinSocket;
GateId: Cardinal;
WindowSize: integer;
GateName: string;
locPacketIn: TInnerPacket;
netPacketIn: TInnerPacket;
locPacketOut: TInnerPacket;
netPacketOUt: TInnerPacket;
isHead: boolean;
isReg: boolean;
LastActiveTime: TDateTime;
ConnectTime: TDateTime;
RecBuffer: TRecBuffer;
constructor Create(Socket:TCustomWinSocket);
destructor destroy ; override ;
function CreatePacket(const CommandId:cardinal):TInnerPacket;
procedure SendPacket(const rTInnerPacket:TInnerPacket);
function WriteMoToBuff(const rTInnerPacket: TInnerPacket):boolean;
procedure DelMtFromBuff(const SeqId:Cardinal);
procedure SendMt(const Mtb: TMtBuffer);
Function SBCToDBC(Str : String) : String;
end;
然后这样用法
procedure TMainForm.InnerServerSocketClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
AddMsgToMemo('内部接口连接成功');
try
Socket.Data := TINNER_OBJ.Create(Socket);
AddMsgToMemo('内部接口对象创建成功');
except
on E:exception do
begin
AddMsgToMemo('内部接口对象创建失败:' + e.Message);
end;
end;
end;
procedure TMainForm.InnerServerSocketClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
rInnerObj : TINNER_OBJ;
begin
if not Assigned(Socket.Data) then
begin
AddMsgToMemo('未创建内部接口对象');
Socket.Close;
Exit;
end;
try
rInnerObj := TINNER_OBJ(Socket.Data);
if rInnerObj.isHead then
begin
if Socket.ReceiveLength < sizeof(TInnerHead) then
exit;
ZeroMemory(@rInnerObj.netPacketIn,sizeof(TInnerPacket));
Socket.ReceiveBuf(rInnerObj.netPacketIn.Head,sizeof(TInnerHead));
rInnerObj.locPacketIn.Head.PackLen := ntohl(rInnerObj.netPacketIn.Head.PackLen);
rInnerObj.locPacketIn.Head.CmdId := ntohl(rInnerObj.netPacketIn.Head.CmdId);
rInnerObj.locPacketIn.Head.SeqId := ntohl(rInnerObj.netPacketIn.Head.SeqId);
…………
下面就不贴上来了
问题
rInnerObj 为何不用Create就可以用了,rInnerObj := TINNER_OBJ(Socket.Data);这句话是什么意思?Socket.Data这个属性怎么用法?