/// <summary>
            /// 连接到远程主机。
            /// </summary>
            /// <param name="ipAddress">远程主机的 IP 地址。</param>
            /// <param name="port">远程主机的端口号。</param>
            /// <param name="userData">用户自定义数据。</param>
            public void Connect(IPAddress ipAddress, int port, object userData)
            {
                if (m_Socket != null)
                {
                    Close();
                    m_Socket = null;
                }

                switch (ipAddress.AddressFamily)
                {
                case AddressFamily.InterNetwork:
                    m_NetworkType = NetworkType.IPv4;
                    break;

                case AddressFamily.InterNetworkV6:
                    m_NetworkType = NetworkType.IPv6;
                    break;

                default:
                    string errorMessage = Utility.Text.Format("Not supported address family '{0}'.", ipAddress.AddressFamily.ToString());
                    if (NetworkChannelError != null)
                    {
                        NetworkChannelError(this, NetworkErrorCode.AddressFamilyError, errorMessage);
                        return;
                    }

                    throw new GameFrameworkException(errorMessage);
                }

                m_Socket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                if (m_Socket == null)
                {
                    string errorMessage = "Initialize network channel failure.";
                    if (NetworkChannelError != null)
                    {
                        NetworkChannelError(this, NetworkErrorCode.SocketError, errorMessage);
                        return;
                    }

                    throw new GameFrameworkException(errorMessage);
                }

                m_SendState.Reset();
                m_ReceiveState.PrepareForPacketHeader(m_NetworkChannelHelper.PacketHeaderLength);

                try
                {
                    m_Socket.BeginConnect(ipAddress, port, ConnectCallback, new ConnectState(m_Socket, userData));
                }
                catch (Exception exception)
                {
                    if (NetworkChannelError != null)
                    {
                        NetworkChannelError(this, NetworkErrorCode.ConnectError, exception.Message);
                        return;
                    }

                    throw;
                }
            }
            /// <summary>
            /// 连接到远程主机。
            /// </summary>
            /// <param name="ipAddress">远程主机的 IP 地址。</param>
            /// <param name="port">远程主机的端口号。</param>
            /// <param name="userData">用户自定义数据。</param>
            public virtual void Connect(IPAddress ipAddress, int port, object userData)
            {
                if (m_Socket != null)
                {
                    Close();
                    m_Socket = null;
                }

                switch (ipAddress.AddressFamily)
                {
                case System.Net.Sockets.AddressFamily.InterNetwork:
                    m_AddressFamily = AddressFamily.IPv4;
                    break;

                case System.Net.Sockets.AddressFamily.InterNetworkV6:
                    m_AddressFamily = AddressFamily.IPv6;
                    break;

                default:
                    string errorMessage = Utility.Text.Format("Not supported address family '{0}'.", ipAddress.AddressFamily.ToString());
                    if (NetworkChannelError != null)
                    {
                        NetworkChannelError(this, NetworkErrorCode.AddressFamilyError, SocketError.Success, errorMessage);
                        return;
                    }

                    throw new GameFrameworkException(errorMessage);
                }

                m_SendState.Reset();
                m_ReceiveState.PrepareForPacketHeader(m_NetworkChannelHelper.PacketHeaderLength);
            }
            private void SendCallback(IAsyncResult ar)
            {
                Socket socket = (Socket)ar.AsyncState;

                try
                {
                    m_SendState.Offset += socket.EndSend(ar);
                }
                catch (ObjectDisposedException)
                {
                    return;
                }
                catch (Exception exception)
                {
                    m_Active = false;
                    if (NetworkChannelError != null)
                    {
                        NetworkChannelError(this, NetworkErrorCode.SendError, exception.Message);
                        return;
                    }

                    throw;
                }

                if (m_SendState.Offset < m_SendState.Length)
                {
                    Send();
                    return;
                }

                m_SendState.Reset();
            }
예제 #4
0
 /// <summary>
 /// Connect network
 /// </summary>
 public virtual void OnConnected()
 {
     sessionID    = ServerRoot.Instance.GetSessionID();
     ReceiveState = new ReceiveState();
     SendState    = new SendState();
     SendState.Reset();
     ReceiveState.PrepareForPacketHeader(5);
     LastPingTime = NetSvc.GetTimeStamp();
     KDCommon.Log("SessionID:" + sessionID + " Client Connect");
 }