protected void SendToAsync(EndPoint endPoint, byte[] data, int offset, int count) { if (null == data || data.Length == 0) { NetDebug.Log("[NetSocket] SendToAsync, the data can not be null."); return; } if (data.Length > NetDefine.MAX_TCP_MESSAGE_LENGTH) { NetDebug.Error("[Netsocket] SendToAsync, the data length:{0} is greater message max length:{1}.", data.Length, NetDefine.MAX_TCP_MESSAGE_LENGTH); return; } SocketAsyncEventArgs saea = AllocSAEA(); try { EncodeSend(saea, data, offset, count); saea.RemoteEndPoint = endPoint; bool willRaiseEvent = socket.SendToAsync(saea); if (!willRaiseEvent) { SendToAsyncCallback(saea); } } catch (Exception ex) { NetDebug.Log("[NetSocket] SendToAsync, error:{0}.", ex.Message.ToString()); } }
private void ReceiveFromAsync(SocketAsyncEventArgs saea) { try { bool willRaiseEvent = socket.ReceiveFromAsync(saea); if (!willRaiseEvent) { ReceiveFromAsyncCallback(saea); } } catch (Exception ex) { NetDebug.Log("[NetSocket] ReceiveFromAsync, error:{0}.", ex.Message.ToString()); } }
private void ReceiveFromAsyncCallback(SocketAsyncEventArgs saea) { try { int read = saea.BytesTransferred; if (read > 0 && saea.SocketError == SocketError.Success) { DecodeReceive(saea); ReceiveFromAsync(saea); } else { CloseSAEA(saea); } } catch (Exception ex) { NetDebug.Log("[NetSocket] ReceiveAsyncCallback, error:{0}.", ex.Message.ToString()); } }
protected virtual void SendAsync(byte[] data) { if (!ready4Send) { NetDebug.Log("[NetSocket] SendAsync, not ready for send."); return; } if (null == data || data.Length == 0) { NetDebug.Log("[NetSocket] SendAsync, the data can not be null."); return; } if (data.Length > NetDefine.MAX_MESSAGE_LENGTH) { NetDebug.Error("[Netsocket] SendAsync, the data length:{0} is greater message max length:{1}.", data.Length, NetDefine.MAX_MESSAGE_LENGTH); return; } SocketAsyncEventArgs saea = AllocSendSAEA(); try { EncodeSend(saea, data); bool willRaiseEvent = socket.SendAsync(saea); if (!willRaiseEvent) { SendAsyncCallback(saea); } } catch (Exception ex) { NetDebug.Log("[NetSocket] SendAsync, error:{0}.", ex.Message.ToString()); } }
protected void RecycleReceiveSAEA(SocketAsyncEventArgs saea) { receiveSAEAPool.Recycle(saea); NetDebug.Log("Recycle Receive SAEA count:{0} --{1}", sendSAEAPool.count, GetType().ToString()); }