function IPSend(pBuf : pointer;
nSize : longint) : longint;
//封装在一个dll中的socket 发送函数.关键在于调用这个函数发送消息包
//以下是结构声明
type
DATA_MSG = record
Head: tagPureMTHead;
Data: array[0..MAX_MSG_DATA_LEN-1] of Byte;
end;
tagPureMTHead = record
nLen: DWORD;
nCmdID: DWORD;
nSenderID: DWORD;
nRecverID: DWORD;
nSequenceID: DWORD;
nStatus: DWORD;
end;
//一个线程中的函数
procedure TTCPSend.
Execute;
var
Buf : DATA_MSG;
MTBuf: MT_DATA_MSG;
SmsBuf: SMS_DATA_MSG;
DataLen : longint;
ErrorID : DWORD;
ReceiverID: DWORD;
sSendDirection: string;
begin
{ Place thread code here }
ThreadExitEvent.ResetEvent;
while (not self.Terminated)do
begin
//降低CPU占用率
//ThreadIsExitEvent.WaitFor(1);
//发送数据
DataLen := sizeof(DATA_MSG);
//没有数据
if not (MsgBufMng.WaitFor(m_SendID, 2000) = wrSignaled) then
Continue;
while MsgBufMng.GetMsgFromID(@Buf, DataLen, m_SendID)do
begin
DataLen := DATA_MSG((@Buf)^).HEAD.nLen;
//试添加
nCommandID := DATA_MSG((@Buf)^).HEAD.nCmdID;
nSequenceID := DATA_MSG((@Buf)^).HEAD.nSequenceID;
ReceiverID := DATA_MSG((@Buf)^).HEAD.nRecverID;
if loginType = ltMtServer then
begin
if ReceiverID = SENDER_ID_FROM_MT_SERVER then
//这里发送数据
ErrorID := IPSend(@Buf, DataLen) //发送数据
else
begin
DataLen := DataLen + sizeOf(tagPureSmsHead);
CopyMemory(@(MTBuf.MTHead), @(Buf.Head), SizeOf(Buf.Head));
MTBuf.MTHead.nLen := DataLen;
MTBuf.SmsHead.nLen := DataLen;
MTBuf.SmsHead.nCmdID := Buf.Head.nCmdID;
MTBuf.SmsHead.nStatus := Buf.Head.nStatus;
MTBuf.SmsHead.nSequenceID := Buf.Head.nSequenceID;
CopyMemory(@(MTBuf.Data), @(Buf.Data), SizeOf(Buf.Data));
ErrorID := IPSend(@MTBuf, DataLen);
//发送数据
end
{$else
}
ErrorID := IPSend(@Buf, DataLen);
//发送数据
{$endif}
end
else
if LoginType = ltSms then
begin
DataLen := DataLen - SizeOf(tagPureMTHead) + SizeOf(tagPureSmsHead);
SmsBuf.Head.nLen := DataLen;
SmsBuf.Head.nCmdID := Buf.Head.nCmdID;
SmsBuf.Head.nStatus := Buf.Head.nStatus;
SmsBuf.Head.nSequenceID := Buf.Head.nSequenceID;
CopyMemory(@(SmsBuf.Data), @(Buf.Data), SizeOf(Buf.Data));
ErrorID := IPSend(@SmsBuf, DataLen);
end;
//Synchronize(ShowSendState);
//发送失败,Socket断连,退出线程
if ErrorID <> 0 then
begin
ConnectError.SetEvent;
Break;
end
else
begin
if (nCommandID = CMD_MT_SHAKE_HAND) and not FTraceShakeHandSwitch then
else
begin
case LoginType of
ltMtServer:
begin
...
end;
ltSms:
begin
...
end;
end;
end;
end;
DataLen := sizeof(DATA_MSG);
//重要!恢复初始长度之用.
end;
//while 发送数据
end;
//while
ThreadExitEvent.SetEvent;
end;