Created
July 17, 2016 03:23
-
-
Save Brian1KB/41ffcd25c9e3226c6572742c9c32f9c8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Net; | |
using System.Net.Sockets; | |
using log4net; | |
namespace Votifier | |
{ | |
public class VotifierServer | |
{ | |
private static readonly ILog Log = LogManager.GetLogger(typeof(VotifierServer)); | |
private readonly TcpListener _listener; | |
public VotifierServer(IPEndPoint endpoint) | |
{ | |
_listener = new TcpListener(endpoint); | |
_listener.BeginAcceptSocket(AcceptSocket, _listener); | |
Log.Info($"Votifier server open on {endpoint.Address}:{endpoint.Port}"); | |
} | |
private void AcceptSocket(IAsyncResult ar) | |
{ | |
try | |
{ | |
var clientSocket = _listener.EndAcceptSocket(ar); | |
var receivedBytes = new byte[256]; | |
clientSocket.Receive(receivedBytes); | |
clientSocket.Close(); | |
// Decrypt and fire event with correct variables here. | |
OnVote(new VoteEventArgs("", "", "", "")); | |
_listener.BeginAcceptSocket(AcceptSocket, _listener); | |
} | |
catch (Exception e) | |
{ | |
Log.Error($"Exception occured while accepting socket: {e}"); | |
} | |
} | |
public event EventHandler<VoteEventArgs> VoteEvent; | |
private void OnVote(VoteEventArgs e) | |
{ | |
var handler = VoteEvent; | |
handler?.Invoke(this, e); | |
} | |
} | |
public class VoteEventArgs : EventArgs | |
{ | |
public string Username { get; set; } | |
public string Address { get; set; } | |
public string Server { get; set; } | |
public string Date { get; set; } | |
public VoteEventArgs(string username, string address, string server, string date) | |
{ | |
Username = username; | |
Address = address; | |
Server = server; | |
Date = date; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment