Ejemplo n.º 1
0
        /// <summary>
        /// 连接服务器
        /// </summary>
        /// <param name="ip">IP地址</param>
        /// <param name="port">端口号</param>
        /// <param name="state">异步状态对象</param>
        /// <returns></returns>
        public bool ConnectServer(string ip, int port, StateObject state)
        {
            try
            {
                if (_socketAsyncEventArgs != null)
                {
                    _socketAsyncEventArgs.Completed -= OnSocketAsyncCompleted;
                    _socketAsyncEventArgs.Dispose();
                    _socketAsyncEventArgs = null;
                }
                //连接 服务端监听套接字
                if (_socketAsyncEventArgs == null)
                {
                    _socketAsyncEventArgs = GetISocketAsyncEventArgs();
                }

                _socketAsyncEventArgs.SocketProtocol = SocketProtocol.Tcp;
                _socketAsyncEventArgs.Completed     -= OnSocketAsyncCompleted;
                _socketAsyncEventArgs.Completed     += OnSocketAsyncCompleted;
                _socketAsyncEventArgs.UserToken      = state;

                // 包含 ip和port
                _socketAsyncEventArgs.RemoteIP   = ip;
                _socketAsyncEventArgs.RemotePort = port;

                //实例化 套接字
                _sokClient = GetISocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                _sokClient.SendBufferSize    = 1024 * 10;
                _sokClient.ReceiveBufferSize = 1024 * 100;
                _sokClient.Ttl = 64;
                _socketAsyncEventArgs.CurrentSocket = _sokClient;

                return(_sokClient.ConnectAsync(_socketAsyncEventArgs));
            }
            catch (Exception ex)
            {
                CloseSocket();
                GC.Collect();
                return(false);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 连接完成回调
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void OnSocketAsyncCompleted(object sender, ISocketAsyncEventArgs e)
        {
            bool        stopReceive = false; //是否停止数据接收
            StateObject state       = e.UserToken as StateObject;

            try
            {
                if (e.SocketError == SocketError.Success)
                {
                    if (e.LastOperation == SocketAsyncOperation.Connect)
                    {
                        _connSuccess = true;
                        ConnectSuccess?.Invoke();
                    }
                    else if (e.LastOperation == SocketAsyncOperation.Receive)
                    {
                        if (state != null)
                        {
                            //服务器发来的消息
                            int length = e.BytesTransferred;
                            state.EnqueueBufferQueue(length); //存储接受过来的数据
                        }
                    }
                }
                else if (e.SocketError == SocketError.ConnectionReset)
                {
                    System.Diagnostics.Debug.WriteLine(e.SocketError);
                    stopReceive = true;
                    SocketDisconnected?.Invoke();
                }
                else if (e.SocketError == SocketError.ConnectionAborted || e.SocketError == SocketError.OperationAborted)
                {
                    stopReceive = true;
                }
                else
                {
                    stopReceive = true;
                }
            }
            catch (Exception ex)
            {
                if (ex is ObjectDisposedException)
                {
                    stopReceive = true;
                    return; //停止接收数据
                }
            }
            finally
            {
                try
                {
                    if (!stopReceive)
                    {
                        if (state != null)
                        {
                            //继续接收消息
                            e.SetBuffer(state.Buffer, 0, state.BufferSize);
                            e.CurrentSocket.ReceiveAsync(e);
                        }
                    }
                    else
                    {
                        e.Dispose();
                        e     = null;
                        state = null;
                        CloseSocket();
                    }
                }
                catch (Exception ex)
                {
                    if (!(ex is ObjectDisposedException))
                    {
                        CloseSocket();
                        GC.Collect();
                    }
                }
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// 异步发送数据
 /// </summary>
 /// <param name="e"></param>
 /// <returns></returns>
 public bool SendAsync(ISocketAsyncEventArgs e)
 {
     e.CurrentSocket = _sokClient;
     return(_sokClient.SendAsync(e));
 }
Ejemplo n.º 4
0
 protected virtual void OnCompleted(ISocketAsyncEventArgs e)
 {
     Completed?.Invoke(e.CurrentSocket, e);
 }
Ejemplo n.º 5
0
 /// <summary>
 /// 开始一个异步请求以便从连接的 <see cref="T:System.Net.Sockets.Socket" /> 对象中接收数据。
 /// </summary>
 /// <param name="e"></param>
 /// <returns></returns>
 public abstract bool ReceiveAsync(ISocketAsyncEventArgs e);
Ejemplo n.º 6
0
 /// <summary>
 /// 开始一个对远程主机连接的异步请求。
 /// </summary>
 /// <param name="e"></param>
 /// <returns></returns>
 public abstract bool ConnectAsync(ISocketAsyncEventArgs e);
Ejemplo n.º 7
0
 public abstract bool SendAsync(ISocketAsyncEventArgs e);