📄 u_mgserver_main.pas
字号:
(*
----------------------------------------------
u_mgServer_main.pas
MediaGate demo application - Server main form source
----------------------------------------------
This source code cannot be used without
proper license granted to you as a private
person or an entity by the Lake of Soft, Ltd
Visit http://lakeofsoft.com/ for more information.
Copyright (c) 2002, 2007 Lake of Soft, Ltd
All rights reserved
----------------------------------------------
created by:
Lake, 04 Apr 2003
modified by:
Lake, Apr-Oct 2003
Lake, Apr 2007
----------------------------------------------
*)
{$I unaDef.inc }
unit
u_mgServer_main;
interface
uses
Windows, unaTypes, unaClasses, Forms,
ExtCtrls, unaVcIDE, Controls, StdCtrls, CheckLst, Classes, ActnList, ComCtrls;
type
Tc_form_main = class(TForm)
c_edit_speakPort: TEdit;
Label1: TLabel;
Label2: TLabel;
c_edit_listenPort: TEdit;
c_button_start: TButton;
c_button_stop: TButton;
c_clb_debug: TCheckListBox;
speakServer: TunavclIPInStream;
listenServer: TunavclIPInStream;
c_timer_update: TTimer;
c_actionList_main: TActionList;
a_srv_start: TAction;
a_srv_stop: TAction;
Label3: TLabel;
Bevel2: TBevel;
Label4: TLabel;
Bevel3: TBevel;
c_statusBar_main: TStatusBar;
c_label_listeners: TLabel;
c_label_served: TLabel;
c_label_received: TLabel;
c_comboBox_speakProto: TComboBox;
Label5: TLabel;
c_comboBox_listenProto: TComboBox;
Label6: TLabel;
c_comboBox_listener: TComboBox;
Label7: TLabel;
c_checkBox_allowListen: TCheckBox;
c_checkBox_acceptSpeaker: TCheckBox;
a_accept_speaker: TAction;
a_accept_listener: TAction;
//
procedure formCreate(sender: tObject);
procedure formDestroy(sender: tObject);
procedure formCloseQuery(sender: tObject; var CanClose: Boolean);
procedure formShow(sender: tObject);
//
procedure c_timer_updateTimer(sender: tObject);
procedure c_comboBox_listenerChange(Sender: TObject);
//
procedure a_srv_startExecute(sender: tObject);
procedure a_srv_stopExecute(sender: tObject);
procedure a_accept_speakerExecute(Sender: TObject);
procedure a_accept_listenerExecute(Sender: TObject);
//
procedure speakServerServerNewClient(sender: TObject; connectionId: Cardinal; connected: LongBool);
procedure listenServerServerNewClient(sender: TObject; connectionId: Cardinal; connected: LongBool);
procedure listenServerServerClientDisconnect(sender: TObject; connectionId: Cardinal; connected: LongBool);
private
{ Private declarations }
f_ini: unaIniFile;
f_needListenerRefresh: bool;
f_needSpeakerRefresh: bool;
public
{ Public declarations }
end;
var
c_form_main: Tc_form_main;
implementation
{$R *.dfm}
uses
Messages, unaUtils, unaVCLUtils, unaWave, winSock, unaSockets;
// -- --
procedure Tc_form_main.formCreate(sender: tObject);
begin
f_ini := unaIniFile.create();
//
c_edit_speakPort.text := f_ini.get('speak.port', '17860');
c_edit_listenPort.text := f_ini.get('listen.port', '17861');
//
c_comboBox_speakProto.itemIndex := f_ini.get('speak.proto', int(0));
c_comboBox_listenProto.itemIndex := f_ini.get('listen.proto', int(0));
end;
// -- --
procedure Tc_form_main.formDestroy(sender: tObject);
begin
freeAndNil(f_ini);
end;
// -- --
procedure Tc_form_main.formCloseQuery(sender: tObject; var canClose: boolean);
begin
saveControlPosition(self, f_ini);
//
c_timer_update.enabled := false;
//
speakServer.close();
//
with (f_ini) do begin
setValue('speak.proto', c_comboBox_speakProto.itemIndex);
setValue('listen.proto', c_comboBox_listenProto.itemIndex);
//
setValue('speak.port', c_edit_speakPort.text);
setValue('listen.port', c_edit_listenPort.text);
end;
end;
// -- --
procedure Tc_form_main.formShow(sender: tObject);
begin
loadControlPosition(self, f_ini);
//
c_clb_debug.visible := {$IFDEF DEBUG}true{$ELSE}false{$ENDIF};
//
c_timer_update.enabled := true;
end;
// -- --
procedure Tc_form_main.c_timer_updateTimer(sender: tObject);
var
running: bool;
index: int;
i: unsigned;
count: int;
conn: unaSocksConnection;
begin
if (not (csDestroying in componentState)) then begin
//
{$IFDEF DEBUG }
c_clb_debug.checked[0] := speakServer.active;
c_clb_debug.checked[1] := listenServer.active;
//
c_statusBar_main.panels[0].text := 'Mem: ' + int2str(ams() shr 10, 10, 3) + ' KB';
{$ENDIF}
//
if (nil <> speakServer.remoteFormat) then
c_label_received.caption := 'Received : ' + int2str(speakServer.inPacketsCount, 10, 3) + ' packets.'
else
c_label_received.caption := 'Waiting for data stream';
//
c_label_listeners.caption := 'Listeners : ' + int2str(listenServer.clientCount) + '/' + int2str(listenServer.maxClients);
c_label_served.caption := 'Served : ' + int2str(listenServer.inBytes, 10, 3) + ' bytes.';
//
running := speakServer.active;
a_srv_start.enabled := not running;
c_edit_speakPort.enabled := not running;
c_comboBox_speakProto.enabled := not running;
//
if (running) then
a_accept_speaker.enabled := (0 < speakServer.clientCount)
else
a_accept_speaker.enabled := false;
//
a_srv_stop.enabled := running and listenServer.active;
running := listenServer.active;
c_edit_listenPort.enabled := not running;
c_comboBox_listenProto.enabled := not running;
//
c_comboBox_listener.enabled := running;
if (running) then
a_accept_listener.enabled := (0 <= c_comboBox_listener.itemIndex)
else
a_accept_listener.enabled := false;
//
if (f_needSpeakerRefresh) then begin
//
f_needSpeakerRefresh := false;
//
a_accept_speaker.checked := (0 <> (speakServer.clientOptions[0] and c_unaIPServer_co_inbound));
end;
//
if (f_needListenerRefresh) then begin
//
f_needListenerRefresh := false;
//
index := c_comboBox_listener.itemIndex;
c_comboBox_listener.clear();
if (0 < listenServer.clientCount) then
//
for i := 0 to listenServer.clientCount - 1 do begin
// get client IP
conn := listenServer.getClientConnection(listenServer.getClientConnId(i));
if (nil <> conn) then begin
//
try
//
c_comboBox_listener.items.Add('Listener #' + int2str(i) + ' / ' + string(inet_ntoa(conn.paddr.sin_addr)) + ':' + int2str(htons(conn.paddr.sin_port)))
finally
conn.release();
end;
end
else
c_comboBox_listener.items.Add('Listener #' + int2str(i) + ' / Unknown Client');
end;
//
count := c_comboBox_listener.items.count;
if (index >= count) then
c_comboBox_listener.itemIndex := count - 1
else
if (0 > index) then begin
//
if (1 > count) then
c_comboBox_listener.itemIndex := -1
else
c_comboBox_listener.itemIndex := 0
end
else
c_comboBox_listener.itemIndex := index;
//
c_comboBox_listenerChange(sender);
end;
end;
end;
// -- --
procedure Tc_form_main.a_srv_startExecute(sender: tObject);
begin
a_srv_start.enabled := false;
//
speakServer.proto := tunavclProtoType(choice(0 = c_comboBox_speakProto.ItemIndex, ord(unapt_UDP), ord(unapt_TCP)));
listenServer.proto := tunavclProtoType(choice(0 = c_comboBox_listenProto.ItemIndex, ord(unapt_UDP), ord(unapt_TCP)));
//
speakServer.port := c_edit_speakPort.text;
listenServer.port := c_edit_listenPort.text;
//
speakServer.open();
end;
// -- --
procedure Tc_form_main.a_srv_stopExecute(sender: tObject);
begin
a_srv_stop.enabled := false;
//
speakServer.close();
//
f_needListenerRefresh := true;
end;
// -- --
procedure Tc_form_main.a_accept_speakerExecute(Sender: TObject);
var
options: unsigned;
begin
// toggle accept speaker stream
options := speakServer.clientOptions[0];
if (0 <> (options and c_unaIPServer_co_inbound)) then
options := options and (not c_unaIPServer_co_inbound)
else
options := options or c_unaIPServer_co_inbound;
//
speakServer.clientOptions[0] := options;
//
a_accept_speaker.checked := (0 <> (options and c_unaIPServer_co_inbound));
end;
// -- --
procedure Tc_form_main.a_accept_listenerExecute(Sender: TObject);
var
index: int;
options: unsigned;
begin
index := c_comboBox_listener.itemIndex;
//
if (0 <= index) then begin
// toggle allow listener stream
options := listenServer.clientOptions[index];
if (0 <> (options and c_unaIPServer_co_outbound)) then
options := options and (not c_unaIPServer_co_outbound)
else
options := options or c_unaIPServer_co_outbound;
//
listenServer.clientOptions[index] := options;
//
a_accept_listener.checked := (0 <> (options and c_unaIPServer_co_outbound));
end;
end;
// -- --
procedure Tc_form_main.speakServerServerNewClient(sender: TObject; connectionId: Cardinal; connected: LongBool);
begin
f_needSpeakerRefresh := true;
end;
// -- --
procedure Tc_form_main.listenServerServerNewClient(sender: TObject; connectionId: Cardinal; connected: LongBool);
begin
f_needListenerRefresh := true;
end;
// -- --
procedure Tc_form_main.listenServerServerClientDisconnect(sender: TObject; connectionId: Cardinal; connected: LongBool);
begin
f_needListenerRefresh := true;
end;
// -- --
procedure Tc_form_main.c_comboBox_listenerChange(Sender: TObject);
begin
if (0 <= c_comboBox_listener.itemIndex) then
a_accept_listener.checked := (0 <> (listenServer.clientOptions[c_comboBox_listener.itemIndex] and c_unaIPServer_co_outbound))
else
a_accept_listener.checked := false;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -