public void OnUserLogin(object sender, MessageHeader msghdr)
        {
            TcpMessageChannel channel = (TcpMessageChannel)sender;
            MsgLogin          login   = (MsgLogin)msghdr;

            Log.Debug($"OnUserRegister() user:{login.username} password:{login.password}");
            MsgText text = new MsgText($"user {login.username} login OK");

            text.ackid = msghdr.id;
            channel.SendMessage(text);
            MsgFriendList friendlist = new MsgFriendList();

            for (int i = 0; i < 10; i++)
            {
                MsgFriend friend = new MsgFriend()
                {
                    friendid   = i,
                    friendname = $"friend{i}",
                    groupname  = $"group{i}",
                    join_time  = DateTime.Now
                };
                friendlist.Friends.Add(friend);
            }
            channel.SendMessage(friendlist);
        }
Esempio n. 2
0
        internal void ProcessAccept(SocketAsyncEventArgs AcceptEventArgs)
        {
            Log.Debug("Connect From " + AcceptEventArgs.AcceptSocket.RemoteEndPoint.ToString());
            if (AcceptEventArgs.SocketError != SocketError.Success)
            {
                if (AcceptEventArgs.AcceptSocket != null)
                {
                    AcceptEventArgs.AcceptSocket.Close();
                }
                AcceptLoop(AcceptEventArgs);
                return;
            }
            Socket ClientSocket = AcceptEventArgs.AcceptSocket;
            //TooManyClients(ClientSocket);
            TcpMessageChannel channel = _ChannelManager.Pop();

            if (channel == null)
            {
                TooManyClients(ClientSocket);
            }
            else
            {
                channel.Start(ClientSocket);
            }
            AcceptLoop(AcceptEventArgs);
        }
 public TcpMessageClient(ClientSettings Settings)
 {
     _Settings            = Settings;
     ServerMessageHandler = null;
     _ClientSocket        = null;
     _TcpMessageChannel   = new TcpMessageChannel(Settings);
     _TcpMessageChannel.OnMessageReceived += OnServerMessage;
 }
Esempio n. 4
0
        void OnChannelClosed(object sender, Exception ex)
        {
            TcpMessageChannel channel = (TcpMessageChannel)sender;

            if (channel.ChannelId != -1)
            {
                _ChannelManager.Push(sender as TcpMessageChannel);
            }
        }
 public void Push(TcpMessageChannel item)
 {
     if (item == null)
     {
         throw new ArgumentException("Items added to a AsyncSocketUserToken cannot be null");
     }
     lock (_syncRoot)
     {
         _UsedChannelList.Remove(item);
         _FreeChannelPool.Push(item);
         Log.Debug($"Push TcpMessageChannel {item.ChannelId}");
     }
 }
 public TcpMessageChannel Pop()
 {
     lock (_syncRoot)
     {
         if (_FreeChannelPool.Count <= 0)
         {
             return(null);
         }
         TcpMessageChannel channel = _FreeChannelPool.Pop();
         _UsedChannelList.Add(channel);
         Log.Debug($"Pop TcpMessageChannel {channel.ChannelId}");
         return(channel);
     }
 }
Esempio n. 7
0
        public TcpMessageServer(ServerSettings ServerSettings)
        {
            _ServerSettings      = ServerSettings;
            _ListenSocket        = null;
            _IsListening         = false;
            ClientMessageHandler = null;

            _ChannelManager = new TcpMessageChannelManager(ServerSettings.MaxChannels);

            TcpMessageChannel channel;

            for (int i = 0; i < ServerSettings.MaxChannels; i++)
            {
                channel = new TcpMessageChannel(i, ServerSettings);
                channel.OnMessageReceived += OnClientMessage;
                channel.OnChannelClosed   += OnChannelClosed;
                _ChannelManager.Push(channel);
            }

            _ErrorMessageChannel = new TcpMessageChannel(-1, ServerSettings);
        }