Skip to content
10 changes: 6 additions & 4 deletions SimpleTCP.Tests/CommTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ public class CommTest
public void SimpleCommTest()
{
SimpleTcpServer server = new SimpleTcpServer().Start(8910);
SimpleTcpClient client = new SimpleTcpClient().Connect(server.GetListeningIPs().FirstOrDefault(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).ToString(), 8910);
SimpleTcpClient client = new SimpleTcpClient(new SimpleTcpParam{Name = "Alex"}).Connect(server.GetListeningIPs().FirstOrDefault(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).ToString(), 8910);

server.DelimiterDataReceived += (sender, msg) => {
server.DelimiterDataReceived += (sender, msg) =>
{
_serverRx.Add(msg.MessageString);
string serverReply = Guid.NewGuid().ToString();
msg.ReplyLine(serverReply);
_serverTx.Add(serverReply);
};

client.DelimiterDataReceived += (sender, msg) => {
client.DelimiterDataReceived += (sender, msg) =>
{
_clientRx.Add(msg.MessageString);
};

Expand Down Expand Up @@ -69,7 +71,7 @@ public void SimpleCommTest()

Assert.IsTrue(true);


}
}
}
5 changes: 3 additions & 2 deletions SimpleTCP.Tests/ServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ public class ServerTests : IDisposable
public ServerTests()
{
_server = new SimpleTcpServer().Start(_serverPort);
}
}


public void Dispose()
public void Dispose()
{
if (_server.IsStarted)
_server.Stop();
Expand Down
53 changes: 53 additions & 0 deletions SimpleTCP/MessagemUdp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.Linq;
using System.Net.Sockets;
using System.Text;

namespace SimpleTCP
{
public class MessagemUdp
{
private UdpClient _udpClient;
private Encoding _encoder;
private byte _writeLineDelimiter;
private bool _autoTrim;

public byte[] Data { get; }

public UdpClient TcpClient => _udpClient;

internal MessagemUdp(byte[] data, UdpClient udpClient, Encoding stringEncoder, byte lineDelimiter, bool autoTrim)
{
Data = data;
_udpClient = udpClient;
_encoder = stringEncoder;
_writeLineDelimiter = lineDelimiter;
_autoTrim = autoTrim;
}

public string MessageString => _autoTrim ? _encoder.GetString(Data).Trim() : _encoder.GetString(Data);

public void Reply(byte[] data)
{
_udpClient.Send(data, data.Length);
}

public void Reply(string data)
{
if (string.IsNullOrEmpty(data)) { return; }
Reply(_encoder.GetBytes(data));
}

public void ReplyLine(string data)
{
if (string.IsNullOrEmpty(data)) { return; }
if (data.LastOrDefault() != _writeLineDelimiter)
{
Reply(data + _encoder.GetString(new[] { _writeLineDelimiter }));
}
else
{
Reply(data);
}
}
}
}
7 changes: 1 addition & 6 deletions SimpleTCP/Server/ConnectedClient.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace SimpleTCP
{
Expand Down
9 changes: 3 additions & 6 deletions SimpleTCP/Server/ServerListener.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace SimpleTCP.Server
{

internal class ServerListener
{
private TcpListenerEx _listener = null;
Expand Down Expand Up @@ -104,7 +101,7 @@ private void RunLoopStep()

if (_listener.Pending())
{
var newClient = _listener.AcceptTcpClient();
var newClient = _listener.AcceptTcpClient();
_connectedClients.Add(newClient);
_parent.NotifyClientConnected(this, newClient);
}
Expand Down
19 changes: 15 additions & 4 deletions SimpleTCP/Server/TcpListenerEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,29 @@ public class TcpListenerEx : TcpListener
/// </summary>
/// <param name="localEP">An <see cref="T:System.Net.IPEndPoint"/> that represents the local endpoint to which to bind the listener <see cref="T:System.Net.Sockets.Socket"/>. </param><exception cref="T:System.ArgumentNullException"><paramref name="localEP"/> is null. </exception>
public TcpListenerEx(IPEndPoint localEP) : base(localEP)
{
}
{
AcceptTcpClient();


}


public TcpClient AcceptTcpClientX()
{
var a = this.AcceptTcpClient();

return a;
}

/// <summary>
/// Initializes a new instance of the <see cref="T:System.Net.Sockets.TcpListener"/> class that listens for incoming connection attempts on the specified local IP address and port number.
/// </summary>
/// <param name="localaddr">An <see cref="T:System.Net.IPAddress"/> that represents the local IP address. </param><param name="port">The port on which to listen for incoming connection attempts. </param><exception cref="T:System.ArgumentNullException"><paramref name="localaddr"/> is null. </exception><exception cref="T:System.ArgumentOutOfRangeException"><paramref name="port"/> is not between <see cref="F:System.Net.IPEndPoint.MinPort"/> and <see cref="F:System.Net.IPEndPoint.MaxPort"/>. </exception>
public TcpListenerEx(IPAddress localaddr, int port) : base(localaddr, port)
{
}
}

public new bool Active
public new bool Active
{
get { return base.Active; }
}
Expand Down
3 changes: 3 additions & 0 deletions SimpleTCP/SimpleTCP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,15 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Message.cs" />
<Compile Include="MessagemUdp.cs" />
<Compile Include="Server\ConnectedClient.cs" />
<Compile Include="Server\ServerListener.cs" />
<Compile Include="Server\TcpListenerEx.cs" />
<Compile Include="SimpleTcpClient.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SimpleTcpParam.cs" />
<Compile Include="SimpleTcpServer.cs" />
<Compile Include="SimpleUdpClient.cs" />
</ItemGroup>
<ItemGroup>
<None Include="SimpleTCP.nuspec" />
Expand Down
40 changes: 33 additions & 7 deletions SimpleTCP/SimpleTcpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,29 @@
using System.Diagnostics;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace SimpleTCP
{
public class SimpleTcpClient : IDisposable
public class SimpleTcpClient : IDisposable
{
public SimpleTcpClient()
private readonly SimpleTcpParam _param;

public SimpleTcpClient()
{
StringEncoder = System.Text.Encoding.UTF8;
ReadLoopIntervalMs = 10;
Delimiter = 0x13;
}

public SimpleTcpClient(SimpleTcpParam param)
{
_param = param;
StringEncoder = System.Text.Encoding.UTF8;
ReadLoopIntervalMs = 10;
Delimiter = 0x13;
}

private Thread _rxThread = null;
private List<byte> _queuedMsg = new List<byte>();
public byte Delimiter { get; set; }
Expand All @@ -38,8 +46,8 @@ public SimpleTcpClient Connect(string hostNameOrIpAddress, int port)
throw new ArgumentNullException("hostNameOrIpAddress");
}

_client = new TcpClient();
_client.Connect(hostNameOrIpAddress, port);
_client = new TcpClient();
_client.Connect(hostNameOrIpAddress, port);

StartRxThread();

Expand Down Expand Up @@ -145,7 +153,7 @@ private void NotifyEndTransmissionRx(TcpClient client, byte[] msg)
public void Write(byte[] data)
{
if (_client == null) { throw new Exception("Cannot send data to a null TcpClient (check to see if Connect was called)"); }
_client.GetStream().Write(data, 0, data.Length);
_client.GetStream().Write(data, 0, data.Length);
}

public void Write(string data)
Expand Down Expand Up @@ -184,6 +192,24 @@ public Message WriteLineAndGetReply(string data, TimeSpan timeout)
return mReply;
}

public Message WriteLineAndGetReply(byte[] data, TimeSpan timeout)
{
Message mReply = null;
this.DataReceived += (s, e) => { mReply = e; };
Write(data);

Stopwatch sw = new Stopwatch();
sw.Start();

while (mReply == null && sw.Elapsed < timeout)
{
System.Threading.Thread.Sleep(10);
}

return mReply;
}



#region IDisposable Support
private bool disposedValue = false; // To detect redundant calls
Expand Down
7 changes: 7 additions & 0 deletions SimpleTCP/SimpleTcpParam.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace SimpleTCP
{
public class SimpleTcpParam
{
public string Name { get; set; }
}
}
Loading