示例#1
0
        /// <summary>
        /// Registers that the given Character has now logged in
        /// </summary>
        /// <param name="characterName">Name of the Character.</param>
        /// <param name="accountName">Account of the Character to login.</param>
        public bool ConnectCharacter(string characterName, string accountName)
        {
            try
            {
                //character cant connect twice
                if (ConnectedCharacters.ContainsKey(characterName))
                {
                    Logger.Log.DebugFormat("[WCF] Character {0} is already connected.", characterName);
                    return(false);
                }
                else
                {
                    //TODO move in own method, cannot do this here because it needs to be called by a client who wants to know if the
                    //character is allowed to connect without doing it actually
                    Logger.Log.DebugFormat("[WCF] Character {0} has connected.", characterName);
                    ConnectedCharacters.Add(characterName, accountName);

                    //inform clients
                    ICommunicationCallback callback = OperationContext.Current.GetCallbackChannel <ICommunicationCallback>();
                    callback.ConnectCharacterCallback(characterName);
                    return(true);
                }
            }
            catch (Exception ex)
            {
                Logger.Log.Error(ex.Message);
                return(false);
            }
        }
        /// <summary>
        /// Registers that the given Account has now logged in
        /// </summary>
        /// <param name="accountName">Name of the Account.</param>
        /// <param name="sessionId">SessionId of the login.</param>
        public bool ConnectAccount(string accountName, int sessionId)
        {
            try
            {
                // Account cant connect twice
                if (ConnectedAccounts.ContainsKey(accountName))
                {
                    Logger.Log.DebugFormat($"[WCF] Account {accountName} is already connected.");
                    return(false);
                }
                else
                {
                    // TODO: move in own method, cannot do this here because it needs to be called by
                    //       a client who wants to know if the Account is allowed to connect without
                    // doing it actually
                    Logger.Log.DebugFormat($"[WCF] Account {accountName} has connected.");
                    ConnectedAccounts.Add(accountName, sessionId);

                    // inform clients
                    ICommunicationCallback callback = OperationContext.Current.GetCallbackChannel <ICommunicationCallback>();
                    callback.ConnectAccountCallback(accountName, sessionId);
                    return(true);
                }
            }
            catch (Exception ex)
            {
                Logger.Log.Error("General Error", ex);
                return(false);
            }
        }
示例#3
0
        /// <summary>
        /// Disconnect character from server.
        /// </summary>
        /// <param name="characterName">Character who wants to disconnect.</param>
        public void DisconnectCharacter(string characterName)
        {
            try
            {
                ConnectedCharacters.Remove(characterName);

                //inform clients
                ICommunicationCallback callback = OperationContext.Current.GetCallbackChannel <ICommunicationCallback>();
                callback.DisconnectCharacterCallback(characterName);

                Logger.Log.DebugFormat("[WCF] Character {0} has been disconnected.", characterName);
            }
            catch (Exception ex)
            {
                Logger.Log.Error(ex.Message);
            }
        }
        /// <summary>
        /// Disconnect Account from server.
        /// </summary>
        /// <param name="accountName">Account who wants to disconnect.</param>
        public void DisconnectAccount(string accountName)
        {
            try
            {
                ConnectedAccounts.Remove(accountName);

                // inform clients
                ICommunicationCallback callback = OperationContext.Current.GetCallbackChannel <ICommunicationCallback>();
                callback.DisconnectAccountCallback(accountName);

                Logger.Log.DebugFormat($"[WCF] Account {accountName} has been disconnected.");
            }
            catch (Exception ex)
            {
                Logger.Log.Error("General Error", ex);
            }
        }
示例#5
0
        /// <summary>
        /// 记录加入的客户端的用户,用于单点登录
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        void Join1(CommunicationUser user)
        {
            try
            {
                bool userAdded = false;

                lock (m_SyncObjForOnlyOneLoginUsers)//线程的同步性,同步访问多个线程的任何变量,利用lock(独占锁),确保数据访问的唯一性。
                {
                    //同一个用户、同一台电脑、同一个进程的重复请求不用处理
                    //if ((m_OnlyOneLoginUsers.Where((pair) => { return pair.Key.ToString() == user.ToString(); })).Count() > 0) return;
                    if (m_OnlyOneLoginUsers.Keys.Contains(user))
                    {
                        return;
                    }

                    //向已经登录的用户发送消息
                    //foreach (CommunicationUser key in m_OnlyOneLoginUsers.Keys)
                    for (int i = m_OnlyOneLoginUsers.Count() - 1; i >= 0; i--)
                    {
                        CommunicationUser key = m_OnlyOneLoginUsers.Keys.ElementAt(i);

                        //只能向以当前用户账号登录系统的用户发送消息
                        if (key.UserID == user.UserID && key.UserName == user.UserName && m_OnlyOneLoginUsers[key] != null)
                        {
                            //给其他用户发送需要退出系统的消息
                            ChatEventArgs e = new ChatEventArgs();
                            e.MessageType = MessageType.UserLeave;
                            e.User        = key;

                            //循环将在线的用户广播信息
                            foreach (CommunicationEventHandler handler in m_OnlyOneLoginUsers[key].GetInvocationList())
                            {
                                //异步方式调用多路广播委托的调用列表中的ChatEventHandler
                                handler.BeginInvoke(this, e, null, null);
                            }

                            //发送完成后移除用户
                            m_OnlyOneLoginUsers.Remove(key);
                            RemoveUserInDGV(key);
                        }
                    }

                    //记录下当前登录的用户
                    //if ((m_OnlyOneLoginUsers.Where((pair) => { return pair.Key.ToString() == user.ToString(); })).Count() == 0)
                    if (!m_OnlyOneLoginUsers.Keys.Contains(user))
                    {
                        m_OnlyOneLoginUsers.Add(user, MyEventHandler);
                        userAdded = true;
                        AddUserInDGV(user);
                    }
                }

                if (userAdded)
                {
                    //获取当前操作客户端实例的通道给IChatCallback接口的实例callback,此通道是一个定义为IChatCallback类型的泛类型,通道的类型是事先服务契约协定好的双工机制。
                    m_Callback = OperationContext.Current.GetCallbackChannel <ICommunicationCallback>();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 public void registerCommunicationCallback <T>(ICommunicationCallback <T> callback) where T : ICommunication
 {
     this.callback = (ICommunicationCallback <ICommunication>)callback;    //<--DOESNT WORK
 }