Skip to content

Commit

Permalink
Send data asynchronously using the thread pool
Browse files Browse the repository at this point in the history
使用线程池异步发送数据
  • Loading branch information
mf authored and mf committed Jan 11, 2018
1 parent 5d049f6 commit f58d219
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ Note: connectId (guid) represents a connection object, data (byte []), success (
* 4. Send a message
> Server server.Send (connectId, data, offset, length);
> Client client.Send (data, offset, length);
* 5. Triggered receive events
* 5. Trigger sent events
> Server server.OnSend (connectId, length);
> Client client.OnSend (length);
* 6. Triggered receive events
> Server server.OnReceive (connectId, data);
> Client client.OnReceive (data);
* 6. Close the connection
* 7. Close the connection
> Server server.Close (connectId);
> Client client.Close ();
* 7. Trigger to close the connection event
* 8. Trigger to close the connection event
> Server server.OnClose (connectId);
> Client client.OnClose ();
Expand Down Expand Up @@ -91,13 +94,16 @@ Paket CLI:paket add socket.core
* 4.发送消息
>服务端 server.Send(connectId,data,offset,length);
>客户端 client.Send(data,offset,length);
* 5.触发接收事件
* 5.触发已发送事件
>服务端 server.OnSend(connectId,length);
>客户端 client.OnSend(length);
* 6.触发接收事件
>服务端 server.OnReceive(connectId, data);
>客户端 client.OnReceive(data);
* 6.关闭连接
* 7.关闭连接
>服务端 server.Close(connectId);
>客户端 client.Close();
* 7.触发关闭连接事件
* 8.触发关闭连接事件
>服务端 server.OnClose(connectId);
>客户端 client.OnClose();

Expand Down
6 changes: 2 additions & 4 deletions socket.core/Client/TcpClients.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,7 @@ internal void Connect(string ip, int port)
{
ProcessConnect(connSocketAsyncEventArgs);
}
//发送线程池
ThreadPool.SetMinThreads(1, 1);
ThreadPool.SetMaxThreads(workingThreadNumber, workingThreadNumber);
//发送线程池
for (int i = 1; i <= workingThreadNumber; i++)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(StartSend));
Expand Down Expand Up @@ -246,7 +244,7 @@ private void StartSend(object obj)
}
else
{
Thread.Sleep(20);
Thread.Sleep(2);
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions socket.core/Server/TcpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,7 @@ internal void Start(int port)
listenSocket.Listen(1000);
//在监听套接字上接受
StartAccept(null);
//发送线程池
ThreadPool.SetMinThreads(1, 1);
ThreadPool.SetMaxThreads(workingThreadNumber, workingThreadNumber);
//发送线程池
for (int i = 1; i <= workingThreadNumber; i++)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(StartSend), i.ToString());
Expand Down Expand Up @@ -320,7 +318,7 @@ private void StartSend(object obj)
/// <param name="length">长度</param>
internal void Send(Guid connectId, byte[] data, int offset, int length)
{
sendQueue.Enqueue(new SendingQueue() { connectId = connectId, data = data, offset = offset, length = length });
sendQueue.Enqueue(new SendingQueue() { connectId = connectId, data = data, offset = offset, length = length });
}

/// <summary>
Expand Down

0 comments on commit f58d219

Please sign in to comment.