private void ReceiveFunctionCallBack(IAsyncResult ar) { StateObject state = (StateObject)ar.AsyncState; Socket handler = state.workSocket; int bytesRec = handler.EndReceive(ar); WriteLog(LogFormats.BytesWereSuccessfulyReceived(bytesRec, GetSocketName(handler))); List <byte> res = state.buffer.ToList(); if (bytesRec < state.buffer.Length) { res.RemoveRange(bytesRec, state.buffer.Length - bytesRec); } state.receivedBytes.AddRange(res); if (state.allBytesCount > state.receivedBytes.Count) { int length = handler.Available > 1024 ? 1024 : handler.Available; state.buffer = new byte[length]; handler.BeginReceive(state.buffer, 0, length, SocketFlags.Partial, new AsyncCallback(ReceiveFunctionCallBack), state); } else { state.function = GetDeserializedFunction(state.receivedBytes.ToArray()); UpdateResult(state); Send(handler); } }
private void SaveResults() { lock (saveResultsObj) { wtch.Stop(); if (resultsSaved) { return; } string resultsFileName = resultTextBox.Text != "" ? resultTextBox.Text : "results.txt"; WriteLog(LogFormats.SavingResultsInto(resultsFileName)); var result = new MathFunction <BigFloat>(1.0, NumericalAnalysisLibrary.Functions.Generic.MathFunctionType.Sum, proccessedFunctions.ToArray()); StreamWriter results = new StreamWriter(resultsFileName); results.WriteLine("Function to express y = {0}\n" + "Function expression by point x = {1}, power = {2}\n" + "y = {3}", function, pointOfExpression, powerOfExpression, result); Invoke(new Method(() => MessageBox.Show(string.Format("Function to express y = {0}\n" + "Function expression by point x = {1}, power = {2}\n" + "y = {3}", function, pointOfExpression, powerOfExpression, result)))); results.WriteLine("Time spent: {0}", wtch.Elapsed); results.Close(); resultsSaved = true; } }
private void ReceiveCallback(IAsyncResult ar) { StateObject state = (StateObject)ar.AsyncState; Socket handler = state.workSocket; int rec = handler.EndReceive(ar); WriteLog(LogFormats.BytesWereSuccessfulyReceived(rec, GetSocketName(handler))); string[] arr = Encoding.UTF8.GetString(state.buffer).Split(';'); state.functionIndex = Convert.ToInt32(arr[0]); state.allBytesCount = Convert.ToInt32(arr[2]); int length = arr[0].Length + arr[1].Length + arr[2].Length + 3; if (rec > length) { state.receivedBytes = state.buffer.ToList(); state.receivedBytes.RemoveRange(0, length); rec -= length; state.receivedBytes.RemoveRange(rec, state.receivedBytes.Count - rec); } state.buffer = new byte[1024]; handler.BeginReceive(state.buffer, 0, state.BufferSize, SocketFlags.Partial, new AsyncCallback(ReceiveFunctionCallBack), state); }
// callbacks private void SendCallback(IAsyncResult ar) { try { StateObject state = (StateObject)ar.AsyncState; Socket handler = state.workSocket; int bytesSent = handler.EndSend(ar); string socketName = GetSocketName(handler); WriteLog(LogFormats.BytesWereSentSuccessfuly(bytesSent, socketName)); if (Encoding.UTF8.GetString(state.buffer).IndexOf(endMessage) != -1) { WriteLog(LogFormats.ClosingSocket(socketName)); handler.Close(); return; } state.buffer = new byte[100]; state.workSocket = handler; handler.BeginReceive(state.buffer, 0, state.BufferSize, SocketFlags.Partial, new AsyncCallback(ReceiveCallback), state); WriteLog(LogFormats.WaitingToReceiveResultFromWorker(socketName)); } catch (Exception e) { WriteLog(LogFormats.ExceptionWasThrownString(e)); } }
// Starting to listen to the connections private void StartListening() { IPHostEntry ipHostInfo = Dns.GetHostEntry(host); IPAddress ipAddress = ipHostInfo.AddressList[0]; IPEndPoint localEndPoint = new IPEndPoint(ipAddress, socketPort); listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); try { WriteLog(LogFormats.TryingToBindSocketToLocalEndPoint(localEndPoint)); listener.Bind(localEndPoint); WriteLog(LogFormats.bindingWasSuccessfulString); WriteLog(LogFormats.tryingToListenString); listener.Listen(numberOfPossibleListeners); WriteLog(LogFormats.listeningWasStartedString); Thread thrd = new Thread(() => { for (int i = 0; i < numberToAccept; i++) { listener.BeginAccept(new AsyncCallback(AcceptCallback), listener); } }); thrd.Start(); } catch (Exception e) { WriteLog(LogFormats.ExceptionWasThrownString(e)); } }
private void Send(Socket handler) { string msg = GetNewMessage(); StateObject state = new StateObject(); state.workSocket = handler; state.buffer = Encoding.UTF8.GetBytes(msg); WriteLog(LogFormats.AtemptingToSendMessage(msg, GetSocketName(handler))); handler.BeginSend(state.buffer, 0, state.buffer.Length, 0, new AsyncCallback(SendCallback), state); }
private void UpdateResult(StateObject state) { Invoke(new Method(() => { progressIdicator.Value += 1; timeLeftLabel.Text = wtch.Elapsed.ToString(); })); lock (updateResultObj) { proccessedFunctions[state.functionIndex ?? 0] = state.function; if (proccessedFunctions.IndexOf(null) == -1) { SaveResults(); } } WriteLog(LogFormats.ResultWasUpdated(state.functionIndex ?? 0, state.function)); }
private void AcceptCallback(IAsyncResult ar) { Socket listener = (Socket)ar.AsyncState; Socket handler = listener.EndAccept(ar); AddSocket(handler); string socketName = GetSocketName(handler); WriteLog(LogFormats.AcceptedSuccessfully(socketName)); lock (workerListObj) { Invoke(new Method(() => workersList.Items.Add(socketName))); } handler.Send(GetSerializedFunction()); WriteLog(LogFormats.FunctionWasSentToWorker(function, socketName)); Send(handler); }
public void IncreaseLogFormats() { // Create a new log format. this.EnsureChildControls(); LogFormatRow row = this.NewRow(); // Get the log formats from the last row. LogFormatRow lastrow = (LogFormatRow)this.Rows[this.rows_ - 1]; LogFormats formats = new LogFormats(lastrow.LogFormats.ToArray()); // Remove last row's selected log format from all rows. LogFormat format = lastrow.SelectedItem; formats.Remove(format); int lastindex = this.rows_ - 1; for (int i = 0; i < this.rows_; ++i) { // Get the current row. LogFormatRow currrow = (LogFormatRow)this.Rows[i]; // Remove the previously selected format from the row. if (i != lastindex) { currrow.Remove(format); } // Remove the newly selected format from the row. currrow.Remove(formats[0]); } // Initialize the contents of the row. row.AddRange(formats.ToArray()); row.SelectedValue = formats[0].ID; // Increment the row count. ++this.rows_; }
private void CloseListenerSocket() { WriteLog(LogFormats.ListenerSocketWasClosed()); listener.Close(); }