Skip to content

Commit

Permalink
Merge pull request #8 from akon47/develop
Browse files Browse the repository at this point in the history
feat: Write a class summary, fixed some bug
  • Loading branch information
akon47 authored Aug 26, 2021
2 parents c5056c6 + 01b9f00 commit c33359d
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 36 deletions.
7 changes: 7 additions & 0 deletions SimpleTcp/Client/BaseTcpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,14 @@ public abstract class BaseTcpClient : IDisposable
#endregion

#region Public Member
/// <summary>
/// Called when connecting to the server.
/// </summary>
public event ConnectedHandler Connected;

/// <summary>
/// Called when the connection with the server is lost.
/// </summary>
public event DisconnectedHandler Disconnected;
#endregion

Expand Down
15 changes: 15 additions & 0 deletions SimpleTcp/Client/RawTcpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,21 @@ namespace SimpleTcp.Client
public class RawTcpClient : BaseTcpClient
{
#region Public Member
/// <summary>
/// Called when data is received.
/// </summary>
public event DataReceivedEventHandler DataReceived;
#endregion

#region Public Methods
#region Constructor

/// <summary>
/// RawTcpClient
/// </summary>
/// <param name="host">The name of the remote host</param>
/// <param name="port">The port number of the remote host</param>
/// <param name="timeout">connection timeout (ms)</param>
public RawTcpClient(string host = null, int port = -1, int timeout = 3000) : base(host, port, timeout) { }
#endregion

Expand All @@ -30,6 +40,11 @@ public RawTcpClient(string host = null, int port = -1, int timeout = 3000) : bas
return base.ReadExisting();
}

/// <summary>
/// If exist return readed byte
/// If not exist return -1
/// </summary>
/// <returns></returns>
new public int ReadByte()
{
return base.ReadByte();
Expand Down
96 changes: 65 additions & 31 deletions SimpleTcp/Server/BaseTcpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@ public IClient[] Clients
/// </summary>
public int Port { get => (tcpListener?.LocalEndpoint as IPEndPoint)?.Port ?? -1; }

public long TotalReceiveBytes { get; private set; }
/// <summary>
/// Get Total received bytes count
/// </summary>
public long TotalReceivedBytes { get; private set; }

/// <summary>
/// Get Total sended bytes count
/// </summary>
public long TotalSendedBytes { get; private set; }
#endregion

#region Private Member
Expand All @@ -50,12 +58,24 @@ public IClient[] Clients
#endregion

#region Public Member
/// <summary>
/// Client connected event handler
/// </summary>
public event ClientConnectedHandler ClientConnected;

/// <summary>
/// Client disconnected event handler
/// </summary>
public event ClientDisconnectedHandler ClientDisconnected;
#endregion

#region Public Methods
#region Constructor

/// <summary>
/// BaseTcpServer
/// </summary>
/// <param name="port">If you specify a valid port, the server starts immediately.</param>
public BaseTcpServer(int port = -1)
{
if(port > 0)
Expand All @@ -65,6 +85,10 @@ public BaseTcpServer(int port = -1)
}
#endregion

/// <summary>
/// Start tcp server
/// </summary>
/// <param name="port">Server port</param>
public void Start(int port)
{
lock (syncObject)
Expand All @@ -89,6 +113,9 @@ public void Start(int port)
}
}

/// <summary>
/// Stop tcp server
/// </summary>
public void Stop()
{
lock (syncObject)
Expand Down Expand Up @@ -137,7 +164,7 @@ private void AcceptTcpClientCallback(IAsyncResult ar)
TcpClient tcpClient = tcpListener.EndAcceptTcpClient(ar);
if(tcpClient != null) // new client connected
{
Connection connection = new Connection(tcpClient);
Connection connection = new Connection(this, tcpClient);
lock(syncObject)
{
connections.Add(connection);
Expand Down Expand Up @@ -185,22 +212,20 @@ private void DisconnectedCallback(Connection connection)

private void DataReceivedCallback(Connection connection, int receivedSize)
{
TotalReceiveBytes += receivedSize;
TotalReceivedBytes += receivedSize;
OnDataReceived(connection, receivedSize);
}
#endregion


protected class Connection : IClient
{
#region Properties
public TcpClient TcpClient { get; private set; }
public IPEndPoint IPEndPoint { get => TcpClient?.Client?.RemoteEndPoint as IPEndPoint; }
public NetworkStream NetworkStream { get { try { return TcpClient?.GetStream(); } catch { return null; } } }

public int BytesToRead { get => ringBuffer.Count; }
public int BytesToRead { get => _ringBuffer.Count; }
public long DropBytes { get; private set; } = 0;
public long SendBytes { get; private set; } = 0;
public long SendedBytes { get; private set; } = 0;
public long ReceivedBytes { get; private set; } = 0;
#endregion

Expand All @@ -209,69 +234,77 @@ protected class Connection : IClient

#region Private Members
private object syncObject = new object();
private byte[] buffer;
private RingBuffer ringBuffer;
private DataReceivedCallback dataReceived;
private DisconnectedCallback disconnected;
private byte[] _buffer;
private RingBuffer _ringBuffer;
private DataReceivedCallback _dataReceived;
private DisconnectedCallback _disconnected;
private BaseTcpServer _baseTcpServer;
#endregion

public Connection(TcpClient tcpClient)
public Connection(BaseTcpServer baseTcpServer, TcpClient tcpClient)
{
TcpClient = tcpClient;
buffer = new byte[tcpClient.ReceiveBufferSize];
ringBuffer = new RingBuffer(tcpClient.ReceiveBufferSize);
_buffer = new byte[tcpClient.ReceiveBufferSize];
_ringBuffer = new RingBuffer(tcpClient.ReceiveBufferSize);
_baseTcpServer = baseTcpServer;
}

public void BeginRead(DataReceivedCallback dataReceivedCallback, DisconnectedCallback disconnectedCallback)
{
dataReceived = dataReceivedCallback;
disconnected = disconnectedCallback;
NetworkStream?.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(ReadCallback), this);
_dataReceived = dataReceivedCallback;
_disconnected = disconnectedCallback;
try
{
TcpClient?.GetStream()?.BeginRead(_buffer, 0, _buffer.Length, new AsyncCallback(ReadCallback), this);
}
catch
{
_disconnected?.Invoke(this);
}
}

private void ReadCallback(IAsyncResult ar)
{
if (ar.AsyncState is Connection connection)
{
int readSize = 0;
int readSize;
try
{
readSize = connection.NetworkStream?.EndRead(ar) ?? 0;
readSize = connection.TcpClient.GetStream()?.EndRead(ar) ?? 0;
}
catch { }
catch { readSize = 0; }

if (readSize == 0) // client disconnected when readSize is zero
{
disconnected?.Invoke(this);
_disconnected?.Invoke(this);
}
else
{
int writeBytes = ringBuffer.Write(buffer, 0, readSize);
int writeBytes = _ringBuffer.Write(_buffer, 0, readSize);
if(writeBytes < readSize)
{
DropBytes += (readSize - writeBytes);
}
ReceivedBytes += readSize;

dataReceived?.Invoke(this, readSize);
BeginRead(dataReceived, disconnected);
_dataReceived?.Invoke(this, readSize);
BeginRead(_dataReceived, _disconnected);
}
}
}

public int Read(byte[] buffer, int offset, int count)
{
return ringBuffer.Read(buffer, offset, count);
return _ringBuffer.Read(buffer, offset, count);
}

public byte[] ReadExisting()
{
return ringBuffer.ReadExisting();
return _ringBuffer.ReadExisting();
}

public int ReadByte()
{
return ringBuffer.ReadByte();
return _ringBuffer.ReadByte();
}

public void Write(byte[] buffer, int offset, int count)
Expand All @@ -280,18 +313,19 @@ public void Write(byte[] buffer, int offset, int count)
{
lock (syncObject)
{
NetworkStream networkStream = NetworkStream;
NetworkStream networkStream = TcpClient?.GetStream();
if (networkStream.CanWrite)
{
networkStream.Write(buffer, offset, count);
networkStream.Flush();
SendBytes += count;
SendedBytes += count;
_baseTcpServer.TotalSendedBytes += count;
}
}
}
catch
{
disconnected?.Invoke(this);
_disconnected?.Invoke(this);
}
}

Expand Down
27 changes: 26 additions & 1 deletion SimpleTcp/Server/IClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,37 @@ namespace SimpleTcp.Server
{
public interface IClient
{
/// <summary>
/// Client
/// </summary>
TcpClient TcpClient { get; }

/// <summary>
/// The client's address.
/// </summary>
IPEndPoint IPEndPoint { get; }

/// <summary>
/// Gets the number of bytes of data in the receive buffer.
/// </summary>
int BytesToRead { get; }

/// <summary>
/// The data in the receive buffer overflowed to get the number of bytes lost.
/// </summary>
long DropBytes { get; }
long SendBytes { get; }

/// <summary>
/// Get the total number of bytes sent to that client.
/// </summary>
long SendedBytes { get; }

/// <summary>
/// Get the total number of bytes received from the client.
/// </summary>
long ReceivedBytes { get; }


int Read(byte[] buffer, int offset, int count);
byte[] ReadExisting();
int ReadByte();
Expand Down
11 changes: 11 additions & 0 deletions SimpleTcp/Server/IPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,19 @@ namespace SimpleTcp.Server
{
public interface IPacket
{
/// <summary>
/// The client that sent the packet.
/// </summary>
TcpClient TcpClient { get; }

/// <summary>
/// Address of the client that sent the packet
/// </summary>
IPEndPoint IPEndPoint { get; }

/// <summary>
/// Packet data
/// </summary>
byte[] PacketData { get; }
}
}
4 changes: 4 additions & 0 deletions SimpleTcp/Server/PacketTcpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public class PacketTcpServer : BaseTcpServer
#region Public Methods

#region Constructor
/// <summary>
/// PacketTcpServer
/// </summary>
/// <param name="port">If you specify a valid port, the server starts immediately.</param>
public PacketTcpServer(int port = -1) : base(port) { }
#endregion

Expand Down
29 changes: 29 additions & 0 deletions SimpleTcp/Server/RawTcpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,44 @@ namespace SimpleTcp.Server
public class RawTcpServer : BaseTcpServer
{
#region Public Member
/// <summary>
/// Called when data is received.
/// </summary>
public event DataReceivedEventHandler DataReceived;
#endregion

#region Public Methods

#region Constructor
/// <summary>
/// RawTcpServer
/// </summary>
/// <param name="port">If you specify a valid port, the server starts immediately.</param>
public RawTcpServer(int port = -1) : base(port) { }
#endregion

/// <summary>
/// Send data
/// </summary>
/// <param name="tcpClient"></param>
/// <param name="buffer">buffer</param>
/// <param name="offset">offset</param>
/// <param name="count">count</param>
public void Write(TcpClient tcpClient, byte[] buffer, int offset, int count)
{
base.GetClient(tcpClient)?.Write(buffer, offset, count);
}

/// <summary>
/// Send data to all connected clinets
/// </summary>
/// <param name="buffer">buffer</param>
/// <param name="offset">offset</param>
/// <param name="count">count</param>
new public void WriteToAllClients(byte[] buffer, int offset, int count)
{
base.WriteToAllClients(buffer, offset, count);
}
#endregion

#region Protected Methods
Expand Down
8 changes: 4 additions & 4 deletions SimpleTcp/SimpleTcp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
<PackageProjectUrl>https://github.com/akon47/SimpleTcp</PackageProjectUrl>
<RepositoryUrl>https://github.com/akon47/SimpleTcp</RepositoryUrl>
<PackageTags>SimpleTcp Tcp Client Server Socket</PackageTags>
<PackageReleaseNotes>change .Net Frameworkd to .Net Standard</PackageReleaseNotes>
<AssemblyVersion>1.0.2.0</AssemblyVersion>
<FileVersion>1.0.2.0</FileVersion>
<Version>1.0.2</Version>
<PackageReleaseNotes>Write a class summary, fixed some bug</PackageReleaseNotes>
<AssemblyVersion>1.0.3.0</AssemblyVersion>
<FileVersion>1.0.3.0</FileVersion>
<Version>1.0.3</Version>
<PackageIcon>logo.png</PackageIcon>
<PackageIconUrl />
<Authors>Kim Hwan</Authors>
Expand Down

0 comments on commit c33359d

Please sign in to comment.