diff --git a/SimpleTcp/Client/BaseTcpClient.cs b/SimpleTcp/Client/BaseTcpClient.cs index f14c1f5..ab1abb0 100644 --- a/SimpleTcp/Client/BaseTcpClient.cs +++ b/SimpleTcp/Client/BaseTcpClient.cs @@ -38,7 +38,14 @@ public abstract class BaseTcpClient : IDisposable #endregion #region Public Member + /// + /// Called when connecting to the server. + /// public event ConnectedHandler Connected; + + /// + /// Called when the connection with the server is lost. + /// public event DisconnectedHandler Disconnected; #endregion diff --git a/SimpleTcp/Client/RawTcpClient.cs b/SimpleTcp/Client/RawTcpClient.cs index ea2a46a..0099ba2 100644 --- a/SimpleTcp/Client/RawTcpClient.cs +++ b/SimpleTcp/Client/RawTcpClient.cs @@ -12,11 +12,21 @@ namespace SimpleTcp.Client public class RawTcpClient : BaseTcpClient { #region Public Member + /// + /// Called when data is received. + /// public event DataReceivedEventHandler DataReceived; #endregion #region Public Methods #region Constructor + + /// + /// RawTcpClient + /// + /// The name of the remote host + /// The port number of the remote host + /// connection timeout (ms) public RawTcpClient(string host = null, int port = -1, int timeout = 3000) : base(host, port, timeout) { } #endregion @@ -30,6 +40,11 @@ public RawTcpClient(string host = null, int port = -1, int timeout = 3000) : bas return base.ReadExisting(); } + /// + /// If exist return readed byte + /// If not exist return -1 + /// + /// new public int ReadByte() { return base.ReadByte(); diff --git a/SimpleTcp/Server/BaseTcpServer.cs b/SimpleTcp/Server/BaseTcpServer.cs index 46f2691..501f4f7 100644 --- a/SimpleTcp/Server/BaseTcpServer.cs +++ b/SimpleTcp/Server/BaseTcpServer.cs @@ -36,7 +36,15 @@ public IClient[] Clients /// public int Port { get => (tcpListener?.LocalEndpoint as IPEndPoint)?.Port ?? -1; } - public long TotalReceiveBytes { get; private set; } + /// + /// Get Total received bytes count + /// + public long TotalReceivedBytes { get; private set; } + + /// + /// Get Total sended bytes count + /// + public long TotalSendedBytes { get; private set; } #endregion #region Private Member @@ -50,12 +58,24 @@ public IClient[] Clients #endregion #region Public Member + /// + /// Client connected event handler + /// public event ClientConnectedHandler ClientConnected; + + /// + /// Client disconnected event handler + /// public event ClientDisconnectedHandler ClientDisconnected; #endregion #region Public Methods #region Constructor + + /// + /// BaseTcpServer + /// + /// If you specify a valid port, the server starts immediately. public BaseTcpServer(int port = -1) { if(port > 0) @@ -65,6 +85,10 @@ public BaseTcpServer(int port = -1) } #endregion + /// + /// Start tcp server + /// + /// Server port public void Start(int port) { lock (syncObject) @@ -89,6 +113,9 @@ public void Start(int port) } } + /// + /// Stop tcp server + /// public void Stop() { lock (syncObject) @@ -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); @@ -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 @@ -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) @@ -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); } } diff --git a/SimpleTcp/Server/IClient.cs b/SimpleTcp/Server/IClient.cs index 555a3fb..689c64f 100644 --- a/SimpleTcp/Server/IClient.cs +++ b/SimpleTcp/Server/IClient.cs @@ -10,12 +10,37 @@ namespace SimpleTcp.Server { public interface IClient { + /// + /// Client + /// TcpClient TcpClient { get; } + + /// + /// The client's address. + /// IPEndPoint IPEndPoint { get; } + + /// + /// Gets the number of bytes of data in the receive buffer. + /// int BytesToRead { get; } + + /// + /// The data in the receive buffer overflowed to get the number of bytes lost. + /// long DropBytes { get; } - long SendBytes { get; } + + /// + /// Get the total number of bytes sent to that client. + /// + long SendedBytes { get; } + + /// + /// Get the total number of bytes received from the client. + /// long ReceivedBytes { get; } + + int Read(byte[] buffer, int offset, int count); byte[] ReadExisting(); int ReadByte(); diff --git a/SimpleTcp/Server/IPacket.cs b/SimpleTcp/Server/IPacket.cs index 03e8c3e..6c1451c 100644 --- a/SimpleTcp/Server/IPacket.cs +++ b/SimpleTcp/Server/IPacket.cs @@ -10,8 +10,19 @@ namespace SimpleTcp.Server { public interface IPacket { + /// + /// The client that sent the packet. + /// TcpClient TcpClient { get; } + + /// + /// Address of the client that sent the packet + /// IPEndPoint IPEndPoint { get; } + + /// + /// Packet data + /// byte[] PacketData { get; } } } diff --git a/SimpleTcp/Server/PacketTcpServer.cs b/SimpleTcp/Server/PacketTcpServer.cs index a0db72f..4140523 100644 --- a/SimpleTcp/Server/PacketTcpServer.cs +++ b/SimpleTcp/Server/PacketTcpServer.cs @@ -23,6 +23,10 @@ public class PacketTcpServer : BaseTcpServer #region Public Methods #region Constructor + /// + /// PacketTcpServer + /// + /// If you specify a valid port, the server starts immediately. public PacketTcpServer(int port = -1) : base(port) { } #endregion diff --git a/SimpleTcp/Server/RawTcpServer.cs b/SimpleTcp/Server/RawTcpServer.cs index ee018de..12ac16f 100644 --- a/SimpleTcp/Server/RawTcpServer.cs +++ b/SimpleTcp/Server/RawTcpServer.cs @@ -12,15 +12,44 @@ namespace SimpleTcp.Server public class RawTcpServer : BaseTcpServer { #region Public Member + /// + /// Called when data is received. + /// public event DataReceivedEventHandler DataReceived; #endregion #region Public Methods #region Constructor + /// + /// RawTcpServer + /// + /// If you specify a valid port, the server starts immediately. public RawTcpServer(int port = -1) : base(port) { } #endregion + /// + /// Send data + /// + /// + /// buffer + /// offset + /// count + public void Write(TcpClient tcpClient, byte[] buffer, int offset, int count) + { + base.GetClient(tcpClient)?.Write(buffer, offset, count); + } + + /// + /// Send data to all connected clinets + /// + /// buffer + /// offset + /// count + new public void WriteToAllClients(byte[] buffer, int offset, int count) + { + base.WriteToAllClients(buffer, offset, count); + } #endregion #region Protected Methods diff --git a/SimpleTcp/SimpleTcp.csproj b/SimpleTcp/SimpleTcp.csproj index b1ac7ed..610ab08 100644 --- a/SimpleTcp/SimpleTcp.csproj +++ b/SimpleTcp/SimpleTcp.csproj @@ -8,10 +8,10 @@ https://github.com/akon47/SimpleTcp https://github.com/akon47/SimpleTcp SimpleTcp Tcp Client Server Socket - change .Net Frameworkd to .Net Standard - 1.0.2.0 - 1.0.2.0 - 1.0.2 + Write a class summary, fixed some bug + 1.0.3.0 + 1.0.3.0 + 1.0.3 logo.png Kim Hwan