PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/CoreSDK/test/HowToCreateP2P.pas

http://swingamesdk.googlecode.com/
Pascal | 198 lines | 156 code | 22 blank | 20 comment | 20 complexity | 73998107d79c13553113ca62c4795b4a MD5 | raw file
Possible License(s): LGPL-2.0, BSD-3-Clause, GPL-2.0
  1. program HowToCreateHost;
  2. uses
  3. SwinGame, sgTypes, sgNetworking, SysUtils;
  4. //----------------------------------------------------------------------------
  5. // Init
  6. //----------------------------------------------------------------------------
  7. procedure InitPanel(var aMenuPanel : Panel);
  8. begin
  9. GUISetForegroundColor(ColorBlack);
  10. GUISetBackgroundColor(ColorWhite);
  11. ShowPanel(aMenuPanel);
  12. ActivatePanel(aMenuPanel);
  13. DrawGUIAsVectors(True);
  14. end;
  15. procedure SetLabelText();
  16. begin
  17. LabelSetText('MyIPLbl', 'My IP:');
  18. LabelSetText('MyIPVal', MyIP());
  19. LabelSetText('MyPorttLbl', 'Port: ');
  20. LabelSetText('HostStatusLbl', 'Status: ');
  21. LabelSetText('MsgsLbl', 'Message: ');
  22. LabelSetText('ServerLbl', 'Server:');
  23. LabelSetText('ServerPorttLbl', 'Port: ');
  24. LabelSetText('ClientStatusLbl', 'Status: ');
  25. LabelSetText('ServerVal', '127.0.0.1');
  26. end;
  27. //----------------------------------------------------------------------------
  28. // Connect
  29. //----------------------------------------------------------------------------
  30. procedure CreateHost(var aMenuPanel : Panel; var aStatus : Boolean);
  31. var
  32. lPort : Integer;
  33. begin
  34. if TryStrToInt(TextBoxText('MyPortVal'), lPort) then
  35. begin
  36. CreateTCPHost(lPort);
  37. LabelSetText('HostStatusVal', 'Listening...');
  38. LabelSetText('HostLbl', 'Disconnect');
  39. ListAddItem(aMenuPanel, 'MsgList', 'Now Listening for Connections....');
  40. aStatus := True;
  41. end;
  42. end;
  43. procedure ConnectToHost(aMenuPanel : Panel; var aStatus : Boolean; var lPeer : Connection);
  44. var
  45. lPort : Integer;
  46. begin
  47. if TryStrToInt(TextBoxText('ServerPortVal'), lPort) then
  48. begin
  49. lPeer := CreateTCPConnection(TextBoxText('ServerVal'), lPort);
  50. if Assigned(lPeer) then
  51. begin
  52. LabelSetText('ClientStatusVal', 'Connected');
  53. ListAddItem(aMenuPanel, 'MsgList', 'Connected to: ' + TextBoxText('ServerVal') + ':' + IntToStr(lPort));
  54. LabelSetText('ConnLbl', 'Disconnect');
  55. aStatus := True;
  56. end;
  57. end;
  58. end;
  59. procedure AcceptConnection(var aMenuPanel : Panel; var lPeer : Connection);
  60. begin
  61. if AcceptTCPConnection(); then
  62. begin
  63. lPeer := FetchConnection();
  64. ListAddItem(aMenuPanel, 'MsgList', + 'Connected.');
  65. end;
  66. end;
  67. //----------------------------------------------------------------------------
  68. // Disconnect
  69. //----------------------------------------------------------------------------
  70. procedure DisconnectHost(var aMenuPanel : Panel; var aStatus : Boolean);
  71. begin
  72. CloseAllTCPReceiverSocket();
  73. CloseAllTCPHostSocket();
  74. LabelSetText('HostStatusVal', 'Idle');
  75. LabelSetText('HostLbl', 'Connect');
  76. ListAddItem(aMenuPanel, 'MsgList', 'Listening Sockets Closed.');
  77. aStatus := False;
  78. end;
  79. procedure DisconnectFromAllHost(aMenuPanel : Panel; var aStatus : Boolean);
  80. begin
  81. BroadcastTCPMessage('[ClientDisconnect]');
  82. CloseAllTCPSenderSocket();
  83. LabelSetText('ClientStatusVal', 'Disconnected');
  84. LabelSetText('ConnLbl', 'Connect');
  85. ListAddItem(aMenuPanel, 'MsgList', 'You Disconnected.');
  86. aStatus := False;
  87. end;
  88. //----------------------------------------------------------------------------
  89. // Messages
  90. //----------------------------------------------------------------------------
  91. procedure ReceiveMessage(var aMenuPanel : Panel);
  92. var
  93. i : Integer;
  94. begin
  95. if (not TCPMessageReceived()) then exit;
  96. for i:= 0 to GetMessageCount() - 1 do
  97. begin
  98. if (GetMessage() = '[ClientDisconnect]') then
  99. begin
  100. CloseTCPReceiverSocket(GetIPFromMessage(), GetPortFromMessage());
  101. ListAddItem(aMenuPanel, 'MsgList', GetIPFromMessage() + ' Disconnected.');
  102. end else begin
  103. ListAddItem(aMenuPanel, 'MsgList', GetIPFromMessage() + ' said: ' + GetMessage());
  104. ListSetActiveItemIndex(aMenuPanel, 'MsgList', ListItemCount('MsgList') - 1);
  105. end;
  106. DequeueTopMessage();
  107. end;
  108. end;
  109. //----------------------------------------------------------------------------
  110. // Input
  111. //----------------------------------------------------------------------------
  112. procedure HandleGUIInput(aChatPanel : Panel; var aServerStatus, aClientStatus : Boolean);
  113. begin
  114. //Server Connect and Disconnect
  115. if (not aServerStatus) and (RegionClickedID() = 'HostBtn') then
  116. CreateHost(aChatPanel, aServerStatus)
  117. else if (aServerStatus) and (RegionClickedID() = 'HostBtn') then
  118. DisconnectHost(aChatPanel, aServerStatus)
  119. //Client Connect and Disconnect
  120. else if (not aClientStatus) and (RegionClickedID() = 'ConnectBtn') then
  121. ConnectToHost(aChatPanel, aClientStatus)
  122. else if (aClientStatus) and (RegionClickedID() = 'ConnectBtn') then
  123. DisconnectFromAllHost(aChatPanel, aClientStatus)
  124. //Message Input
  125. else if GUITextEntryComplete() and (GUITextBoxOfTextEntered() = TextBoxFromID('MsgVal')) then
  126. begin
  127. if (TextBoxText('MsgVal') = '') then exit;
  128. if (Length(BroadcastTCPMessage(TextBoxText('MsgVal'))) <> 0) then
  129. begin
  130. ListAddItem(aChatPanel, 'MsgList', 'Host Disconnected.');
  131. DisconnectFromAllHost(aChatPanel, aClientStatus);
  132. exit;
  133. end;
  134. ListAddItem(aChatPanel, 'MsgList', 'You Say: ' + TextBoxText('MsgVal'));
  135. ListSetActiveItemIndex(aChatPanel, 'MsgList', ListItemCount('MsgList') - 1);
  136. TextBoxSetText('MsgVal', '');
  137. GUISetActiveTextbox(TextBoxFromID('MsgVal'));
  138. end;
  139. end;
  140. procedure Main();
  141. var
  142. lServerPanel, lClientPanel, lChatPanel : Panel;
  143. //True for Listening, False for Idle.
  144. lHostStatus : Boolean = False;
  145. //True for Connected, False for Disconnected
  146. lClientStatus : Boolean = False;
  147. lPeer : Connection = nil;
  148. begin
  149. OpenGraphicsWindow('How To Create Host', 800, 600);
  150. LoadDefaultColors();
  151. LoadResourceBundle('MainMenu.txt');
  152. lServerPanel := LoadPanel('hostpanel.txt');
  153. lClientPanel := LoadPanel('clientpanel.txt');
  154. lChatPanel := LoadPanel('ChatWindowPanel.txt');
  155. InitPanel(lServerPanel);
  156. InitPanel(lClientPanel);
  157. InitPanel(lChatPanel);
  158. SetLabelText();
  159. repeat
  160. ProcessEvents();
  161. ClearScreen(ColorBlack);
  162. DrawPanels();
  163. UpdateInterface();
  164. HandleGUIInput(lChatPanel, lHostStatus, lClientStatus);
  165. if not lHostStatus then
  166. AcceptConnection(lChatPanel, lPeer);
  167. else
  168. ConnectToHost(aMenuPanel : Panel; var aStatus : Boolean; var lPeer : Connection);
  169. ReceiveMessage(lChatPanel);
  170. RefreshScreen();
  171. until WindowCloseRequested();
  172. ReleaseAllResources();
  173. end;
  174. begin
  175. Main();
  176. end.