Ejemplo n.º 1
0
        /// <summary>
        /// 异步连接服务器
        /// </summary>
        /// <param name="hostNameOrAddress">地址(支持ipv6)</param>
        /// <param name="port">端口</param>
        /// <param name="invokeElement">连接完成回调元素</param>
        public void ConnectServer(string hostNameOrAddress, int port, TcpSocketInvokeElement invokeElement)
        {
            EndPoint  remoteEndPoint = null;
            IPAddress ipAddress      = null;

            if (IPAddress.TryParse(hostNameOrAddress, out ipAddress))
            {
                remoteEndPoint = new IPEndPoint(ipAddress, port);
            }
            else
            {
                IPAddress[] addressList = Dns.GetHostAddresses(hostNameOrAddress);
                if (addressList != null)
                {
                    for (int i = 0; i < addressList.Length; i++)
                    {
                        if (addressList[i].AddressFamily == AddressFamily.InterNetwork ||
                            addressList[i].AddressFamily == AddressFamily.InterNetworkV6)
                        {
                            remoteEndPoint = new IPEndPoint(addressList[i], port);
                            break;
                        }
                    }
                }
            }
            ConnectServer(remoteEndPoint, invokeElement);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 异步发送,前一步次发送没完成,不影响后一次发送,强连接使用,比如帧同步战斗数据发送
        /// </summary>
        /// <param name="buffer">发送数据</param>
        /// <param name="invokeElement">发送完成回调元素</param>
        public void BeginSend(byte[] buffer, TcpSocketInvokeElement invokeElement)
        {
            if (UserToken.TcpSocket == null)
            {
                return;
            }
            if (!UserToken.TcpSocket.Connected)
            {
                invokeElement.SocketErrorInvoke(UserToken, SocketError.Shutdown);
                return;
            }
            TcpSocketSendState sendState = new TcpSocketSendState(UserToken.TcpSocket);

            sendState.SendInvokeElement = invokeElement;
            sendState.Buffer            = buffer;
            UserToken.TcpSocket.BeginSend(sendState.Buffer, sendState.CurrentLength, sendState.TargetLength, SocketFlags.None, out sendState.errorCode, new AsyncCallback(EndSend), sendState);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 完成端口的异步发送,前一步次发送没完成前,后一次不能发送,建议发送用这种方法
        /// </summary>
        /// <param name="buffer">发送数据</param>
        /// <param name="invokeElement">发送完成回调元素</param>
        /// <returns></returns>
        public bool SendAsync(byte[] buffer, TcpSocketInvokeElement invokeElement)
        {
            if (UserToken.TcpSocket == null || !UserToken.LockSendState())
            {
                return(false);
            }
            if (!UserToken.TcpSocket.Connected)
            {
                invokeElement.SocketErrorInvoke(UserToken, SocketError.Shutdown);
                return(false);
            }
            UserToken.SendInvokeElement = invokeElement;

            UserToken.SendEventArgs.SetBuffer(buffer, 0, buffer.Length);
            bool willRaiseEvent = UserToken.TcpSocket.SendAsync(UserToken.SendEventArgs);

            if (!willRaiseEvent)
            {
                ProcessSend(UserToken.SendEventArgs);
            }
            return(true);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 异步连接服务器
        /// </summary>
        /// <param name="remoteEndPoint">请求连接的地址</param>
        /// <param name="invokeElement">连接完成回调元素</param>
        public void ConnectServer(EndPoint remoteEndPoint, TcpSocketInvokeElement invokeElement)
        {
            if (remoteEndPoint == null)
            {
                return;
            }

            this.RemoteEndPoint             = remoteEndPoint;
            tcpSocket                       = new Socket(remoteEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            tcpSocket.NoDelay               = true;
            UserToken.TcpSocket             = tcpSocket;
            connectEventArgs.UserToken      = invokeElement;
            connectEventArgs.RemoteEndPoint = remoteEndPoint;

            Info("start connect server : {0}", remoteEndPoint);

            bool willRaiseEvent = tcpSocket.ConnectAsync(connectEventArgs);

            if (!willRaiseEvent)
            {
                ProcessConnect(connectEventArgs);
            }
        }
Ejemplo n.º 5
0
 /// <summary>
 /// 重连
 /// </summary>
 /// <param name="invokeElement">重连完成回调元素</param>
 public void ReconnectSocket(TcpSocketInvokeElement invokeElement)
 {
     UserToken.TcpSocket.Close();
     ConnectServer(RemoteEndPoint, invokeElement);
 }