private static void OnSend(object sender, SocketAsyncEventArgs e) { AsyncSocket2 asyncSocket = e.UserToken as AsyncSocket2; if (asyncSocket != null) { asyncSocket.OnSend(e); } }
public static void Init() { AsyncSocket2.Init(); }
private bool _BeginSend() { int num; ArraySegment <byte> nums; bool flag; if (this.sendSize < 8192) { if (this.sendingPacket.Count > 0) { num = System.Math.Min(this.sendingPacket.Count, 8192 - this.sendSize); Buffer.BlockCopy(this.sendingPacket.Array, this.sendingPacket.Offset, this.sendArg.Buffer, this.sendArg.Offset + this.sendSize, num); this.sendSize += num; if (num < this.sendingPacket.Count) { goto Label1; } this.sendingPacket = new ArraySegment <byte>(); } while (this.sendSize < 8192 && this.sendQueue.TryDequeue(out nums)) { if (nums.Count <= 0) { continue; } int count = 0; if (nums.Count + this.sendSize > 8192) { count = 8192 - this.sendSize; this.sendingPacket = new ArraySegment <byte>(nums.Array, nums.Offset + count, nums.Count - count); } else { count = nums.Count; } Buffer.BlockCopy(nums.Array, nums.Offset, this.sendArg.Buffer, this.sendArg.Offset + this.sendSize, count); this.sendSize += count; } this.sendArg.SetBuffer(this.sendArg.Offset, this.sendSize); } if (this.sendSize == 0) { return(false); } Label0: try { if (this.socket.Connected) { AsyncSocket2 totalSentCount = this; totalSentCount.TotalSentCount = totalSentCount.TotalSentCount + 1; if (this.socket.SendAsync(this.sendArg)) { return(true); } else { this.OnSend(this.sendArg); flag = false; } } else { ConcurrentQueue <ArraySegment <byte> > arraySegments = new ConcurrentQueue <ArraySegment <byte> >(); Interlocked.Exchange <ConcurrentQueue <ArraySegment <byte> > >(ref this.sendQueue, arraySegments); flag = false; } } catch (ObjectDisposedException) { ConcurrentQueue <ArraySegment <byte> > arraySegments1 = new ConcurrentQueue <ArraySegment <byte> >(); Interlocked.Exchange <ConcurrentQueue <ArraySegment <byte> > >(ref this.sendQueue, arraySegments1); flag = false; } catch (Exception exception) { this.OnException(exception); flag = false; } return(flag); Label1: this.sendingPacket = new ArraySegment <byte>(this.sendingPacket.Array, this.sendingPacket.Offset + num, this.sendingPacket.Count - num); this.sendArg.SetBuffer(this.sendArg.Offset, this.sendSize); goto Label0; }