Created
February 17, 2013 00:28
-
-
Save agesome/4969425 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 System.Threading; | |
using System.Text.RegularExpressions; | |
namespace server | |
{ | |
class ProcessingServer | |
{ | |
const uint bufferSize = 1024; | |
const byte goodbyeMagic = 0x42; | |
readonly ManualResetEvent incomingConnection = new ManualResetEvent (false); | |
class Moment | |
{ | |
public uint Year{ get; set; } | |
public uint Month{ get; set; } | |
public uint Day{ get; set; } | |
public uint Hour{ get; set; } | |
public uint Minute{ get; set; } | |
public uint Second{ get; set; } | |
public override string ToString () | |
{ | |
return Year.ToString () + " " + Month.ToString () + " " + Day.ToString () + | |
" " + Hour.ToString () + " " + Minute.ToString () + " " + Second.ToString (); | |
} | |
public bool Validate () | |
{ | |
if (Month > 12 || Day > 31 || Hour > 24 || Minute > 59 || Second > 59) | |
return false; | |
return true; | |
} | |
}; | |
public void startListening () | |
{ | |
IPEndPoint local = new IPEndPoint (IPAddress.Any, 8080); | |
Socket listener = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); | |
listener.Bind (local); | |
listener.Listen (10); | |
while (true) { | |
incomingConnection.Reset (); | |
listener.BeginAccept (new AsyncCallback (processingThread), listener); | |
incomingConnection.WaitOne (); | |
} | |
} | |
private void processingThread (IAsyncResult rv) | |
{ | |
byte [] buffer = new byte[bufferSize]; | |
incomingConnection.Set (); | |
Console.WriteLine ("Accepted connection"); | |
Socket listener = (Socket)rv.AsyncState; | |
Socket io = listener.EndAccept (rv); | |
int size; | |
while (true) { | |
try { | |
size = io.Receive (buffer); | |
} | |
catch (SocketException e) { | |
break; | |
} | |
if (size == 0) | |
continue; | |
if (buffer[0] == goodbyeMagic) | |
break; | |
byte[] buf = new byte[size]; | |
System.Buffer.BlockCopy (buffer, 0, buf, 0, size); | |
string line = System.Text.UnicodeEncoding.Unicode.GetString (buf); | |
string processed = processData (line); | |
sendResponse (processed, io); | |
} | |
io.Close (); | |
Console.WriteLine ("Thread closed"); | |
} | |
private string processData (string data) | |
{ | |
string[] regexes = | |
{ | |
@"^(?<y>\d\d\d\d)-(?<m>\d\d)-(?<d>\d\d) (?<hh>\d\d):(?<mm>\d\d):(?<ss>\d\d)$", | |
@"^(?<hh>\d\d):(?<mm>\d\d):(?<ss>\d\d) (?<y>\d\d\d\d)-(?<m>\d\d)-(?<d>\d\d)$", | |
@"^(?<y>\d\d\d\d)/(?<m>\d\d)/(?<d>\d\d) (?<hh>\d\d):(?<mm>\d\d):(?<ss>\d\d)$", | |
@"^(?<hh>\d\d):(?<mm>\d\d):(?<ss>\d\d) (?<y>\d\d\d\d)/(?<m>\d\d)/(?<d>\d\d)$" | |
}; | |
Moment m = new Moment (); | |
Console.WriteLine ("Processing: " + data); | |
MatchCollection matches; | |
Match mm = null; | |
foreach (string regex in regexes) { | |
matches = Regex.Matches (data, regex); | |
if (matches.Count > 0) | |
{ | |
mm = matches[0]; | |
break; | |
} | |
} | |
if (mm == null) | |
return "Processing failed"; | |
m.Year = uint.Parse(mm.Groups["y"].Value); | |
m.Month = uint.Parse(mm.Groups["m"].Value); | |
m.Day = uint.Parse(mm.Groups["d"].Value); | |
m.Hour = uint.Parse(mm.Groups["hh"].Value); | |
m.Minute = uint.Parse(mm.Groups["mm"].Value); | |
m.Second = uint.Parse(mm.Groups["ss"].Value); | |
if (!m.Validate ()) | |
return "Bad data"; | |
return m.ToString (); | |
} | |
private void sendResponse (string str, Socket io) | |
{ | |
Console.WriteLine ("Response: " + str); | |
byte[] buf = new byte[str.Length * sizeof (char)]; | |
System.Buffer.BlockCopy (str.ToCharArray (), 0, buf, 0, buf.Length); | |
io.Send (buf); | |
Console.WriteLine ("Response sent"); | |
} | |
} | |
class MainClass | |
{ | |
public static void Main (string[] args) | |
{ | |
ProcessingServer s = new ProcessingServer (); | |
s.startListening (); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment