📄 dxp2pservercore.pas
字号:
unit DXP2PServerCore;
interface
///////////////////////////////////////////////////////////////////////////////
// Component: TDXP2PServerCore
// Author: G.E. Ozz Nixon Jr. (staff@bpdx.com)
// ========================================================================
// Source Owner: DX, Inc. 1995-2003
// Copyright: All code is the property of DX, Inc. Licensed for
// resell by Brain Patchwork DX (tm) and part of the
// DX (r) product lines, which are (c) 1999-2003
// DX, Inc. Source may not be distributed without
// written permission from both Brain Patchwork DX,
// and DX, Inc.
// License: (Reminder), None of this code can be added to other
// developer products without permission. This includes
// but not limited to DCU's, DCP's, DLL's, OCX's, or
// any other form of merging our technologies. All of
// your products released to a public consumer be it
// shareware, freeware, commercial, etc. must contain a
// license notification somewhere visible in the
// application.
// Example is Internet Explorer - Help->About screen
// shows the licensed code contained in the application.
// Code Version: (4th Generation Code)
// ========================================================================
// Description: *Example*
// ========================================================================
///////////////////////////////////////////////////////////////////////////////
uses
Dialogs,
SysUtils,
DXSock,
Classes,
DXServerCore;
{$I dxsock.def}
type
P2PTSimpleEvent = procedure(ClientThread: TDXServerThread) of object;
P2PTBasicEvent = procedure(ClientThread: TDXServerThread; Parm: string) of object;
P2PTComplexEvent = procedure(ClientThread:TDXServerThread;Parm1,Parm2:string) of object;
P2PTOtherEvent = procedure(ClientThread: TDXServerThread; Command: string; Parm: string; var Handled: Boolean) of object;
// DXP2PServerCore is a proprietary protocol established by
// Brain Patchwork DX, LLC.
//
//
//
// Summary
// A proprietary protocol by Brain Patchwork DX, LLC.
TDXP2PServerCore = class(TDXServerCore)
private
fOnCommandTimeout:P2PTSimpleEvent;
fOnCommandAUTH: P2PTComplexEvent;
fOnCommandQUERY: P2PTBasicEvent;
fOnCommandPING: P2PTSimpleEvent;
fOnCommandPONG: P2PTSimpleEvent;
fOnCommandOther: P2PTOtherEvent;
fEventArray:TList;
fiTimeout:Cardinal;
protected
Procedure SetOnCommandPING(value: P2PTSimpleEvent);
Procedure SetOnCommandPONG(value: P2PTSimpleEvent);
Procedure SetOnCommandQUERY(value: P2PTBasicEvent);
Procedure SetOnCommandAUTH(value: P2PTComplexEvent);
public
{$IFDEF OBJECTS_ONLY}
constructor Create;
{$ELSE}
constructor Create(AOwner:TComponent); override;
{$ENDIF}
destructor Destroy; override;
procedure SayHello(ClientThread:TDXServerThread;Header,MOTD:TStream);
procedure SayGoodbye(ClientThread:TDXServerThread;Footer:TStream);
procedure ProcessSession(ClientThread:TDXServerThread);
Procedure AddBasicEvent(Command:String;EventProc:P2PTBasicEvent);
Procedure AddSimpleEvent(Command:String;EventProc:P2PTSimpleEvent);
Procedure AddComplexEvent(Command:String;EventProc:P2PTComplexEvent);
published
// All of our protocols the expect commands to be received from
// the client, have a timeout value. This timeout is used
// internally by the socket Readln command. In this protocol, we
// implemented support for an OnCommandTimeout event. FOr this
// protocol we expect to have client systems too busy or with
// bad connections, so we support multiple timeouts before
// calling it quits.
//
//
//
// \See Also
//
// <LINK TDXP2PServerCore.OnCommandTimeout, OnCommandTimeout>
//
//
//
// Summary
// Maximum idle time before considering the client has timed
// out.
property Timeout:Cardinal read fiTimeout
write fiTimeout;
// When the P2P Server Core receives an AUTH command, the string
// from the client is parsed and this event is fired.
//
//
//
// The first parameter is always the clientthread.
//
//
//
// The next parameter is usally a login name, it is up to your
// implementation if this is case sensative of not.
//
//
//
// The next parameter in our implementation is a MD5 hash code
// for the password. Allowing us to transmit it an readable text
// and know that it will be rare for someone to hack the hash
// code.
//
//
//
// Summary
// AUTH command received.
property OnCommandAUTH: P2PTComplexEvent read fOnCommandAUTH
write SetOnCommandAUTH;
// When the P2P Server Core receives an QUERY command, the string
// from the client is parsed and this event is fired.
//
//
//
// The first parameter is always the clientthread.
//
//
//
// In our implementation the second parameter was the query.
//
//
//
// Summary
// QUERY command received.
property OnCommandQUERY: P2PTBasicEvent read fOnCommandQUERY
write SetOnCommandQUERY;
// When the P2P Server Core receives an PING command, the string
// from the client is parsed and this event is fired.
//
//
//
// The first parameter is always the clientthread.
//
//
//
// In our implementation the client sends a PING statment to the
// server, and the server tries to respond as fast as possible
// with a PONG reply. Like IRC protocol.
//
//
//
// Summary
// PING command received.
property OnCommandPING: P2PTSimpleEvent read fOnCommandPING
write SetOnCommandPING;
// When the P2P Server Core receives an PONG command, the string
// from the client is parsed and this event is fired.
//
//
//
// The first parameter is always the clientthread.
//
//
//
// In our implementation the server periodically sends a PING
// message to the client, which tries to respond to the server
// as fast as possible with a PONG reply.
//
//
//
// Summary
// PONG command received.
property OnCommandPONG: P2PTSimpleEvent read fOnCommandPONG
write SetOnCommandPONG;
// Almost every protocol in our suite has this "Unknown Command
// was received" event handler. If you do not implement this
// event handler, the internal "default" error message will be
// sent to the client sending the unknown command.
//
//
//
// Summary
// Unknown command was received.
property OnCommandOther: P2PTOtherEvent read fOnCommandOther
write fOnCommandOther;
// When the P2P Server Core does not receives a command within
// the Timeout property.
//
//
//
// The first parameter is always the clientthread.
//
//
//
// Unlike most of our server implementations, we needed to be
// able to set the TIMEOUT low, and when a command was not
// received from the client within this TIMEOUT. This event
// would fire and we would write to the client a PING command.
// Internally (via the fpSessionData pointer) we would track how
// many PING commands were sent, and after an excess amount we
// would terminate the session. And in our OnCommandPONG event
// we would set this counter back to zero.
//
//
//
// Summary
// No command received within timeout limit.
property OnCommandTimeout: P2PTSimpleEvent read fOnCommandTimeout
write fOnCommandTimeout;
end;
implementation
uses
DxString;
Type
PP2PBasicEvent=^TP2PBasicEvent;
TP2PBasicEvent=record
Tag:Integer;
Command:String;
EventProcedure:P2PTBasicEvent;
End;
PP2PSimpleEvent=^TP2PSimpleEvent;
TP2PSimpleEvent=record
Tag:Integer;
Command:String;
EventProcedure:P2PTSimpleEvent;
End;
PP2PComplexEvent=^TP2PComplexEvent;
TP2PComplexEvent=record
Tag:Integer;
Command:String;
EventProcedure:P2PTComplexEvent;
End;
{$IFDEF OBJECTS_ONLY}
constructor TDXP2PServerCore.Create;
{$ELSE}
constructor TDXP2PServerCore.Create(AOwner:TComponent);
{$ENDIF}
begin
{$IFDEF OBJECTS_ONLY}
inherited Create;
{$ELSE}
inherited Create(AOwner);
{$ENDIF}
ServerPort:=1881;
fiTimeout:=12000; // client should send a command, or noop every 10 seconds!
fEventArray:=TList.Create;
end;
destructor TDXP2PServerCore.Destroy;
Var
PBasicEvent:PP2PBasicEvent;
PSimpleEvent:PP2PSimpleEvent;
PComplexEvent:PP2PComplexEvent;
begin
If Assigned(fEventArray) then Begin
While fEventArray.Count>0 do Begin
Case PP2PBasicEvent(fEventArray[0]).Tag of
1:Begin
PBasicEvent:=fEventArray[0];
Dispose(PBasicEvent);
End;
2:Begin
PSimpleEvent:=fEventArray[0];
Dispose(PSimpleEvent);
End;
3:Begin
PComplexEvent:=fEventArray[0];
Dispose(PComplexEvent);
End;
End;
fEventArray.Delete(0);
End;
fEventArray.Free;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -