//Begins an Asynchronous write to a specific client's network stream. This function handles //writing of header information that defines the size of the object, and then invokes a callback //which will handle serializing and writing the actual object. public void BeginWrite(onWriteEventHandler callback, int clientNumber, object obj) { if (m_listener != null) { try { //I resused the ServerReadInfo class which contains the client's id and a byte array //containing data to be written or read to or from the server. Here we will de- //serialize the object that we want to write. ServerReadInfo writeInfo = new ServerReadInfo(); writeInfo.Id = clientNumber; writeInfo.Data = Serializer.Serializer.SerializeObject(obj); //Store the size of our object in a Byte array. Byte[] array = BitConverter.GetBytes(writeInfo.Data.Length); onWriteUserCallback = callback; //Set writing to true. The caller has to use an if to check if we're writing before //calling this function. m_clients[clientNumber].Writing = true; //Write out the size of the object. Send the actual object to the onWrite header local callback m_clients[clientNumber].Stream.BeginWrite(array, 0, array.Length, onWriteHeaderLocalCallback, writeInfo); } catch (Exception e) { throw e; } } }
public void BeginWrite(onWriteEventHandler callback, object obj) { if (m_client.Connected) { try { ClientReadInfo writeInfo = new ClientReadInfo(); writeInfo.Data = Serializer.Serializer.SerializeObject(obj); Byte[] array = BitConverter.GetBytes(writeInfo.Data.Length); onWriteUserCallback = callback; m_writing = true; m_stream.BeginWrite(array, 0, array.Length, onWriteHeaderLocalCallback, writeInfo); } catch (Exception e) { throw e; } } }