Exemple #1
0
        private void StartRecMsg()
        {
            try
            {
                _udpClient.BeginReceive(asyncCallback =>
                {
                    try
                    {
                        IPEndPoint iPEndPoint = null;
                        byte[] bytes          = _udpClient.EndReceive(asyncCallback, ref iPEndPoint);
                        StartRecMsg();

                        HandleRecMsg?.BeginInvoke(this, iPEndPoint, bytes, null, null);
                    }
                    catch (Exception ex)
                    {
                        HandleException?.BeginInvoke(ex, null, null);
                    }
                }, null);
            }
            catch (Exception ex)
            {
                HandleException?.BeginInvoke(ex, null, null);
            }
        }
Exemple #2
0
        /// <summary>
        /// 开始接受客户端消息
        /// </summary>
        public void StartRecMsg()
        {
            try
            {
                byte[] container = new byte[1024 * 1024 * 4];
                _socket.BeginReceive(container, 0, container.Length, SocketFlags.None, asyncResult =>
                {
                    try
                    {
                        int length = _socket.EndReceive(asyncResult);

                        //马上进行下一轮接受,增加吞吐量
                        if (length > 0 && _isRec && IsSocketConnected())
                        {
                            StartRecMsg();
                        }

                        if (length > 0)
                        {
                            byte[] recBytes = new byte[length];
                            Array.Copy(container, 0, recBytes, 0, length);
                            try
                            {
                                //处理消息
                                HandleRecMsg?.BeginInvoke(recBytes, this, _server, null, null);
                            }
                            catch (Exception ex)
                            {
                                HandleException?.Invoke(ex);
                            }
                        }
                        else
                        {
                            Close();
                        }
                    }
                    catch (Exception ex)
                    {
                        HandleException?.BeginInvoke(ex, null, null);
                        Close();
                    }
                }, null);
            }
            catch (Exception ex)
            {
                HandleException?.BeginInvoke(ex, null, null);
                Close();
            }
        }
        /// <summary>
        /// 读取消息内容
        /// </summary>
        /// <param name="ar"></param>
        public void ReadBodyCallback(IAsyncResult ar)
        {
            try
            {
                String content = String.Empty;
                // Retrieve the state object and the handler socket
                // from the asynchronous state object.
                StateObject state   = (StateObject)ar.AsyncState;
                Socket      handler = state.workSocket;
                // Read data from the client socket.
                int bytesRead = handler.EndReceive(ar);
                if (bytesRead > 0)
                {
                    // There might be more data, so store the data received so far.
                    var bytes = new byte[bytesRead];
                    Array.Copy(state.buffer, 0, state.data, state.dataRecviedLen, bytesRead);

                    state.dataRecviedLen += bytesRead;
                    if (state.dataRecviedLen < state.dataLen)
                    {
                        _socket.BeginReceive(state.buffer, 0, StateObject.BufferSize, SocketFlags.None, new AsyncCallback(ReadBodyCallback), state);
                    }
                    else
                    {
                        try
                        {
                            HandleRecMsg?.BeginInvoke(state.data, this, _server, null, null);
                        }
                        catch (Exception ex)
                        {
                            HandleException?.Invoke(ex);
                        }
                        if (_isRec && IsSocketConnected())
                        {
                            StartRecMsg();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                HandleException?.BeginInvoke(ex, null, null);
                Close();
            }
        }