Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Collections.Generic;
- using System.Windows.Forms;
- using System.Threading;
- using System.Net.Sockets;
- using System.Linq;
- namespace LoginServer
- {
- public partial class MainForm : Form
- {
- const int port = 10000;
- // For accepting connections
- TcpListener listener = null;
- Thread listenerThread = null;
- List<UserConnection> connections = new List<UserConnection>();
- int loggedInUsers = 0;
- TextBoxStreamWriter textboxWriter = null; // New StreamWriter object for Console
- TextWriter consoleWriter = null; // Original Console stream is stored for later restoration
- delegate void UpdateConnectionsStatCallback();
- delegate void IncrementLoggedInUsersStatCallback();
- delegate void DecrementLoggedInUsersStatCallback();
- public MainForm()
- {
- InitializeComponent();
- }
- void MainForm_Load(object sender, EventArgs e)
- {
- // TODO: This line of code loads data into the 'usersDataSet.Users' table. You can move, or remove it, as needed.
- this.usersTableAdapter.Fill(this.usersDataSet.Users);
- // TODO: This line of code loads data into the 'usersDataSet.Users' table. You can move, or remove it, as needed.
- this.usersTableAdapter.Fill(this.usersDataSet.Users);
- // Redirect Console output to consoleTextBox
- consoleWriter = Console.Out;
- textboxWriter = new TextBoxStreamWriter(this, consoleTextBox);
- Console.SetOut(textboxWriter);
- // Begin listening for connections
- listenerThread = new Thread(new ThreadStart(DoListen));
- listenerThread.Start();
- }
- void MainForm_FormClosing(object sender, FormClosingEventArgs e)
- {
- // Reset Console output stream
- Console.SetOut(consoleWriter);
- textboxWriter.Close();
- // Stop listening for connections
- listener.Stop();
- }
- // Thread function, listenes for connections
- void DoListen()
- {
- try
- {
- listener = new TcpListener(System.Net.IPAddress.Any, port);
- listener.Start();
- Console.WriteLine("Listener started");
- do
- {
- UserConnection client = new UserConnection(listener.AcceptTcpClient());
- client.LineReceived += new LineReceive(OnLineReceived);
- connections.Add(client);
- Console.WriteLine("New connection accepted");
- }
- while (true);
- }
- catch (Exception e)
- {
- Console.WriteLine("Error in listener: " + e.ToString());
- }
- }
- // Called when a UserConnection receives data
- void OnLineReceived(UserConnection sender, string data)
- {
- string[] dataArray = data.Split('\n');
- dataArray = dataArray[0].Split('|');
- switch (dataArray[0])
- {
- case "LOGIN":
- LoginUser(dataArray[1], dataArray[2], sender);
- break;
- case "LOGOUT":
- LogoutUser(sender);
- break;
- case "CREATEACCOUNT":
- CreateAccount(dataArray[1], dataArray[2], sender);
- break;
- case "GETFRIENDS":
- GetFriends(sender);
- break;
- default:
- Console.WriteLine("Unrecognized data received from user");
- break;
- }
- }
- void LoginUser(string userName, string password, UserConnection sender)
- {
- foreach (DataGridViewRow row in userDataGridView.Rows)
- {
- if (row.Cells[0].Value.Equals(userName) && row.Cells[1].Value.Equals(password) && row.Cells[2].Value.Equals(false))
- {
- row.Cells[2].Value = true;
- sender.SendData("LOGINGRANTED");
- sender.userName = userName;
- Console.WriteLine("User " + userName + " logged in");
- IncrementLoggedInUsersStat();
- return;
- }
- }
- sender.SendData("LOGINDENIED");
- }
- void LogoutUser(UserConnection sender)
- {
- foreach (DataGridViewRow row in userDataGridView.Rows)
- {
- if (row.Cells[0].Value.Equals(sender.userName) && row.Cells[2].Value.Equals(true))
- {
- Console.WriteLine("User " + sender.userName + " logged out");
- row.Cells[2].Value = false;
- DecrementLoggedInUsersStat();
- }
- }
- }
- void CreateAccount(string userName, string password, UserConnection sender)
- {
- foreach (DataGridViewRow row in userDataGridView.Rows)
- {
- if (row.Cells[0].Value.Equals(userName))
- {
- sender.SendData("CREATEACCOUNTDENIED");
- return;
- }
- }
- UsersDataSet.UsersRow newRow = usersDataSet.Users.NewUsersRow();
- newRow[0] = userName;
- newRow[1] = password;
- newRow[2] = false;
- newRow[3] = "";
- newRow[4] = 0;
- newRow[5] = 0;
- newRow[6] = 0;
- newRow[7] = 0;
- newRow[8] = 0;
- newRow[9] = 0;
- newRow[10] = 0;
- newRow[11] = 0;
- usersDataSet.Users.Rows.Add(newRow);
- usersTableAdapter.Update(usersDataSet.Users);
- sender.SendData("CREATEACCOUNTGRANTED");
- Console.WriteLine("Account created: " + userName);
- }
- void GetFriends(UserConnection sender)
- {
- foreach (DataGridViewRow row in userDataGridView.Rows)
- {
- if (row.Cells[0].Value.Equals(sender.userName))
- {
- sender.SendData("RECEIVEFRIENDS|" + row.Cells[3].Value.ToString());
- }
- }
- }
- void HeartbeatTimer_Tick(object sender, EventArgs e)
- {
- List<UserConnection> removeConns = new List<UserConnection>();
- foreach (UserConnection conn in connections)
- {
- if (!conn.active)
- {
- Console.WriteLine("Cleaning up dropped user");
- LogoutUser(conn);
- removeConns.Add(conn);
- }
- }
- foreach (UserConnection conn in removeConns)
- {
- connections.Remove(conn);
- }
- UpdateConnectionsStat();
- }
- void UpdateConnectionsStat()
- {
- if (statsListView.InvokeRequired)
- {
- UpdateConnectionsStatCallback d = new UpdateConnectionsStatCallback(UpdateConnectionsStat);
- Invoke(d);
- }
- else
- {
- statsListView.Items[0].SubItems[1].Text = connections.Count.ToString();
- }
- }
- void IncrementLoggedInUsersStat()
- {
- if (statsListView.InvokeRequired)
- {
- IncrementLoggedInUsersStatCallback d = new IncrementLoggedInUsersStatCallback(IncrementLoggedInUsersStat);
- Invoke(d);
- }
- else
- {
- ++loggedInUsers;
- statsListView.Items[0].SubItems[1].Text = loggedInUsers.ToString();
- }
- }
- void DecrementLoggedInUsersStat()
- {
- if (statsListView.InvokeRequired)
- {
- DecrementLoggedInUsersStatCallback d = new DecrementLoggedInUsersStatCallback(DecrementLoggedInUsersStat);
- Invoke(d);
- }
- else
- {
- --loggedInUsers;
- statsListView.Items[0].SubItems[1].Text = loggedInUsers.ToString();
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment