Skip to content

Commit

Permalink
Minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
BenHall committed Sep 24, 2010
1 parent 8e668cb commit ec60849
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
21 changes: 11 additions & 10 deletions src/PersistentDlr/PersistentDlr.Console/Program.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PersistentDlr.Console
namespace PersistentDlr.Console
{
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine("Starting...");
static void Main(string[] args) {
SetTitle("Starting...");
Agent agent = new Agent();
agent.Start();

System.Console.WriteLine("Started");
SetTitle("Started");

System.Console.ReadLine();
agent.Stop();

SetTitle("Stopped");
}

private static void SetTitle(string title) {
System.Console.Title = "PersistentDlr.Console " + title;
System.Console.WriteLine(title);
}
}
}
11 changes: 7 additions & 4 deletions src/PersistentDlr/PersistentDlr/NetworkStreamHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ public static string ReadStreamIntoString(NetworkStream ns)

if (ns.CanRead)
{
var recv = ns.Read(data, 0, data.Length);
stringData = Encoding.UTF8.GetString(data, 0, recv);
stringData = stringData.Trim(new[] { '\r', '\n', '\0' });
Console.WriteLine("Got: " + stringData);
if (ns.DataAvailable) {
ns.ReadTimeout = 1;
var recv = ns.Read(data, 0, data.Length);
stringData = Encoding.UTF8.GetString(data, 0, recv);
stringData = stringData.Trim(new[] {'\r', '\n', '\0'});
Console.WriteLine("Got: " + stringData);
}
}

return stringData;
Expand Down
14 changes: 11 additions & 3 deletions src/PersistentDlr/PersistentDlr/RequestHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net.Sockets;
using System;
using System.Net.Sockets;
using System.Threading;
using PersistentDlr.Dlr;

Expand Down Expand Up @@ -26,11 +27,17 @@ public void ProcessConnections()
TcpClient client = _server.AcceptTcpClient();
NetworkStream ns = client.GetStream();

string request = NetworkStreamHandler.ReadStreamIntoString(ns);
try
{
string request = NetworkStreamHandler.ReadStreamIntoString(ns);

while (request!= "quit" && client.Connected) {
ExecuteRequest(ns, request);
request = NetworkStreamHandler.ReadStreamIntoString(ns);
//ns.Close(1000);
}
catch (Exception ex)
{
Console.WriteLine("It's all gone wrong - " + ex.Message);
}

ns.Close();
Expand All @@ -43,6 +50,7 @@ public void ProcessConnections()
private void ExecuteRequest(NetworkStream ns, string request) {
if (!string.IsNullOrEmpty(request)) {
var response = _dlrHost.Execute(request);
Console.WriteLine("Result: " + response);
WriteResponse(ns, response);
}
}
Expand Down

0 comments on commit ec60849

Please sign in to comment.