示例#1
0
 public Client(TcpClient tc, User user)
 {
     _client = tc;
     Id      = _instanceId++;
     User    = user;
     _reader = new StreamReader(tc.GetStream());
     _writer = new StreamWriter(tc.GetStream());
     _waiter = new Thread(() =>
     {
         while (Thread.CurrentThread.IsAlive)
         {
             if (_client.Connected)
             {
                 try
                 {
                     var d = Data.Deserialize(_reader.ReadLine());
                     DataRecevied?.Invoke(this, d);
                 }
                 catch
                 {
                     Disconnect();
                     break;
                 }
             }
             else
             {
                 Disconnected?.Invoke(this, _client);
                 break;
             }
         }
     });
     _waiter.Start();
 }
示例#2
0
 public Client(string ip, int port)
 {
     _client = new TcpClient();
     _client.Connect(ip, port);
     _reader = new StreamReader(_client.GetStream());
     _writer = new StreamWriter(_client.GetStream());
     _waiter = new Thread(() =>
     {
         while (Thread.CurrentThread.IsAlive)
         {
             if (_client.Connected)
             {
                 try
                 {
                     var d = Data.Deserialize(_reader.ReadLine());
                     if (d.Command == Command.Disconnect)
                     {
                         Disconnect();
                     }
                     else
                     {
                         DataRecevied?.Invoke(this, d);
                     }
                 }
                 catch
                 {
                     Disconnect();
                     break;
                 }
             }
             else
             {
                 break;
             }
         }
         Disconnected?.Invoke(this, null);
     });
     _waiter.Start();
 }