/* * void SendCallback(IAsyncResult ar) * { * try * { * Socket client = (Socket)ar.AsyncState; * * // Complete sending the data to the remote device. * int bytesSent = client.EndSend(ar); * * // Signal that all bytes have been sent. * sendEvent.Set(); * } * catch (Exception) * { * } * } */ void ClientThreadHandler() { byte[] buffer = null; while (true) { do { Thread.Sleep(50); } while (client.Available < 6); try { BinaryReader br = new BinaryReader(iostream); int length = AdHocDesktop_BinaryFormatter.DeserializeInt32(br) - 6; // 1 byte + 1 int + 1 byte buffer = null; if (client.Available < length) { MemoryStream ms = new MemoryStream(); while (ms.Length < length) { if (client.Available > 0) { int readLength = ms.Length + client.Available > length ? length - (int)ms.Length : client.Available; buffer = br.ReadBytes(readLength); ms.Write(buffer, 0, buffer.Length); } Thread.Sleep(10); } buffer = ms.ToArray(); ms.Close(); } else { buffer = br.ReadBytes(length); } AdHocDesktop_TcpObject obj = (AdHocDesktop_TcpObject)AdHocDesktop_BinaryFormatter.DeserializeType(buffer); //AdHocDesktop_TcpObject obj = (AdHocDesktop_TcpObject)binary.Deserialize(iostream); OnReceived(obj); } catch (Exception e) { OnError(e.ToString()); } } }
public virtual void Deserialize(byte[] data) { ms = new MemoryStream(data); br = new BinaryReader(ms); //int length = AdHocDesktop_BinaryFormatter.Deserialize(br); object[] objs = AdHocDesktop_BinaryFormatter.Deserialize(br); command = (AdHocDesktop_TcpCommand)objs[0]; src = (string)objs[1]; dest = (string)objs[2]; ArrayList al = new ArrayList(); for (int i = 3; i < objs.Length - 1; i++) // objs[objs.Length-1] = AdHocDesktop_SerializeType.AdHocDesktop_TcpObject { al.Add(objs[i]); } this.data = al.ToArray(); br.Close(); ms.Close(); }
public virtual byte[] Serialize() { ms = new MemoryStream(); bw = new BinaryWriter(ms); AdHocDesktop_BinaryFormatter.SerializeInt32(bw, (int)0); AdHocDesktop_BinaryFormatter.Serialize(bw, (int)command); AdHocDesktop_BinaryFormatter.Serialize(bw, src); AdHocDesktop_BinaryFormatter.Serialize(bw, dest); foreach (object obj in data) { AdHocDesktop_BinaryFormatter.Serialize(bw, obj); } AdHocDesktop_BinaryFormatter.Serialize(bw, AdHocDesktop_SerializeType.AdHocDesktop_TcpObject); ms.Position = 0; AdHocDesktop_BinaryFormatter.SerializeInt32(bw, (int)ms.Length); byte[] result = ms.ToArray(); bw.Close(); ms.Close(); return(result); }