/// <summary> /// Handelt de bestand upload af. /// </summary> /// <param name="request"></param> /// <param name="client"></param> private void HandleFileUploadRequest(FileUploadRequest request, Client client) { FileStateObject state = new FileStateObject(client, request.File); Receive(client, state.Buffer, 0, FileStateObject.BufferSize, 0, ReceiveFileCallback, state); //client.Socket.BeginReceive(state.Buffer, 0, FileStateObject.BufferSize, 0, ReceiveFileCallback, state); }
private void ReceiveFileCallback(IAsyncResult ar) { FileStateObject state = (FileStateObject)ar.AsyncState; Socket handler = state.Client.Socket; int read = 0; try { read = handler.EndReceive(ar); } catch (SocketException sx) { } catch (System.IO.IOException) { } if (read > 0) { state.BytesReceived += read; state.Output = state.Output ?? new Func <FileStream>(() => { string id = RandomIDGenerator.GetBase62(5); state.ID = id; string fileName = Settings.Default.FileFolder + id; return(new FileStream(fileName + state.File.FileFormat, FileMode.CreateNew, FileAccess.Write, FileShare.None, Settings.Default.FileWriteBufferSize, false)); // TODO onderzoek })(); state.Output.BeginWrite(state.Buffer, 0, read, WriteFileCallback, state); } else { throw new Exception("Read 0"); // DEBUG } }
/// <summary> /// Callback voor download schrijven naar bestand. /// </summary> /// <param name="ar"></param> private void WriteFileCallback(IAsyncResult ar) { FileStateObject state = (FileStateObject)ar.AsyncState; state.Output.EndWrite(ar); Socket handler = state.Client.Socket; if (state.BytesToReceive == state.BytesReceived) { state.Output.Close(); string json = JsonConvert.SerializeObject(NetworkUtils.ToJson(state.File)); File.WriteAllText(Settings.Default.FileFolder + state.ID + ".json", json); SendPacket(state.Client, new DownloadID(state.ID), SendCallback, FollowUpTask.DISCONNECT); } else { Receive(handler, state.Buffer, 0, FileStateObject.BufferSize, 0, ReceiveFileCallback, state); } /*handler.BeginReceive(state.Buffer, 0, FileStateObject.BufferSize, 0, ReceiveFileCallback, * state);*/ }