Exemplo n.º 1
0
        public Program()
        {
            Console.WriteLine("Provider Server");

            _connection = TcpConnection.Instance;
            _provider = TcpProvider.Instance;
            _messages = new string[] {
                "The",
                "quick",
                "brown",
                "fox",
                "jumps",
                "over",
                "the",
                "lazy",
                "dog"
            };

            var listenWorker = new Thread(_connection.Listen);
            listenWorker.Name = "Listen Worker";
            listenWorker.Start();

            var notifyWorker = new Thread(Notify);
            notifyWorker.Name = "Notify Worker";
            notifyWorker.Start();
        }
Exemplo n.º 2
0
        public void NotifySubscribers(string txt)
        {
            if (_connection == null)
                _connection = TcpConnection.Instance;

            int i = 0;

            foreach (var subscriber in _subscribers)
            {
                _connection.Post(subscriber.EndPoint, txt);
                Console.WriteLine("notified subscriber {0} about {1}", i, txt);
                i++;
            }
        }
Exemplo n.º 3
0
        private static async void GetMusicList(TcpConnection con, NetMessage message)
        {
            WriteLog(con, "尝试获取音乐列表");
            DataSet set = await API.GetMusicList();

            NetMessage mess;

            if (set == null)
            {
                WriteLog(con, "获取音乐列表失败");
                mess = GetFailMessage();
            }
            else
            {
                WriteLog(con, "获取音乐列表成功");
                MusicInfo[] info = new MusicInfo[set.Tables[0].Rows.Count];
                for (int i = 0; i != set.Tables[0].Rows.Count; ++i)
                {
                    info[i] = new MusicInfo();
                    object[] objs = set.Tables[0].Rows[i].ItemArray;
                    info[i].name   = objs[0] as string;
                    info[i].singer = objs[1] as string;
                    string url = outpath + objs[3] + objs[2];
                    info[i].url         = url;
                    info[i].playedtimes = (int)objs[4];
                    info[i].uname       = objs[5] as string;
                }
                mess = GetGetListSuccessMessage(info);
            }
            try
            {
                await con.Send(mess);
            }
            catch
            {
                con.Close();
                return;
            }
            con.Close();
        }
Exemplo n.º 4
0
        static async void AcceptMessage(TcpConnection con)
        {
            object obj;

            try
            {
                obj = await con.ReceiveOnceAsync();
            }
            catch (Exception e)
            {
                //Console.Out.WriteLine("tag1");
                Console.Out.WriteLine(DateTime.Now.ToString() + " " + e.Message);
                con.Close();
                return;
            }
            if (obj == null)
            {
                Console.Out.WriteLine(DateTime.Now.ToString() + " " + "非指定程序的连接");
                con.Close();
                return;
            }
            NetMessage message = null;

            try
            {
                message = obj as NetMessage;
            }
            catch (Exception e)
            {
                Console.Out.WriteLine(DateTime.Now.ToString() + " " + e.Message);
                con.Close();
                return;
            }
            try
            {
                switch (message.Message)
                {
                case MessageType.SignIn:
                    SignIn(con, message);
                    break;

                case MessageType.SignUp:
                    SignUp(con, message);
                    break;

                case MessageType.GetList:
                    GetMusicList(con, message);
                    break;

                case MessageType.CheckMD5:
                    CheckMD5(con, message);
                    break;

                case MessageType.Upload:
                    AddMusic(con, message);
                    break;

                case MessageType.MusicPlayed:
                    MusicPlayed(con, message);
                    break;

                case MessageType.AddFavorite:
                    AddFavorite(con, message);
                    break;

                case MessageType.GetFavoriteList:
                    GetFavoriteList(con, message);
                    break;

                default:
                    break;
                }
            }
            catch (Exception e)
            {
                Console.Out.WriteLine(DateTime.Now.ToString() + " " + con.ToString() + " " + e.Message);
                con.Close();
                return;
            }
        }
Exemplo n.º 5
0
 static void AcceptConnectionConsole(TcpConnection con)
 {
     Console.Out.WriteLine(DateTime.Now.ToString() + " " + con.ToString() + " 已连接");
 }
Exemplo n.º 6
0
 private static void WriteLog(TcpConnection con, string message, params object[] objs)
 {
     Console.Out.WriteLine(DateTime.Now.ToString() + " " + con.ToString() + " " + string.Format(message, objs));
 }
Exemplo n.º 7
0
 private static void WriteLog(TcpConnection con, string message)
 {
     Console.Out.WriteLine(DateTime.Now.ToString() + " " + con.ToString() + " " + message);
 }