|
I assume this had something to do with IAsyncResult.CompletedSynchronously [^] (possibly derived from Socket.Available [^])?
Interesting because under extreme and consistent load this means that the async loop could cause a StackOverflowException (even with your fixed code); I wonder if there is a way to turn off this 'optimization' without having to resort to:
private void StartAsyncReadLoop()
{
ReadAsyncLoop(null);
}
private void ReadAsyncLoop(IAsyncResult state)
{
try
{
if (state != null)
{
var length = _socket.EndReceive(state);
}
if (state == null || !state.CompletedSynchronously)
_socket.BeginReceive(..., ReadAsyncLoop, null);
else
ThreadPool.QueueUserWorkItem(_ => _socket.BeginReceive(..., ReadAsyncLoop, null));
}
catch (SocketException ex)
{
}
}
I wonder if Socket.UseOnlyOverlappedIO [^] would have an effect on this, considering Overlapped IO is my 'asyncy' than IOCP.
He who asks a question is a fool for five minutes. He who does not ask a question remains a fool forever. [Chinese Proverb]
Jonathan C Dickinson (C# Software Engineer)
|
|
|
|
|
Apparently, one of my co-workers found out that the GetTickCount function from the Windows API isn't reliable as it seemed in the first time he used it. The solution, obviously, was to refactor our code base to use a more reliable function, like timeGetTime.
I almost cried when I saw his commit changing a bunch of *.cpp files, with the following code in the beginning of every file:
#undef GetTickCount
#define GetTickCount timeGetTime
Sad thing is, he doesn't take criticism well.
|
|
|
|
|
Stop being such a bore! Don't criticize, teach! ^^
My programming get away... The Blog...
Taking over the world since 1371!
|
|
|
|
|
Even though it hurts my eyes a little, I must say that it does demonstrate some prettydamn good knowledge about how the C/++ compiler goes about things.
At least it didn't go into stdafx.h.
He who asks a question is a fool for five minutes. He who does not ask a question remains a fool forever. [Chinese Proverb]
Jonathan C Dickinson (C# Software Engineer)
|
|
|
|
|
Oh indeed, put it in a header file; not the code files. Code files should contain very few directives.
|
|
|
|
|
I think you'll find the technical term is refuctoring.
|
|
|
|
|
While editing a much tweaked process that I wrote, I found this T-SQL:
DECLARE @NumDays INT;
SELECT @NumDays =
CASE
WHEN @Client = 'Client1' THEN @DaysBack
WHEN @Client = 'Client2' THEN @DaysBack
ELSE @DaysBack
END;
This is what happens when you go from actual numbers to a variable, without checking the code.
|
|
|
|
|
yeah you clearly missed the
@DaysBack
is this a signature ?
|
|
|
|
|
I've done something similar to this recently. It was like this:
SELECT CASE
WHEN Field1 = 'Something #1' THEN 'Result #1'
WHEN Field1 = 'Something #2' THEN 'N/A'
WHEN Field1 = 'Something #3' THEN '(Empty)'
ELSE 'N/A'
END
And I continued to wonder why even when 'Something #1' was not met, I was seeing 'N/A' in the results. Took me a bit to figure it out.
djj55: Nice but may have a permission problem
Pete O'Hanlon: He has my permission to run it.
|
|
|
|
|
djj55 wrote: What was I thinking
about Client3's perky assets?
|
|
|
|
|
My personal favorite from C#. Someone was testing the value of a boolean variable.
if (someBooleanVariable == true)
return true;
else
return false;
Later they refactored it to this thinking it was an improvement.
return someBooleanVariable == true ? true : false;
|
|
|
|
|
You mean they missed the totally obvious contraction to
return someBooleanVariable ? true : false
They should hang their head in shame!
cheers,
Chris Maunder
The Code Project | Co-founder
Microsoft C++ MVP
|
|
|
|
|
well, it should be just
return someBooleanVariable
|
|
|
|
|
cheers,
Chris Maunder
The Code Project | Co-founder
Microsoft C++ MVP
|
|
|
|
|
It took this long to get this simple return?
|
|
|
|
|
You're not following good form. You should use CONSTANTS for things that like.
return someBooleanVariable ? Boolean.Parse(Boolean.TrueString) : Boolean.Parse(Boolean.FalseString);
|
|
|
|
|
You mean
return someBooleanVariable.ToString() == Boolean.TrueString? Boolean.Parse(Boolean.TrueString) : Boolean.Parse(Boolean.FalseString);
Right?
cheers,
Chris Maunder
The Code Project | Co-founder
Microsoft C++ MVP
|
|
|
|
|
Good call.
|
|
|
|
|
Chris Maunder wrote: You mean they missed the totally obvious contraction to
return someBooleanVariable ? true : false
They should hang their head in shame!
No! Wrong!
return (someBooleanVariable == true)
|
|
|
|
|
Not good enough:
if (((Boolean)someBooleanVariable).equals(true)) {
return someBooleanVariable == true ? someBooleanVariable : false;
} else {
return someBooleanVariable == false ? someBooleanVariable : true;
}
it's called the Irish Method - to be sure, to be sure, to be sure.
Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
|
|
|
|
|
G James wrote: Later they refactored it to this thinking it was an improvement.
return someBooleanVariable == true ? true : false;
That's pretty poor refactoring, it should have been:
interface IBooleanConverter {
bool BooleanToBoolean(bool value);
}
class BooleanConverter : IBooleanConverter {
public BooleanToBoolean(bool value) {
return value == true ? true : false;
}
}
interface IBooleanConverterFactory {
IBooleanConverter CreateBooleanConverterFactory();
}
class ConfigurationBooleanConverterFactory : IBooleanConverter {
public IBooleanConverter CreateBooleanConverter() {
return (IBooleanConverter)
Activator.CreateInstance(ConfigurationManager.AppSettings["Types.BooleanConverter"]);
}
}
static class BooleanConverterFactoryFactory {
public IBooleanConverterFactory CreateBooleanConverterFactory() {
return (IBooleanConverterFactory)
Activator.CreateInstance(ConfigurationManager.AppSettings["Types.BooleanConverterFactory"]);
}
}
return BooleanConverterFactoryFactory.CreateBooleanConverterFactory().CreateBooleanConverter().BooleanToBoolean(someBooleanVariable == true ? true : false);
It is clear that this is more maintainable. There is currently this method in our code-base because some [one] of our developers is so bad that we need to hold his hand through everything:
public static T As<T>(this object obj) {
return obj as T;
}
Yes guys, an extension method that replicates a keyword.
He who asks a question is a fool for five minutes. He who does not ask a question remains a fool forever. [Chinese Proverb]
Jonathan C Dickinson (C# Software Engineer)
|
|
|
|
|
my eyes bleed and my head hurts
it's so dificult to believe i'm seeing so well structured sh*t-code
I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p)
|
|
|
|
|
Sentenryu wrote: it's so dificult to believe i'm seeing so well structured sh*t-code
And a lesson has been learnt about GOF design patterns .
He who asks a question is a fool for five minutes. He who does not ask a question remains a fool forever. [Chinese Proverb]
Jonathan C Dickinson (C# Software Engineer)
|
|
|
|
|
I hope you are not one of those gang of 4 guys
Ranjan.D
|
|
|
|
|
Ah, now I see what people mean by maintainable code - code that will require maintenance, and hence pay the bills for years to come. Genius.
|
|
|
|