private static SocketAwaitable DoSendAsync(Socket socket, SocketAsyncEventArgs args, ReadOnlyMemory <byte> memory, string name) { // The BufferList getter is much less expensive then the setter. if (args.BufferList != null) { args.BufferList = null; } #if SOCKET_STREAM_BUFFERS args.SetBuffer(MemoryMarshal.AsMemory(memory)); #else var segment = memory.GetArray(); args.SetBuffer(segment.Array, segment.Offset, segment.Count); #endif Helpers.DebugLog(name, $"## {nameof(socket.SendAsync)} {memory.Length}"); if (socket.SendAsync(args)) { Helpers.Incr(Counter.SocketSendAsyncSingleAsync); } else { Helpers.Incr(Counter.SocketSendAsyncSingleSync); SocketAwaitable.OnCompleted(args); } return(GetAwaitable(args)); }
public void Send(ReadOnlyMemory <byte> data) { Socket?socket; if (isAccpet) { socket = _accpet?.AcceptSocket; } else { socket = _accpet?.ConnectSocket; } if (socket != null && socket.Connected) { try { var array = data.GetArray(); socket.Send(array.Array, array.Offset, array.Count, SocketFlags.None); } catch (SocketException er) { if (TheSocketExceptionThrow(er)) { throw er; } } } }
public async ValueTask <int> SendAsync(ReadOnlyMemory <byte> data) { Socket?socket; if (isAccpet) { socket = _accpet?.AcceptSocket; } else { socket = _accpet?.ConnectSocket; } if (socket != null && socket.Connected) { var async = _sendPool.GetObject(); var array = data.GetArray(); async.SetBuffer(array.Array, array.Offset, array.Count); try { var len = await async.SendSync(socket); return(len); } finally { _sendPool.ReleaseObject(async); } } else { return(0); } }
public ValueTask <int> SendAsync(ReadOnlyMemory <byte> data) { Socket?socket; if (isAccpet) { socket = _accpet?.AcceptSocket; } else { socket = _accpet?.ConnectSocket; } #if !NETSTANDARD2_0 if (socket != null && socket.Connected) { return(socket.SendAsync(data, SocketFlags.None)); } else { return(new ValueTask <int>(0)); } #else if (socket != null && socket.Connected) { return(new ValueTask <int>(socket.SendAsync(data.GetArray(), SocketFlags.None))); } else { return(new ValueTask <int>(0)); } #endif }
public async Task SendUnreliable(AsyncPeer peer, ReadOnlyMemory <byte> buffer) { try { await _udpSocket .SendToAsync(buffer.GetArray(), SocketFlags.None, peer.UdpEndpoint) .ConfigureAwait(false); } catch { } }
public async Task Send(int type, ReadOnlyMemory <byte> buffer) { try { await _sendLock.WaitAsync().ConfigureAwait(false); int bytesSent; if (buffer.Length == 0) { bytesSent = 0; while (bytesSent < 8) { bytesSent += await _socket .SendAsync(new ArraySegment <byte>(AsyncTcp.Headers(type), bytesSent, 8 - bytesSent), SocketFlags.None) .ConfigureAwait(false); } // If the packet type is of ErrorType call Shutdown and let the peer gracefully finish processing if (type == AsyncTcp.ErrorType) { ShutDown(); } return; } var header = AsyncTcp.GetHeaderBuffer(); WriteHeader(header, type, buffer.Length); bytesSent = 0; while (bytesSent < 8) { bytesSent += await _socket .SendAsync(new ArraySegment <byte>(header, bytesSent, 8 - bytesSent), SocketFlags.None) .ConfigureAwait(false); } var segment = buffer.GetArray(); bytesSent = 0; while (bytesSent < segment.Count) { bytesSent += await _socket .SendAsync(new ArraySegment <byte>(segment.Array, segment.Offset + bytesSent, segment.Count - bytesSent), SocketFlags.None) .ConfigureAwait(false); } } catch { } finally { _sendLock.Release(); } }
private SocketAwaitable SendAsync(ReadOnlyMemory <byte> memory) { var awaitable = _sendAwaitable; // The BufferList getter is much less expensive then the setter. if (awaitable.EventArgs.BufferList != null) { awaitable.EventArgs.BufferList = null; } #if NETCOREAPP2_1 awaitable.EventArgs.SetBuffer(MemoryMarshal.AsMemory(memory)); #else var segment = memory.GetArray(); awaitable.EventArgs.SetBuffer(segment.Array, segment.Offset, segment.Count); #endif return(_socket.SendAsync(awaitable)); }
private SocketAwaitable SendAsync(ReadOnlyMemory <byte> memory) { // The BufferList getter is much less expensive then the setter. if (_eventArgs.BufferList != null) { _eventArgs.BufferList = null; } #if NETCOREAPP2_1 _eventArgs.SetBuffer(MemoryMarshal.AsMemory(memory)); #else var segment = memory.GetArray(); _eventArgs.SetBuffer(segment.Array, segment.Offset, segment.Count); #endif if (!_socket.SendAsync(_eventArgs)) { _awaitable.Complete(_eventArgs.BytesTransferred, _eventArgs.SocketError); } return(_awaitable); }
private SocketAwaitableEventArgs SendAsync(ReadOnlyMemory <byte> memory) { // The BufferList getter is much less expensive then the setter. if (_awaitableEventArgs.BufferList != null) { _awaitableEventArgs.BufferList = null; } #if NETCOREAPP2_1 _awaitableEventArgs.SetBuffer(MemoryMarshal.AsMemory(memory)); #elif NETSTANDARD2_0 var segment = memory.GetArray(); _awaitableEventArgs.SetBuffer(segment.Array, segment.Offset, segment.Count); #else #error TFMs need to be updated #endif if (!_socket.SendAsync(_awaitableEventArgs)) { _awaitableEventArgs.Complete(); } return(_awaitableEventArgs); }
public static IEnumerable <PotentialArray> FindArrays(ReadOnlyMemory <byte> payload) { using var reader = new GameBinaryReader(payload.GetArray()); for (var i = 0; i < reader.Length; i++) { reader.Position = i; if (!reader.CanRead(sizeof(ushort) * 2)) { break; } var countPos = reader.Position; var count = reader.ReadUInt16(); var offsetPos = reader.Position; var offset = reader.ReadOffset(); if (count == 0 || (count * (sizeof(ushort) * 2 + sizeof(byte)) + sizeof(ushort) * 2) > reader.Length) { continue; } var positions = new HashSet <int> { countPos, countPos + sizeof(byte), offsetPos, offsetPos + sizeof(byte), }; var elems = new List <PotentialArrayElement>(); var good = true; var last = offsetPos; var next = offset; while (next != -PacketHeader.HeaderSize) { if (!(good = next >= 0 && next > last && next <= reader.Length - sizeof(ushort) * 2 - sizeof(byte))) { break; } last = next + sizeof(ushort) * 2; reader.Position = next; var herePos = reader.Position; var here = reader.ReadOffset(); if (!(good = here == herePos && here == next && positions.Add(herePos) && positions.Add(herePos + sizeof(byte)))) { break; } var nextPos = reader.Position; next = reader.ReadOffset(); if (!(good = positions.Add(nextPos) && positions.Add(nextPos + sizeof(byte)))) { break; } elems.Add(new PotentialArrayElement(here, nextPos, next == -PacketHeader.HeaderSize ? 0 : next)); } if (good && elems.Count == count) { yield return(new PotentialArray(countPos, count, offsetPos, offset, elems)); } } }
public static IEnumerable <PotentialString> FindStrings(ReadOnlyMemory <byte> payload, bool whiteSpace, bool control, int minLength) { using var reader = new GameBinaryReader(payload.GetArray()); for (var i = 0; i < reader.Length; i++) { reader.Position = i; if (!reader.CanRead(sizeof(ushort))) { break; } var offsetPos = reader.Position; var offset = reader.ReadOffset(); if (offset < 0 || offset < offsetPos + sizeof(ushort) || offset > reader.Length - sizeof(char)) { continue; } reader.Position = offset; string str; try { str = reader.ReadString(); GameBinaryReader.Encoding.GetString(GameBinaryReader.Encoding.GetBytes(str)); } catch (Exception e) when(IsStringException(e)) { continue; } if (!whiteSpace && string.IsNullOrWhiteSpace(str)) { continue; } if (minLength != 0 && str.Length < minLength) { continue; } var hasBadChars = str.Any(c => { var cat = char.GetUnicodeCategory(c); return(cat == UnicodeCategory.Control || cat == UnicodeCategory.Format || cat == UnicodeCategory.OtherNotAssigned || cat == UnicodeCategory.PrivateUse || cat == UnicodeCategory.Surrogate); }); if (!control && hasBadChars) { continue; } yield return(new PotentialString(offsetPos, offset, str)); } }
internal static Task WriteAsync(this Stream stream, ReadOnlyMemory <byte> buffer) { var arraySegment = buffer.GetArray(); return(stream.WriteAsync(arraySegment.Array, arraySegment.Offset, arraySegment.Count)); }