Esempio n. 1
0
        private void InternalSend(byte[] buffer)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }

            if (!this.socket.Connected)
            {
                return;
            }

            SocketAsyncEventArgs seae = asyncpool.Pop();

            System.Threading.Interlocked.Increment(ref concurrentsends);

            try
            {
                var token = new usertoken(this, buffer);
                seae.UserToken = token;
                seae.SetBuffer(buffer, 0, buffer.Length);
                System.Threading.Thread.MemoryBarrier();
                bool willRaiseEvent = this.socket.SendAsync(seae);
                if (!willRaiseEvent)
                {
                    ProcessSend(seae);
                }
            }
            catch (Exception excp)
            {
                Disconnect();
                asyncpool.Push(seae);
            }
        }
Esempio n. 2
0
        public bool Connect(EndPoint ipEnd)
        {
            var asyncargs = asyncpool.Pop();

            var token = new usertoken(this, null);

            asyncargs.RemoteEndPoint = ipEnd;
            asyncargs.UserToken      = token;
            try
            {
                bool willRaiseEvent = socket.ConnectAsync(asyncargs);
                if (!willRaiseEvent)
                {
                    ProcessConnect(asyncargs);
                }
                return(true);
            }
            catch
            {
                asyncargs.UserToken = null;
                asyncpool.Push(asyncargs);
            }
            return(false);
        }