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 DecodeReceive(SocketAsyncEventArgs saea) { try { byte[] buffer = saea.Buffer; int offset = saea.Offset; int count = saea.BytesTransferred; int error = 0; if (!OnDecodeReceive(buffer, offset, count, saea.RemoteEndPoint, out error)) { if (error > 0) { CloseSAEA(saea); return; } NotifyReceiveMessage(buffer, offset, count, saea.RemoteEndPoint); } } catch (Exception ex) { NetDebug.Error("[NetSocket] OnReceive, error: {0}.", ex.Message.ToString()); } }
public void Write(byte[] data, int offset, int count) { if (offset > totalSize || count + offset > totalSize) { NetDebug.Error("[SAEABuffer] write error."); return; } Buffer.BlockCopy(data, 0, buffer, offset, count); }
private SocketAsyncEventArgs AllocSAEA() { if (null == saeaMemory) { NetDebug.Error("[NetSocket] AllocSAEA, the saea memory is null."); return(null); } return(saeaMemory.Alloc()); }
private void EncodeSend(SocketAsyncEventArgs saea, byte[] data, int offset, int count) { try { byte[] buffer = saea.Buffer; int saeaOffset = saea.Offset; int packCount = count; if (OnEncodeSend(buffer, saeaOffset, count, data, offset, out packCount)) { saea.SetBuffer(offset, packCount); } else { saea.SetBuffer(data, offset, count); } } catch (Exception ex) { NetDebug.Error("[NetSocket] OnSend, 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()); } }