Skip to content

Commit

Permalink
1:全面提高socket性能。
Browse files Browse the repository at this point in the history
2:连接标识号从guid改为int。
  • Loading branch information
mf authored and mf committed Jan 16, 2018
1 parent aa01c98 commit 5716c52
Show file tree
Hide file tree
Showing 22 changed files with 219 additions and 229 deletions.
2 changes: 1 addition & 1 deletion socket.core/Client/TcpClients.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private void ProcessConnect(SocketAsyncEventArgs e)
StartSend();
}));
thread.IsBackground = true;
thread.Priority = ThreadPriority.AboveNormal;
thread.Priority = ThreadPriority.Highest;
thread.Start();
}
else
Expand Down
26 changes: 13 additions & 13 deletions socket.core/Client/TcpPackClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,17 @@ public bool Connected
/// </summary>
/// <param name="receiveBufferSize">用于每个套接字I/O操作的缓冲区大小(接收端)</param>
/// <param name="headerFlag">包头标记范围0~1023(0x3FF),当包头标识等于0时,不校验包头</param>
public TcpPackClient( int receiveBufferSize,uint headerFlag)
public TcpPackClient(int receiveBufferSize, uint headerFlag)
{
if(headerFlag<0|| headerFlag>1023)
if (headerFlag < 0 || headerFlag > 1023)
{
headerFlag = 0;
}
this.headerFlag = headerFlag;
Thread thread = new Thread(new ThreadStart(() =>
{
queue = new List<byte>();
tcpClients = new TcpClients( receiveBufferSize);
tcpClients = new TcpClients(receiveBufferSize);
tcpClients.OnConnect += TcpServer_eventactionConnect;
tcpClients.OnReceive += TcpServer_eventactionReceive;
tcpClients.OnClose += TcpServer_eventClose;
Expand All @@ -83,13 +83,13 @@ public TcpPackClient( int receiveBufferSize,uint headerFlag)
/// </summary>
/// <param name="ip">ip地址或域名</param>
/// <param name="port">端口</param>
public void Connect(string ip,int port)
public void Connect(string ip, int port)
{
while (tcpClients == null)
{
Thread.Sleep(2);
}
tcpClients.Connect(ip,port);
tcpClients.Connect(ip, port);
}

/// <summary>
Expand Down Expand Up @@ -128,15 +128,15 @@ private void TcpClients_OnSend(int length)
/// 接收通知事件方法
/// </summary>
/// <param name="data">数据</param>
private void TcpServer_eventactionReceive( byte[] data)
private void TcpServer_eventactionReceive(byte[] data)
{
if (OnReceive != null)
{
{
queue.AddRange(data);
byte[] datas=Read();
if(datas != null&& datas.Length>0)
byte[] datas = Read();
if (datas != null && datas.Length > 0)
{
OnReceive( datas);
OnReceive(datas);
}
}
}
Expand Down Expand Up @@ -166,10 +166,10 @@ private void TcpServer_eventClose()
/// <returns></returns>
private byte[] AddHead(byte[] data)
{
uint len =(uint)data.Length;
uint len = (uint)data.Length;
uint header = (headerFlag << 22) | len;
byte[] head=System.BitConverter.GetBytes(header);
return head.Concat(data).ToArray();
byte[] head = System.BitConverter.GetBytes(header);
return head.Concat(data).ToArray();
}

/// <summary>
Expand Down
20 changes: 10 additions & 10 deletions socket.core/Client/TcpPullClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ public class TcpPullClient
/// <summary>
/// 已发送通知事件
/// </summary>
public event Action<int> OnSend;
public event Action<int> OnSend;
/// <summary>
/// 断开连接通知事件
/// </summary>
public event Action OnClose;
/// <summary>
/// 接收到的数据缓存
/// </summary>
private List<byte> queue;
private List<byte> queue;
/// <summary>
/// 互斥锁
/// </summary>
Expand Down Expand Up @@ -63,7 +63,7 @@ public TcpPullClient(int receiveBufferSize)
Thread thread = new Thread(new ThreadStart(() =>
{
queue = new List<byte>();
tcpClients = new TcpClients( receiveBufferSize);
tcpClients = new TcpClients(receiveBufferSize);
tcpClients.OnConnect += TcpServer_eventactionConnect;
tcpClients.OnReceive += TcpServer_eventactionReceive;
tcpClients.OnSend += TcpClients_OnSend;
Expand All @@ -78,13 +78,13 @@ public TcpPullClient(int receiveBufferSize)
/// </summary>
/// <param name="ip">ip地址或域名</param>
/// <param name="port">端口</param>
public void Connect(string ip,int port)
public void Connect(string ip, int port)
{
while (tcpClients == null)
{
Thread.Sleep(2);
}
tcpClients.Connect(ip,port);
tcpClients.Connect(ip, port);
}

/// <summary>
Expand All @@ -103,7 +103,7 @@ private void TcpServer_eventactionConnect(bool success)
/// <param name="data">数据</param>
/// <param name="offset">偏移位</param>
/// <param name="length">长度</param>
public void Send( byte[] data, int offset, int length)
public void Send(byte[] data, int offset, int length)
{
tcpClients.Send(data, offset, length);
}
Expand All @@ -125,7 +125,7 @@ private void TcpClients_OnSend(int length)
private void TcpServer_eventactionReceive(byte[] data)
{
if (OnReceive != null)
{
{
queue.AddRange(data);
OnReceive(queue.Count);
}
Expand Down Expand Up @@ -156,7 +156,7 @@ public byte[] Fetch(int length)
queue.RemoveRange(0, length);
mutex.ReleaseMutex();
return f;
}
}

/// <summary>
/// 断开连接
Expand All @@ -176,7 +176,7 @@ private void TcpServer_eventClose()
OnClose();
}



}
}
6 changes: 3 additions & 3 deletions socket.core/Client/TcpPushClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public bool Connected
{
get
{
if(tcpClients==null)
if (tcpClients == null)
{
return false;
}
Expand Down Expand Up @@ -70,13 +70,13 @@ public TcpPushClient(int receiveBufferSize)
/// </summary>
/// <param name="ip">ip地址或域名</param>
/// <param name="port">端口</param>
public void Connect(string ip,int port)
public void Connect(string ip, int port)
{
while (tcpClients == null)
{
Thread.Sleep(2);
}
tcpClients.Connect(ip,port);
tcpClients.Connect(ip, port);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion socket.core/Common/SendingQueueItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal class SendingQueue
/// <summary>
/// 连接标记
/// </summary>
internal Guid connectId { get; set; }
internal int connectId { get; set; }
/// <summary>
/// 发送的数据
/// </summary>
Expand Down
66 changes: 26 additions & 40 deletions socket.core/Server/TcpPackServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,27 @@ public class TcpPackServer
/// <summary>
/// 连接成功事件
/// </summary>
public event Action<Guid> OnAccept;
public event Action<int> OnAccept;
/// <summary>
/// 接收通知事件
/// </summary>
public event Action<Guid, byte[]> OnReceive;
public event Action<int, byte[]> OnReceive;
/// <summary>
/// 发送通知事件
/// </summary>
public event Action<Guid, int> OnSend;
public event Action<int, int> OnSend;
/// <summary>
/// 断开连接通知事件
/// </summary>
public event Action<Guid> OnClose;
public event Action<int> OnClose;
/// <summary>
/// 接收到的数据缓存
/// </summary>
private Dictionary<Guid, List<byte>> queue;
private Dictionary<int, List<byte>> queue;
/// <summary>
/// 包头标记
/// </summary>
private uint headerFlag;
/// <summary>
/// 连接状态下的客户端列表
/// </summary>
//public ConcurrentBag<ConnectClient> ConnectClient
//{
// get
// {
// if (tcpServer == null)
// {
// return null;
// }
// return tcpServer.connectClient;
// }
//}

/// <summary>
/// 设置基本配置
Expand All @@ -63,16 +49,16 @@ public class TcpPackServer
/// <param name="receiveBufferSize">用于每个套接字I/O操作的缓冲区大小(接收端)</param>
/// <param name="overtime">超时时长,单位秒.(每10秒检查一次),当值为0时,不设置超时</param>
/// <param name="headerFlag">包头标记范围0~1023(0x3FF),当包头标识等于0时,不校验包头</param>
public TcpPackServer(int numConnections, int receiveBufferSize, int overtime,uint headerFlag)
public TcpPackServer(int numConnections, int receiveBufferSize, int overtime, uint headerFlag)
{
if(headerFlag<0|| headerFlag>1023)
if (headerFlag < 0 || headerFlag > 1023)
{
headerFlag = 0;
}
this.headerFlag = headerFlag;
Thread thread = new Thread(new ThreadStart(() =>
{
queue = new Dictionary<Guid, List<byte>>();
queue = new Dictionary<int, List<byte>>();
tcpServer = new TcpServer(numConnections, receiveBufferSize, overtime);
tcpServer.OnAccept += TcpServer_eventactionAccept;
tcpServer.OnReceive += TcpServer_eventactionReceive;
Expand Down Expand Up @@ -100,7 +86,7 @@ public void Start(int port)
/// 连接成功事件方法
/// </summary>
/// <param name="connectId">连接标记</param>
private void TcpServer_eventactionAccept(Guid connectId)
private void TcpServer_eventactionAccept(int connectId)
{
if (OnAccept != null)
OnAccept(connectId);
Expand All @@ -113,7 +99,7 @@ private void TcpServer_eventactionAccept(Guid connectId)
/// <param name="data">数据</param>
/// <param name="offset">偏移位</param>
/// <param name="length">长度</param>
public void Send(Guid connectId, byte[] data, int offset, int length)
public void Send(int connectId, byte[] data, int offset, int length)
{
data = AddHead(data.Skip(offset).Take(length).ToArray());
tcpServer.Send(connectId, data, 0, data.Length);
Expand All @@ -124,7 +110,7 @@ public void Send(Guid connectId, byte[] data, int offset, int length)
/// </summary>
/// <param name="connectId">连接标记</param>
/// <param name="length">长度</param>
private void TcpServer_OnSend(Guid connectId, int length)
private void TcpServer_OnSend(int connectId, int length)
{
if (OnSend != null)
{
Expand All @@ -137,7 +123,7 @@ private void TcpServer_OnSend(Guid connectId, int length)
/// </summary>
/// <param name="connectId">连接标记</param>
/// <param name="data">数据</param>
private void TcpServer_eventactionReceive(Guid connectId, byte[] data,int offset,int length)
private void TcpServer_eventactionReceive(int connectId, byte[] data, int offset, int length)
{
if (OnReceive != null)
{
Expand All @@ -148,8 +134,8 @@ private void TcpServer_eventactionReceive(Guid connectId, byte[] data,int offset
byte[] r = new byte[length];
Buffer.BlockCopy(data, offset, r, 0, length);
queue[connectId].AddRange(r);
byte[] datas=Read(connectId);
if(datas != null&& datas.Length>0)
byte[] datas = Read(connectId);
if (datas != null && datas.Length > 0)
{
OnReceive(connectId, datas);
}
Expand All @@ -159,8 +145,8 @@ private void TcpServer_eventactionReceive(Guid connectId, byte[] data,int offset
/// <summary>
/// 断开连接
/// </summary>
/// <param name="guid">连接ID</param>
public void Close(Guid connectId)
/// <param name="int">连接ID</param>
public void Close(int connectId)
{
tcpServer.Close(connectId);
}
Expand All @@ -169,7 +155,7 @@ public void Close(Guid connectId)
/// 断开连接通知事件方法
/// </summary>
/// <param name="connectId">连接标记</param>
private void TcpServer_eventClose(Guid connectId)
private void TcpServer_eventClose(int connectId)
{
if (queue.ContainsKey(connectId))
{
Expand All @@ -188,34 +174,34 @@ private byte[] AddHead(byte[] data)
{
uint len = (uint)data.Length;
uint header = (headerFlag << 22) | len;
byte[] head=System.BitConverter.GetBytes(header);
return head.Concat(data).ToArray();
byte[] head = System.BitConverter.GetBytes(header);
return head.Concat(data).ToArray();
}

/// <summary>
/// 读取数据
/// </summary>
/// <param name="connectId">连接标记</param>
/// <returns></returns>
private byte[] Read(Guid connectId)
private byte[] Read(int connectId)
{
if (!queue.ContainsKey(connectId))
{
return null;
}
List<byte> data = queue[connectId];
uint header=BitConverter.ToUInt32(data.ToArray(), 0);
if(headerFlag!= (header >> 22))
uint header = BitConverter.ToUInt32(data.ToArray(), 0);
if (headerFlag != (header >> 22))
{
return null;
}
uint len = header & 0x3fffff;
if (len > data.Count-4)
if (len > data.Count - 4)
{
return null;
}
byte[] f = data.Skip(4).Take((int)len).ToArray();
queue[connectId].RemoveRange(0,(int)len+4);
queue[connectId].RemoveRange(0, (int)len + 4);
return f;
}

Expand All @@ -225,7 +211,7 @@ private byte[] Read(Guid connectId)
/// <param name="connectId">连接标识</param>
/// <param name="data">附加数据</param>
/// <returns>true:设置成功,false:设置失败</returns>
public bool SetAttached(Guid connectId, object data)
public bool SetAttached(int connectId, object data)
{
return tcpServer.SetAttached(connectId, data);
}
Expand All @@ -235,7 +221,7 @@ public bool SetAttached(Guid connectId, object data)
/// </summary>
/// <param name="connectId">连接标识</param>
/// <returns>返回附加数据</returns>
public T GetAttached<T>(Guid connectId)
public T GetAttached<T>(int connectId)
{
return tcpServer.GetAttached<T>(connectId);
}
Expand Down
Loading

0 comments on commit 5716c52

Please sign in to comment.