private void GetData(TcpClient client) { try { NetworkStream stream = client.GetStream(); while (true) { using (MemoryStream ms = new MemoryStream()) { int bytes = 0; byte[] data = new byte[65536]; do { bytes = stream.Read(data, 0, data.Length); ms.Write(data, 0, bytes); }while (stream.DataAvailable); SendData(client, ms.ToArray()); } } } catch { if (isRunning) { if (client.Connected) { clientInfo.Remove(clientInfo.Find(obj => obj.ID == (int)(clients[client] & 0xFFFFFFFC))); SendData(client, CollectionConversion.AddToEndArray(CollectionConversion.AddToEndArray(ConvertClass.ObjectToByteArray(clientInfo), new byte[] { 4 }), new byte[4])); clients.Remove(client); client.Close(); } } } }
public void SendData(byte[] data, byte operation, int uniqueID) { try { data = CollectionConversion.AddToEndArray(CollectionConversion.AddToEndArray(data, new byte[] { operation }), BitConverter.GetBytes(uniqueID)); threads[uniqueID].GetStream().Write(data, 0, data.Length); } catch { } }
private void DataProcess(TcpClient client, byte[] source) { var(userID, operation, data) = CollectionConversion.GetSenderInformation(source); switch (operation) { case 0: var listData = CollectionConversion.AddToEndArray(CollectionConversion.AddToEndArray(ConvertClass.ObjectToByteArray(clientInfo), new byte[] { 1 }), userID); client.GetStream().Write(listData, 0, listData.Length); clientInfo.Add(ConvertClass.ByteArrayToObject(data) as UserModel); break; case 2: UserModel user = clientInfo.Find(obj => obj.ID == BitConverter.ToInt32(userID, 0)); PropertyInfo property = ConvertClass.ByteArrayToObject(data) as PropertyInfo; property.SetValue(user, !(bool)property.GetValue(user)); break; } }