📄 unitnetwork.pas
字号:
unit UnitNetwork;
interface
uses
Windows,
Messages,
SysUtils,
Variants,
Classes,
Graphics,
Controls,
Forms,
Dialogs,
ExtCtrls,
Menus,
ComCtrls,
StdCtrls,
Sockets,
CompressionStreamUnitForms;
type
TNetwork = class(TForm)
Label1: TLabel;
Memo1: TMemo;
Label2: TLabel;
Edit1: TEdit;
Label3: TLabel;
Edit2: TEdit;
Button1: TButton;
StatusBar1: TStatusBar;
PopupMenu1: TPopupMenu;
Refresh1: TMenuItem;
Button2: TButton;
procedure Refresh1Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCanResize(Sender: TObject; var NewWidth,
NewHeight: Integer; var Resize: Boolean);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormDeactivate(Sender: TObject);
procedure FormShow(Sender: TObject);
private
{ Private declarations }
DataSocket: TCustomWinSocket;
Connected: Boolean;
ConnectNotifyInfo: TNotifyInfo;
ReadNotifyInfo: TNotifyInfo;
DisconnectNotifyInfo: TNotifyInfo;
procedure Connect(var Socket: TCustomWinSocket; Data: Pointer);
procedure Read(var Socket: TCustomWinSocket; CommandFrame: TCommandFrame; Stream: TMemoryStream; Data: Pointer);
procedure Disconnect(var Socket: TCustomWinSocket; Data: Pointer);
function SocketConnected: Boolean;
public
{ Public declarations }
RemoteAddress: string;
WindowItem: TListItem;
end;
implementation
uses UnitMain;
{$R *.dfm}
const
N_TYPE = 5;
N_LIST = 1;
N_MAP = 2;
N_UNMAP = 3;
function TNetwork.SocketConnected: Boolean;
begin
if ((Connected) and (DataSocket <> nil)) then
begin
Connected := DataSocket.Connected;
if not Connected then StatusBar1.Panels[0].Text := ' 断开连接';
end;
Result := Connected;
end;
procedure TNetwork.Connect(var Socket: TCustomWinSocket; Data: Pointer);
var
Network: TNetwork;
ConnectionInfo: TConnectionInfo;
begin
Network := TNetwork(Data);
if TStreamRecord(Socket.Data).LocalAddress <> Network.RemoteAddress then Exit;
if Network.DataSocket = nil then Network.DataSocket := Socket else Exit;
ConnectionInfo.ConnectionType := N_TYPE;
Socket.SendBuf(ConnectionInfo, SizeOf(TConnectionInfo));
Network.Connected := True;
Network.StatusBar1.Panels.Items[0].Text := ' 已连接: ' + Socket.RemoteAddress;
Network.StatusBar1.Tag := 1;
Socket := nil;
end;
procedure TNetwork.Read(var Socket: TCustomWinSocket; CommandFrame: TCommandFrame; Stream: TMemoryStream; Data: Pointer);
var
Network: TNetwork;
Buffer: Pointer;
begin
Network := TNetwork(Data);
if Network.DataSocket = Socket then
begin
Buffer := Stream.Memory;
case CommandFrame.Command of
N_LIST:
begin
Network.Memo1.Text := string(Buffer);
end;
end;
Socket := nil;
end;
end;
procedure TNetwork.Disconnect(var Socket: TCustomWinSocket; Data: Pointer);
var
Network: TNetwork;
begin
Network := TNetwork(Data);
if Network.DataSocket = Socket then
begin
Network.Connected := False;
Network.StatusBar1.Panels[0].Text := ' 断开连接';
Socket := nil;
Network.DataSocket := nil;
end;
end;
procedure TNetwork.Refresh1Click(Sender: TObject);
var
CommandFrame: TCommandFrame;
ReplyStream: TMemoryStream;
begin
if StatusBar1.Tag <> 1 then Exit;
if not SocketConnected then Exit;
Memo1.Lines.Clear;
Memo1.Lines.Add('刷新中,请稍后...');
CommandFrame.len := 0;
CommandFrame.Command := N_LIST;
CommandFrame.ID := FRAME_ID;
ReplyStream := TMemoryStream.Create;
ReplyStream.WriteBuffer(CommandFrame, SizeOf(TCommandFrame));
Main.SendStream(DataSocket, ReplyStream);
end;
procedure TNetwork.Button1Click(Sender: TObject);
var
CommandFrame: TCommandFrame;
ReplyStream: TMemoryStream;
LocalName: array[0..MAX_PATH] of Char;
RemoteName: array[0..MAX_PATH] of Char;
begin
if StatusBar1.Tag <> 1 then Exit;
if not SocketConnected then Exit;
CopyMemory(@LocalName, @Edit2.Text[1], Length(Edit2.Text));
CopyMemory(@RemoteName, @Edit1.Text[1], Length(Edit1.Text));
CommandFrame.len := MAX_PATH * 2 + 2;
CommandFrame.Command := N_MAP;
CommandFrame.ID := FRAME_ID;
ReplyStream := TMemoryStream.Create;
ReplyStream.WriteBuffer(CommandFrame, SizeOf(TCommandFrame));
ReplyStream.WriteBuffer(LocalName, MAX_PATH + 1);
ReplyStream.WriteBuffer(RemoteName, MAX_PATH + 1);
Main.SendStream(DataSocket, ReplyStream);
end;
procedure TNetwork.FormCreate(Sender: TObject);
begin
DataSocket := nil;
ConnectNotifyInfo := TNotifyInfo.Create;
ConnectNotifyInfo.Data := Self;
ConnectNotifyInfo.Callback := @TNetwork.Connect;
Main.NotifyConnectList.Add(ConnectNotifyInfo);
ReadNotifyInfo := TNotifyInfo.Create;
ReadNotifyInfo.Data := Self;
ReadNotifyInfo.Callback := @TNetwork.Read;
Main.NotifyReadList.Add(ReadNotifyInfo);
DisconnectNotifyInfo := TNotifyInfo.Create;
DisconnectNotifyInfo.Data := Self;
DisconnectNotifyInfo.Callback := @TNetwork.Disconnect;
Main.NotifyDisconnectList.Add(DisconnectNotifyInfo);
end;
procedure TNetwork.Button2Click(Sender: TObject);
var
CommandFrame: TCommandFrame;
ReplyStream: TMemoryStream;
LocalName: array[0..MAX_PATH] of Char;
begin
if StatusBar1.Tag <> 1 then Exit;
if not SocketConnected then Exit;
CopyMemory(@LocalName, @Edit2.Text[1], Length(Edit2.Text));
CommandFrame.len := MAX_PATH + 1;
CommandFrame.Command := N_UNMAP;
CommandFrame.ID := FRAME_ID;
ReplyStream := TMemoryStream.Create;
ReplyStream.WriteBuffer(CommandFrame, SizeOf(TCommandFrame));
ReplyStream.WriteBuffer(LocalName, MAX_PATH + 1);
Main.SendStream(DataSocket, ReplyStream);
end;
procedure TNetwork.FormCanResize(Sender: TObject; var NewWidth,
NewHeight: Integer; var Resize: Boolean);
begin
Resize := False;
end;
procedure TNetwork.FormClose(Sender: TObject; var Action: TCloseAction);
var
Socket: TCustomWinSocket;
begin
Main.NotifyConnectList.Delete(Main.NotifyConnectList.IndexOf(ConnectNotifyInfo));
Main.NotifyReadList.Delete(Main.NotifyReadList.IndexOf(ReadNotifyInfo));
Main.NotifyDisconnectList.Delete(Main.NotifyDisconnectList.IndexOf(DisconnectNotifyInfo));
WindowItem.Delete;
if DataSocket <> nil then
begin
if not SocketConnected then
begin
Socket := DataSocket;
DataSocket := nil;
Connected := False;
Socket.Close;
end;
end;
end;
procedure TNetwork.FormDeactivate(Sender: TObject);
begin
if WindowState = wsMinimized then Hide;
end;
procedure TNetwork.FormShow(Sender: TObject);
begin
SetWindowLong(Button1.Handle, GWL_STYLE, WS_CHILD or WS_VISIBLE or BS_FLAT);
SetWindowLong(Button2.Handle, GWL_STYLE, WS_CHILD or WS_VISIBLE or BS_FLAT);
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -