Ejemplo n.º 1
0
        private void OnSend(IAsyncResult ar)
        {
            ar.AsyncWaitHandle.Close();
            SendRecvBuffer buffer = (SendRecvBuffer)ar.AsyncState;

            //已经断开连接
            if (buffer.Socket == null || !buffer.Socket.Connected)
            {
                m_buffer_pools.Despawn(buffer);
                this.CloseClientSocket(buffer.ConnId);
                return;
            }

            try
            {
                buffer.Socket.EndSend(ar);
            }
            catch (Exception e)
            {
                Log.Exception(e);
                this.CloseClientSocket(buffer.ConnId);
            }
            finally
            {
                m_buffer_pools.Despawn(buffer);
            }
        }
Ejemplo n.º 2
0
        public void Send(long conn_idx, byte[] message, int offset, int count)
        {
            if (m_is_close)
            {
                return;
            }

            UserToken token;

            if (!m_user_tokens.TryGetValue(conn_idx, out token) || token.Socket == null || !token.Socket.Connected || message == null)
            {
                return;
            }

            SendRecvBuffer buffer = m_buffer_pools.Spawn();

            buffer.ConnId = conn_idx;
            buffer.Socket = token.Socket;
            System.Array.Copy(message, offset, buffer.Buffer, 0, count);
            try
            {
                buffer.Socket.BeginSend(buffer.Buffer, 0, count, 0, new AsyncCallback(OnSend), buffer);
            }
            catch (Exception e)
            {
                Log.Error("发送失败:" + e.Message);
                this.CloseClientSocket(conn_idx);
                m_buffer_pools.Despawn(buffer);
            }
        }
        /// <summary>
        /// 接收数据
        /// </summary>
        private void OnReceive(IAsyncResult ar)
        {
            SendRecvBuffer buffer = (SendRecvBuffer)ar.AsyncState;

            ar.AsyncWaitHandle.Close();
            try
            {
                if (buffer.Socket == null)
                {
                    return;
                }
                lock (m_sync_lock)
                {
                    int len = buffer.Socket.EndReceive(ar);
                    if (!m_user_tokens.ContainsKey(buffer.ConnId))
                    {
                        return;
                    }
                    if (len > 0)
                    {
                        if (OnMessage != null)
                        {
                            OnMessage(buffer.ConnId, buffer.Buffer, len);
                        }

                        //派发消息的时候,有可能上层逻辑关闭了当前连接,必须再判断一次当前连接是否正常
                        if (m_user_tokens.ContainsKey(buffer.ConnId))
                        {
                            this.BeginReceive(buffer);
                        }
                        else
                        {
                            m_buffer_pools.Despawn(buffer);
                        }
                    }
                    else
                    {
                        Log.Error("OnReceive Recv Error");
                        m_buffer_pools.Despawn(buffer);
                        this.CloseClientSocket(buffer.ConnId);
                    }
                }
            }
            catch (SocketException e)
            {
                if (e.ErrorCode != 10054)
                {
                    Log.Exception(e);
                }
                m_buffer_pools.Despawn(buffer);
                this.CloseClientSocket(buffer.ConnId);
            }
            catch (Exception e)
            {
                Log.Exception(e);
                m_buffer_pools.Despawn(buffer);
                this.CloseClientSocket(buffer.ConnId);
                return;
            }
        }
Ejemplo n.º 4
0
 private void BeginReceive(SendRecvBuffer buffer)
 {
     if (m_socket == null)
     {
         return;
     }
     buffer.Socket.BeginReceive(buffer.Buffer, 0, buffer.Buffer.Length, SocketFlags.None, new AsyncCallback(this.OnReceive), buffer);
 }
 public void Despawn(SendRecvBuffer obj)
 {
     if (obj == null)
     {
         return;
     }
     obj.Reset();
     m_pools.Add(obj);
     System.Threading.Interlocked.Increment(ref m_total_remove_count);
 }
        public SendRecvBuffer Spawn()
        {
            SendRecvBuffer obj = null;

            if (!m_pools.TryTake(out obj))
            {
                System.Threading.Interlocked.Increment(ref m_total_new_count);
                obj = new SendRecvBuffer();
            }
            return(obj);
        }
Ejemplo n.º 7
0
        private void OnAccept(IAsyncResult ar)
        {
            if (m_socket == null || m_is_close)
            {
                return;
            }

            Socket server_socket = (Socket)ar.AsyncState;

            ar.AsyncWaitHandle.Close();
            try
            {
                //初始化一个SOCKET,用于其它客户端的连接
                Socket    client_socket = server_socket.EndAccept(ar);
                long      conn_idx      = System.Threading.Interlocked.Increment(ref m_share_conn_idx);
                UserToken token         = m_user_tokens_pools.Spawn();
                token.ConnId = conn_idx;
                token.Remote = client_socket.RemoteEndPoint;
                token.Socket = client_socket;
                m_user_tokens.TryAdd(conn_idx, token);

                if (OnOpen != null)
                {
                    OnOpen(conn_idx);
                }

                //连接成功,有可能被踢出,需要再次判断是否有效
                if (m_user_tokens.ContainsKey(conn_idx))
                {
                    SendRecvBuffer buffer = m_buffer_pools.Spawn();
                    buffer.ConnId = token.ConnId;
                    buffer.Socket = token.Socket;
                    BeginReceive(buffer);
                }
                //等待新的客户端连接
                server_socket.BeginAccept(new AsyncCallback(OnAccept), server_socket);
            }
            catch (Exception e)
            {
                Log.Exception(e);
                this.Close();
                return;
            }
        }
Ejemplo n.º 8
0
        public void Send(byte[] message, int offset, int count)
        {
            if (m_socket == null || !m_socket.Connected || message == null)
            {
                return;
            }
            SendRecvBuffer buffer = m_buffer_pools.Spawn();

            buffer.Socket = m_socket;
            System.Array.Copy(message, offset, buffer.Buffer, 0, count);
            try
            {
                buffer.Socket.BeginSend(buffer.Buffer, 0, count, 0, new AsyncCallback(OnSend), buffer);
            }
            catch (Exception e)
            {
                Log.Error("发送失败:" + e.Message);
                this.Close();
            }
        }