コード例 #1
0
ファイル: TSMessage.cs プロジェクト: northspb/p2pproxy
 public static TSMessage Construct(string msg)
 {
     var tsmsg = new TSMessage {Text = msg};
     var amsg = msg.Split(" ".ToArray(), StringSplitOptions.RemoveEmptyEntries);
     tsmsg.Type = amsg[0];
     if (tsmsg.Type == TorrentStream.MSG_HELLOTS)
     {
         if (amsg.Count() == 2)
         {
             tsmsg.Parameters = new Dictionary<string, string>();
             var aprms = amsg[1].Split("=".ToCharArray(), StringSplitOptions.None);
             tsmsg.Parameters.Add(aprms[0], aprms[1]);
         }
     }
     else if (tsmsg.Type == TorrentStream.MSG_START)
     {
         tsmsg.Parameters = new Dictionary<string, string> { { "url", amsg[1].Split("=".ToCharArray())[1].Replace("%3A", ":") } };
         for (int i = 2; i <amsg.Length; i++)
         {
             var prm = amsg[i].Split("=".ToCharArray());
             tsmsg.Parameters.Add(prm[0], prm[1]);
         }
         
     } else if (tsmsg.Type == TorrentStream.MSG_LOADRESP)
     {
         tsmsg.Parameters = new Dictionary<string, string>();
         DataContractJsonSerializer serial = new DataContractJsonSerializer(typeof(LoadRespData));
         var data = msg.Split(" ".ToArray(), 3);
         MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(data[2]));
         LoadRespData resp = (LoadRespData)serial.ReadObject(ms);
         tsmsg.Parameters.Add("key", data[1]);
         tsmsg.InnerData = resp;
     }
     return tsmsg;
 }
コード例 #2
0
        public static TSMessage Construct(string msg)
        {
            var tsmsg = new TSMessage {
                Text = msg
            };
            var amsg = msg.Split(" ".ToArray(), StringSplitOptions.RemoveEmptyEntries);

            tsmsg.Type = amsg[0];
            if (tsmsg.Type == TorrentStream.MSG_HELLOTS)
            {
                if (amsg.Count() == 2)
                {
                    tsmsg.Parameters = new Dictionary <string, string>();
                    var aprms = amsg[1].Split("=".ToCharArray(), StringSplitOptions.None);
                    tsmsg.Parameters.Add(aprms[0], aprms[1]);
                }
            }
            else if (tsmsg.Type == TorrentStream.MSG_START)
            {
                tsmsg.Parameters = new Dictionary <string, string> {
                    { "url", amsg[1].Split("=".ToCharArray())[1].Replace("%3A", ":") }
                };
                for (int i = 2; i < amsg.Length; i++)
                {
                    var prm = amsg[i].Split("=".ToCharArray());
                    tsmsg.Parameters.Add(prm[0], prm[1]);
                }
            }
            else if (tsmsg.Type == TorrentStream.MSG_LOADRESP)
            {
                tsmsg.Parameters = new Dictionary <string, string>();
                DataContractJsonSerializer serial = new DataContractJsonSerializer(typeof(LoadRespData));
                var          data = msg.Split(" ".ToArray(), 3);
                MemoryStream ms   = new MemoryStream(Encoding.UTF8.GetBytes(data[2]));
                LoadRespData resp = (LoadRespData)serial.ReadObject(ms);
                tsmsg.Parameters.Add("key", data[1]);
                tsmsg.InnerData = resp;
            }
            return(tsmsg);
        }
コード例 #3
0
        private void Receive()
        {
            var    buffer = new byte[Int16.MaxValue];
            string msg    = String.Empty;

            while (_tssock.Connected)
            {
                try
                {
                    if (Owner == null)
                    {
                        _tssock.Close();
                        return;
                    }

                    int bytes = 0;
                    try
                    {
                        bytes = _tssock.Receive(buffer);
                    }
                    catch (ObjectDisposedException e)
                    {
                        break;
                    }
                    if (bytes > 0)
                    {
                        msg = msg + Encoding.UTF8.GetString(buffer, 0, bytes);
                        if (!msg.Contains("\r\n") || msg.LastIndexOf("\r\n", StringComparison.Ordinal) < msg.Length - 2)
                        {
                            continue;
                        }
                        var msglist = msg.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                        foreach (var s in msglist)
                        {
                            var tmsg = TSMessage.Construct(s);
                            if (tmsg.Type == MSG_START)
                            {
                                Started      = true;
                                AceBroadcast = tmsg.Parameters["url"];
                            }
                            else if (s == "STATUS 0")
                            {
                                Started = false;
                            }
                            else if (tmsg.Type == MSG_EVENT)
                            {
                                if (tmsg.Text.Contains("getuserdata"))
                                {
                                    SendMessage("USERDATA [{\"gender\": 1}, {\"age\": 1}]");
                                    Disconnect();
                                }
                            }
                            lock (_messagePool)
                            {
                                _messagePool.Add(tmsg);
                            }
                            RaisReceived(tmsg);
                        }
                        msg = String.Empty;
                    }
                    else
                    {
                        break;
                    }
                    Thread.Sleep(20);
                }
                catch (SocketException)
                {
                    lock (_tssock)
                    {
                        _tssock.Close();
                    }
                    break;
                }
            }
            if (_tssock.Connected)
            {
                Disconnect();
            }
        }