Borland Socket Service

saoren 2000-12-23 03:40:00
这段时间,无事可做,拿SocketService的源程序来看,
但是好多不通不懂,望大虾们一起研究研究
分不多,因为我觉得如果研究出来东西,分数是小事。
当然可以加分。



//最重要的处理。每个客户端连入的ServerSocket中,在这里进行同步的处理。
procedure TSocketDispatcherThread.ClientExecute;
var
Data: IDataBlock;
msg: TMsg;
Obj: ISendDataBlock;
Event: THandle;
WaitTime: DWord;
begin
CoInitialize(nil);
try
Synchronize(AddClient);
//Create New Server Socket transport.TSocketTransport.Create;
FTransport := CreateServerTransport;
try
//?????????????????
Event := FTransport.GetWaitEvent;
PeekMessage(msg, 0, WM_USER, WM_USER, PM_NOREMOVE);
GetInterface(ISendDataBlock, Obj);
//?????????????????????
if FRegisteredOnly then
FInterpreter := TDataBlockInterpreter.Create(Obj, SSockets) else
FInterpreter := TDataBlockInterpreter.Create(Obj, '');
try
Obj := nil;
if FTimeout = 0 then
WaitTime := INFINITE else
WaitTime := 60000;
//当 thread not terminated and FTransport not connected is keepconnection.
//在这里,客户端的要求都在这里进行处理。
while not Terminated and FTransport.Connected do
try
case MsgWaitForMultipleObjects(1, Event, False, WaitTime, QS_ALLEVENTS) of
WAIT_OBJECT_0:
begin
//说明此事件已经处理过了,将其reset nil.
WSAResetEvent(Event);
//接收到 Client port data
Data := FTransport.Receive(False, 0);
if Assigned(Data) then
begin
FLastActivity := Now;
//注1(下面的VCL的处理)
//??????????????处理些什么FInterpreter.InterpretData(Data)
FInterpreter.InterpretData(Data);
//????????????????????????
Data := nil;
FLastActivity := Now;
end;
end;
//??????????????????????????????????????????????
WAIT_OBJECT_0 + 1:
while PeekMessage(msg, 0, 0, 0, PM_REMOVE) do
DispatchMessage(msg);
//??????????????????????????????????????????????
WAIT_TIMEOUT:
if (FTimeout > 0) and ((Now - FLastActivity) > FTimeout) then
FTransport.Connected := False;
end;
except
//except :Client stop
FTransport.Connected := False;
end;
finally
FInterpreter.Free;
FInterpreter := nil;
end;
finally
FTransport := nil;
end;
finally
CoUninitialize;
Synchronize(RemoveClient);
end;
end;


//这个注1
procedure TDataBlockInterpreter.InterpretData(const Data: IDataBlock);
var
Action: Integer;
begin
Action := Data.Signature;
if (Action and asMask) = asError then DoException(Data);
try
case (Action and asMask) of
asInvoke: DoInvoke(Data);
asGetID: DoGetIDsOfNames(Data);
asCreateObject: DoCreateObject(Data);
asFreeObject: DoFreeObject(Data);
asGetServers: DoGetServerList(Data);
asGetAppServers: DoGetAppServerList(Data);
else
if not DoCustomAction(Action and asMask, Data) then
raise EInterpreterError.CreateResFmt(@SInvalidAction, [Action and asMask]);
end;
except
on E: Exception do
begin
Data.Clear;
WriteVariant(E.Message, Data);
Data.Signature := ResultSig or asError;
FSendDataBlock.Send(Data, False);
end;
end;
end;
...全文
325 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
saoren 2000-12-26
  • 打赏
  • 举报
回复

//SocketThread是由TSocketDispatcherThread.Create 出来的,
//所以。。。。 ||
//即说TSocketDispatcher中的ServerSocket端是由它来完成。
//好,TSocketDispatcher的对象和TSocketDispatcherThread连上了,
//在这里,我们用控件TServerSocket,和现在用的TSocketDispatcher.Create出来的ServerSocket不一样了。嘻嘻。
procedure TSocketDispatcher.GetThread(Sender: TObject;
ClientSocket: TServerClientWinSocket;
var SocketThread: TServerClientThread);
begin
SocketThread := TSocketDispatcherThread.Create(False, ClientSocket,
InterceptGUID, Timeout, SocketForm.RegisteredAction.Checked);
end;


constructor TSocketDispatcherThread.Create(CreateSuspended: Boolean;
ASocket: TServerClientWinSocket; const InterceptGUID: string; Timeout: Integer;
RegisteredOnly: Boolean);
begin
//这里面的private object,all for COM
FInterceptGUID := InterceptGUID;
FTimeout := EncodeTime(Timeout div 60, Timeout mod 60, 0, 0);
FLastActivity := Now;
FRegisteredOnly := RegisteredOnly;
inherited Create(CreateSuspended, ASocket);
//if CreateSuspended =false then Execute "ClientExecute" module
//else no else
end;
//哈哈,我找到它们类之间的关系了,哈哈哈哈哈哈
saoren 2000-12-26
  • 打赏
  • 举报
回复
{建立Server--Client对话的Socket}
type
TSocketDispatcher = class(TServerSocket)

{为每个端口建立一个线程进行服务。}
type
TSocketDispatcherThread = class(TServerClientThread, ISendDataBlock)


{TService 能够加入到NT中的自启动的服务中,
是一个32位的服务程序,它能够当系统启动时加载通过SCM(Service Controls Manager)}
//TSocketService类是为在NT中建立的。
//所以在它的OnStart中,PostMessage(main.handle,初始化,)
//在OnStop中,PostMessage(main.Handle,WM_CLOSE);
TSocketService = class(TService)
protected
procedure Start(Sender: TService; var Started: Boolean);
procedure Stop(Sender: TService; var Stopped: Boolean);
public

//它就不说了。
type TSocketForm=class(TForm)

{
所以在Borland Socket Service中最重要的是:
TSocketDispatcherThread类里面的东西。
而其中的东西就值得我们去敲啊敲啊敲啊敲啊敲啊敲啊敲啊敲啊敲啊敲啊敲敲
}
saoren 2000-12-25
  • 打赏
  • 举报
回复
怎么不见有人来的?
大虾们不要有保留啊。
saoren 2000-12-25
  • 打赏
  • 举报
回复
它的大体流程大概是这样:(个人意见)
用TServerSocket和TClientSocket进行通信。
默认了一个211的端口。
为每一个端口,它起用了一个ServerSocket去响应它端口的事,
这个一个差不多是:CHAT的程序,不过它加入线程和COM的interface.
有点像而已。
而我想知道的东西,就是上面的function中打????的意思。
许多API不知其意,不知其所以。唉。
comanche 2000-12-25
  • 打赏
  • 举报
回复
1. 这个我上次看过,这个InterpretData有特点, 大概是在 Data 发送前后让另一监听程序作加密发送。在ScktSrvr上不是有个指定Interpret GUID的Edit?但用了后客户机部分就很烦,还要解密数据,我看了半天也不会作!!你可以参考一下Delphi Demo目录下的 Interpret例子

2. 这个 peekmessage 和 dispatchmessage 我觉得是 WM_USER的消息会发到这,所以收到后要重新分发,上次看时我就这样认为,要不然没得解释
saoren 2000-12-25
  • 打赏
  • 举报
回复
to airhorse
你说的是:MsgWaitForMultipleObjects(1, Event, False, WaitTime, QS_ALLEVENTS)
吧?
saoren 2000-12-25
  • 打赏
  • 举报
回复
FInterpreter.InterpretData(Data);
是做什么用的,看了帮助,没什么用,它怎么处理数据的,
还有那个peekmessage和DispatchMessage(msg) function API不知所以??
airhorse 2000-12-25
  • 打赏
  • 举报
回复
是一个 等待事件。

为了是的没有客户端通讯时,不至于太忙(即将计算机挂起);
saoren 2000-12-23
  • 打赏
  • 举报
回复
Interprets a call received from a remote application server.

procedure InterpretData(const Data: IDataBlock);

Description

Call InterpretData to respond to a COM interface call that is marshaled to a data block. The Data parameter specifies an IDataBlock interface that can be used to read the marshaled version of the message.

InterpretData reads the call, interprets it as an IDispatch call, and performs the appropriate actions (such as invoking a local COM interface).

5,930

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧