Guest User

Untitled

a guest
Aug 17th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.32 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Windows.Forms;
  5. using System.Threading;
  6. using System.Net.Sockets;
  7. using System.Linq;
  8.  
  9.  
  10.  
  11. namespace LoginServer
  12. {
  13. public partial class MainForm : Form
  14. {
  15. const int port = 10000;
  16.  
  17. // For accepting connections
  18. TcpListener listener = null;
  19. Thread listenerThread = null;
  20. List<UserConnection> connections = new List<UserConnection>();
  21. int loggedInUsers = 0;
  22.  
  23. TextBoxStreamWriter textboxWriter = null; // New StreamWriter object for Console
  24. TextWriter consoleWriter = null; // Original Console stream is stored for later restoration
  25.  
  26. delegate void UpdateConnectionsStatCallback();
  27. delegate void IncrementLoggedInUsersStatCallback();
  28. delegate void DecrementLoggedInUsersStatCallback();
  29.  
  30. public MainForm()
  31. {
  32. InitializeComponent();
  33. }
  34.  
  35. void MainForm_Load(object sender, EventArgs e)
  36. {
  37. // TODO: This line of code loads data into the 'usersDataSet.Users' table. You can move, or remove it, as needed.
  38. this.usersTableAdapter.Fill(this.usersDataSet.Users);
  39. // TODO: This line of code loads data into the 'usersDataSet.Users' table. You can move, or remove it, as needed.
  40. this.usersTableAdapter.Fill(this.usersDataSet.Users);
  41.  
  42. // Redirect Console output to consoleTextBox
  43. consoleWriter = Console.Out;
  44. textboxWriter = new TextBoxStreamWriter(this, consoleTextBox);
  45. Console.SetOut(textboxWriter);
  46.  
  47. // Begin listening for connections
  48. listenerThread = new Thread(new ThreadStart(DoListen));
  49. listenerThread.Start();
  50. }
  51.  
  52. void MainForm_FormClosing(object sender, FormClosingEventArgs e)
  53. {
  54. // Reset Console output stream
  55. Console.SetOut(consoleWriter);
  56. textboxWriter.Close();
  57.  
  58. // Stop listening for connections
  59. listener.Stop();
  60. }
  61.  
  62. // Thread function, listenes for connections
  63. void DoListen()
  64. {
  65. try
  66. {
  67. listener = new TcpListener(System.Net.IPAddress.Any, port);
  68. listener.Start();
  69.  
  70. Console.WriteLine("Listener started");
  71.  
  72. do
  73. {
  74. UserConnection client = new UserConnection(listener.AcceptTcpClient());
  75. client.LineReceived += new LineReceive(OnLineReceived);
  76. connections.Add(client);
  77.  
  78. Console.WriteLine("New connection accepted");
  79. }
  80. while (true);
  81. }
  82. catch (Exception e)
  83. {
  84. Console.WriteLine("Error in listener: " + e.ToString());
  85. }
  86. }
  87.  
  88. // Called when a UserConnection receives data
  89. void OnLineReceived(UserConnection sender, string data)
  90. {
  91. string[] dataArray = data.Split('\n');
  92. dataArray = dataArray[0].Split('|');
  93.  
  94. switch (dataArray[0])
  95. {
  96. case "LOGIN":
  97. LoginUser(dataArray[1], dataArray[2], sender);
  98. break;
  99.  
  100. case "LOGOUT":
  101. LogoutUser(sender);
  102. break;
  103.  
  104. case "CREATEACCOUNT":
  105. CreateAccount(dataArray[1], dataArray[2], sender);
  106. break;
  107.  
  108. case "GETFRIENDS":
  109. GetFriends(sender);
  110. break;
  111.  
  112. default:
  113. Console.WriteLine("Unrecognized data received from user");
  114. break;
  115. }
  116. }
  117.  
  118. void LoginUser(string userName, string password, UserConnection sender)
  119. {
  120. foreach (DataGridViewRow row in userDataGridView.Rows)
  121. {
  122. if (row.Cells[0].Value.Equals(userName) && row.Cells[1].Value.Equals(password) && row.Cells[2].Value.Equals(false))
  123. {
  124. row.Cells[2].Value = true;
  125. sender.SendData("LOGINGRANTED");
  126. sender.userName = userName;
  127. Console.WriteLine("User " + userName + " logged in");
  128.  
  129. IncrementLoggedInUsersStat();
  130.  
  131. return;
  132. }
  133. }
  134.  
  135. sender.SendData("LOGINDENIED");
  136. }
  137.  
  138. void LogoutUser(UserConnection sender)
  139. {
  140. foreach (DataGridViewRow row in userDataGridView.Rows)
  141. {
  142. if (row.Cells[0].Value.Equals(sender.userName) && row.Cells[2].Value.Equals(true))
  143. {
  144. Console.WriteLine("User " + sender.userName + " logged out");
  145. row.Cells[2].Value = false;
  146. DecrementLoggedInUsersStat();
  147. }
  148. }
  149. }
  150.  
  151. void CreateAccount(string userName, string password, UserConnection sender)
  152. {
  153. foreach (DataGridViewRow row in userDataGridView.Rows)
  154. {
  155. if (row.Cells[0].Value.Equals(userName))
  156. {
  157. sender.SendData("CREATEACCOUNTDENIED");
  158. return;
  159. }
  160. }
  161.  
  162. UsersDataSet.UsersRow newRow = usersDataSet.Users.NewUsersRow();
  163. newRow[0] = userName;
  164. newRow[1] = password;
  165. newRow[2] = false;
  166. newRow[3] = "";
  167. newRow[4] = 0;
  168. newRow[5] = 0;
  169. newRow[6] = 0;
  170. newRow[7] = 0;
  171. newRow[8] = 0;
  172. newRow[9] = 0;
  173. newRow[10] = 0;
  174. newRow[11] = 0;
  175.  
  176. usersDataSet.Users.Rows.Add(newRow);
  177. usersTableAdapter.Update(usersDataSet.Users);
  178.  
  179. sender.SendData("CREATEACCOUNTGRANTED");
  180. Console.WriteLine("Account created: " + userName);
  181. }
  182.  
  183. void GetFriends(UserConnection sender)
  184. {
  185. foreach (DataGridViewRow row in userDataGridView.Rows)
  186. {
  187. if (row.Cells[0].Value.Equals(sender.userName))
  188. {
  189. sender.SendData("RECEIVEFRIENDS|" + row.Cells[3].Value.ToString());
  190. }
  191. }
  192. }
  193.  
  194. void HeartbeatTimer_Tick(object sender, EventArgs e)
  195. {
  196. List<UserConnection> removeConns = new List<UserConnection>();
  197.  
  198. foreach (UserConnection conn in connections)
  199. {
  200. if (!conn.active)
  201. {
  202. Console.WriteLine("Cleaning up dropped user");
  203. LogoutUser(conn);
  204. removeConns.Add(conn);
  205. }
  206. }
  207.  
  208. foreach (UserConnection conn in removeConns)
  209. {
  210. connections.Remove(conn);
  211. }
  212.  
  213. UpdateConnectionsStat();
  214. }
  215.  
  216. void UpdateConnectionsStat()
  217. {
  218. if (statsListView.InvokeRequired)
  219. {
  220. UpdateConnectionsStatCallback d = new UpdateConnectionsStatCallback(UpdateConnectionsStat);
  221. Invoke(d);
  222. }
  223. else
  224. {
  225. statsListView.Items[0].SubItems[1].Text = connections.Count.ToString();
  226. }
  227. }
  228.  
  229. void IncrementLoggedInUsersStat()
  230. {
  231. if (statsListView.InvokeRequired)
  232. {
  233. IncrementLoggedInUsersStatCallback d = new IncrementLoggedInUsersStatCallback(IncrementLoggedInUsersStat);
  234. Invoke(d);
  235. }
  236. else
  237. {
  238. ++loggedInUsers;
  239. statsListView.Items[0].SubItems[1].Text = loggedInUsers.ToString();
  240. }
  241. }
  242.  
  243. void DecrementLoggedInUsersStat()
  244. {
  245. if (statsListView.InvokeRequired)
  246. {
  247. DecrementLoggedInUsersStatCallback d = new DecrementLoggedInUsersStatCallback(DecrementLoggedInUsersStat);
  248. Invoke(d);
  249. }
  250. else
  251. {
  252. --loggedInUsers;
  253. statsListView.Items[0].SubItems[1].Text = loggedInUsers.ToString();
  254. }
  255. }
  256. }
  257. }
Add Comment
Please, Sign In to add comment