Skip to content

Commit

Permalink
优化客户端核心类
Browse files Browse the repository at this point in the history
  • Loading branch information
fengma312 committed Jan 12, 2018
1 parent 61205ef commit 4ed2933
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 52 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
socket.core
===
This is a socket framework written based on C # standard2.0, the interface design is simple, separate thread operation, does not affect the caller. Can be used in the net Framework 4.x.x / standard assembly, in the window (IOCP) / linux normal operation.
This is a socket framework based on C # net standard2.0 write, can be used for .NET Framework / dotnet core assembly, can run in window (IOCP) / linux (epoll) .Use asynchronous connection, asynchronous send, asynchronous receive, Performance burst tables, and pass the stress test.
---

下面有中文文档
Expand Down Expand Up @@ -68,7 +68,7 @@ Server other methods introduced
socket.core
===

这是一个基于C# standard2.0 写的socket框架,接口设计简单,单独线程运行,不影响调用方。可使用于net Framework 4.x.x/standard程序集,能在window(IOCP)/linux正常运行.
这是一个基于C# .net standard2.0 写的socket框架,可使用于.net Framework/dotnet core程序集,能在window(IOCP)/linux(epoll)运行.使用异步连接,异步发送,异步接收,性能爆表,并且通过压力测试。
---
安装NuGet:
Package Manager: Install-Package socket.core
Expand Down
75 changes: 34 additions & 41 deletions socket.core/Client/TcpClients.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal class TcpClients
/// <summary>
/// 发送端SocketAsyncEventArgs对象重用池,发送套接字操作
/// </summary>
private SocketAsyncEventArgsPool m_sendPool;
private SocketAsyncEventArgsPool m_sendPool;
/// <summary>
/// 用于每个套接字I/O操作的缓冲区大小
/// </summary>
Expand Down Expand Up @@ -69,7 +69,7 @@ public bool Connected
{
get
{
if(socket==null)
if (socket == null)
{
return false;
}
Expand All @@ -95,16 +95,6 @@ private void Init()
{
buffer_receive = new byte[m_receiveBufferSize];
sendQueue = new ConcurrentQueue<SendingQueue>();
//分配的发送对象池socketasynceventargs,不分配缓存
SocketAsyncEventArgs saea_send;
for (int i = 0; i < m_minSendSocketAsyncEventArgs; i++)
{
//预先发送端分配一组可重用的消息
saea_send = new SocketAsyncEventArgs();
saea_send.Completed += new EventHandler<SocketAsyncEventArgs>(IO_Completed);
saea_send.UserToken = new AsyncUserToken();
m_sendPool.Push(saea_send);
}
}

/// <summary>
Expand All @@ -125,14 +115,14 @@ internal void Connect(string ip, int port)
}
IPEndPoint localEndPoint = new IPEndPoint(ipaddr, port);
socket = new Socket(localEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
socket.NoDelay = true;
SocketAsyncEventArgs connSocketAsyncEventArgs = new SocketAsyncEventArgs();
connSocketAsyncEventArgs.AcceptSocket = socket;
connSocketAsyncEventArgs.RemoteEndPoint = localEndPoint;
connSocketAsyncEventArgs.Completed += IO_Completed;
if (!socket.ConnectAsync(connSocketAsyncEventArgs))
{
ProcessConnect(connSocketAsyncEventArgs);
}
}
}

/// <summary>
Expand All @@ -145,10 +135,9 @@ private void ProcessConnect(SocketAsyncEventArgs e)
if (e.SocketError == SocketError.Success)
{
receiveSocketAsyncEventArgs = new SocketAsyncEventArgs();
receiveSocketAsyncEventArgs.AcceptSocket = e.AcceptSocket;
receiveSocketAsyncEventArgs.SetBuffer(buffer_receive, 0, buffer_receive.Length);
receiveSocketAsyncEventArgs.Completed += IO_Completed;
if (!e.AcceptSocket.ReceiveAsync(receiveSocketAsyncEventArgs))
if (!socket.ReceiveAsync(receiveSocketAsyncEventArgs))
{
ProcessReceive(receiveSocketAsyncEventArgs);
}
Expand All @@ -162,7 +151,8 @@ private void ProcessConnect(SocketAsyncEventArgs e)
StartSend();
}));
thread.IsBackground = true;
thread.Start();
thread.Priority = ThreadPriority.AboveNormal;
thread.Start();
}
else
{
Expand Down Expand Up @@ -214,10 +204,13 @@ private void ProcessReceive(SocketAsyncEventArgs e)
{
OnReceive(data);
}
//将收到的数据回显给客户端
if (!e.AcceptSocket.ReceiveAsync(e))
//将收到的数据回显给客户端
if (socket.Connected == true)
{
ProcessReceive(e);
if (!socket.ReceiveAsync(e))
{
ProcessReceive(e);
}
}
}
else
Expand All @@ -226,7 +219,7 @@ private void ProcessReceive(SocketAsyncEventArgs e)
}
}
#endregion

#region 发送

/// <summary>
Expand All @@ -246,7 +239,7 @@ internal void Send(byte[] data, int offset, int length)
/// </summary>
private void StartSend()
{
while (Connected)
while (true)
{
SendingQueue sending;
if (sendQueue.TryDequeue(out sending))
Expand All @@ -268,20 +261,22 @@ private void StartSend()
/// <param name="length">长度</param>
internal void Send(SendingQueue sendQuere)
{
if (!socket.Connected)
{
return;
}
mutex.WaitOne();
//如果发送池为空时,临时新建一个放入池中
if (m_sendPool.Count == 0)
{
SocketAsyncEventArgs saea_send = new SocketAsyncEventArgs();
saea_send.Completed += new EventHandler<SocketAsyncEventArgs>(IO_Completed);
saea_send.UserToken = new AsyncUserToken();
m_sendPool.Push(saea_send);
}
SocketAsyncEventArgs sendEventArgs = m_sendPool.Pop();
mutex.ReleaseMutex();
sendEventArgs.AcceptSocket = socket;
sendEventArgs.SetBuffer(sendQuere.data, sendQuere.offset, sendQuere.length);
if (!sendEventArgs.AcceptSocket.SendAsync(sendEventArgs))
if (!socket.SendAsync(sendEventArgs))
{
ProcessSend(sendEventArgs);
}
Expand All @@ -301,10 +296,6 @@ private void ProcessSend(SocketAsyncEventArgs e)
OnSend(e.BytesTransferred);
}
}
else
{
CloseClientSocket(e);
}
}

#endregion
Expand All @@ -324,22 +315,24 @@ internal void Close()
/// <param name="e">操作对象</param>
private void CloseClientSocket(SocketAsyncEventArgs e)
{
if (socket.Connected)
if (!socket.Connected)
{
return;
}
// 关闭与客户端关联的套接字
try
{
socket.Shutdown(SocketShutdown.Both);
}
// 抛出客户端进程已经关闭
catch (Exception) { }
socket.Close();
m_sendPool.Clear();
if (OnClose != null)
if (e.LastOperation == SocketAsyncOperation.Receive)
{
OnClose();
// 关闭与客户端关联的套接字
try
{
socket.Shutdown(SocketShutdown.Both);
}
// 抛出客户端进程已经关闭
catch (Exception) { }
socket.Close();
if (OnClose != null)
{
OnClose();
}
}
}
#endregion
Expand Down
8 changes: 4 additions & 4 deletions socket.core/Common/SocketAsyncEventArgsPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ public int Count
/// <summary>
/// 清空
/// </summary>
public void Clear()
{
m_pool.Clear();
}
//public void Clear()
//{
// m_pool.Clear();
//}

}
}
1 change: 1 addition & 0 deletions socket.core/Server/TcpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ internal void Start(int port)
StartSend();
}));
thread.IsBackground = true;
thread.Priority = ThreadPriority.AboveNormal;
thread.Start();
//超时机制
if (overtime > 0)
Expand Down
2 changes: 1 addition & 1 deletion socket.core/socket.core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<Copyright>[email protected]</Copyright>
<PackageReleaseNotes>使用线程进行异步发送</PackageReleaseNotes>
<RepositoryType>socket.core</RepositoryType>
<Version>1.0.7</Version>
<Version>1.0.8</Version>
</PropertyGroup>

</Project>
4 changes: 2 additions & 2 deletions test.window.client/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<add key="ip" value="127.0.0.1"/>
<add key="port" value="5555"/>
<!--接收缓存-->
<add key="receiveBufferSize" value="1024"/>
<add key="receiveBufferSize" value="10240"/>
<!--自动发送多少次数-->
<add key="sendnumber" value="10000"/>
<add key="sendnumber" value="1"/>
<!--发送内容-->
<add key="senddata" value="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"/>
</appSettings>
Expand Down
7 changes: 5 additions & 2 deletions test.window.client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ private void Test()
byte[] data = Encoding.UTF8.GetBytes(senddata);


//单个实现测试
////单个实现测试
Push client = new Push(receiveBufferSize, ip, port);
//Pull client = new Pull(receiveBufferSize, ip, port);
//Pack client = new Pack(receiveBufferSize, ip, port, 0xff);
Expand All @@ -37,6 +37,9 @@ private void Test()
client.Send(data, 0, data.Length);
}

//Thread.Sleep(1000*10);
//client.Close();

//多线程测试

//ThreadPool.QueueUserWorkItem(new WaitCallback((object o) =>
Expand Down Expand Up @@ -83,7 +86,7 @@ private void Test()


//多对象测试
//for (int j = 0; j < 20; j++)
//for (int j = 0; j < 200; j++)
//{
// Push client2 = new Push(receiveBufferSize, ip, port);
// //Pull client2 = new Pull(receiveBufferSize, ip, port);
Expand Down

0 comments on commit 4ed2933

Please sign in to comment.