• Create Account

### #ActualTheis_Bane

Posted 23 February 2013 - 04:01 PM

Hmmm...okay, maybe I don't exactly understand how a buffer works then.  Here's how I THINK it is working...

Program calls ReceiveMessage, where it then waits for an incoming message.  Once a message is detected, it adds this message into the buffer, then continues through the ReceiveMessage method.  The buffer is stored as a stream of bytes, bytes which represent an encoded string.  The bytes themselves do nothing for me.  What I'm wanting is the string that they represent.

So I decode this message.  Here's where I think I'm confused.  I don't know if this is the whole message or not.  I'm thinking that if the message is longer than my buffer, I only get the first part of the message, hence why I decode this part and stored it in a decoded buffer.  Then, I ReceivedMessage again to get the rest of the message.  Is this not the case?  If part of a message doesn't end up in the buffer the first go around, does it just disappear?  If that's the case, I understand now why I would need to have a bigger buffer, and I wouldn't then need to check for partial messages, reducing the complexity of my method.  Is this correct?

The reason I've included headers and footers goes back to the above paragraph, and that many of the examples I've seen floating around the interwebs includes both.  But...if I understand what you are telling me, I can see how it would only be necessary to have a footer only.

I've adjusted my code to reflect what I think you are saying, and you're right, it's much shorter:

private void ReceiveMessage(IAsyncResult Result)
{
ClientInfo thisClient = (ClientInfo)Result.AsyncState;

try
{

//If we've received any sort of message...
{
//Translate the message
string Contents = Encoding.UTF8.GetString(thisClient.Buffer, 0, AmountReceived);

//Clear buffer and begin receiving again
thisClient.Buffer = new byte[BUFFER_SIZE];  //Changed to 4096

int TermLoc = 0;

while (true)
{
TermLoc = Contents.IndexOf(FOOTER);

if (TermLoc == -1)
{
break;
}

//Get the message

string[] AccountSplit = Split[0].Split('~');
string[] MessageSplit = Split[1].Split('~');

//Message received format = AccountID~CharacterIDect~ect.
RequestModel Request = new RequestModel();
Request.Client = thisClient.WorkSocket;
Request.AccountName = AccountSplit[0];
Request.CharacterID = AccountSplit[1];
Request.Request = MessageSplit;

//Removed received message and look for more
Contents = Contents.Substring(TermLoc + 2, Contents.Length - (TermLoc + 2));
}
}
}
catch
{
}
}


EDIT - Well, I've tested everything, and it seems to be working just perfectly.  I give it a month before I start noticing more odd behavior, lol.

### #3Theis_Bane

Posted 23 February 2013 - 03:20 PM

Hmmm...okay, maybe I don't exactly understand how a buffer works then.  Here's how I THINK it is working...

Program calls ReceiveMessage, where it then waits for an incoming message.  Once a message is detected, it adds this message into the buffer, then continues through the ReceiveMessage method.  The buffer is stored as a stream of bytes, bytes which represent an encoded string.  The bytes themselves do nothing for me.  What I'm wanting is the string that they represent.

So I decode this message.  Here's where I think I'm confused.  I don't know if this is the whole message or not.  I'm thinking that if the message is longer than my buffer, I only get the first part of the message, hence why I decode this part and stored it in a decoded buffer.  Then, I ReceivedMessage again to get the rest of the message.  Is this not the case?  If part of a message doesn't end up in the buffer the first go around, does it just disappear?  If that's the case, I understand now why I would need to have a bigger buffer, and I wouldn't then need to check for partial messages, reducing the complexity of my method.  Is this correct?

The reason I've included headers and footers goes back to the above paragraph, and that many of the examples I've seen floating around the interwebs includes both.  But...if I understand what you are telling me, I can see how it would only be necessary to have a footer only.

I've adjusted my code to reflect what I think you are saying, and you're right, it's much shorter:

private void ReceiveMessage(IAsyncResult Result)
{
ClientInfo thisClient = (ClientInfo)Result.AsyncState;

try
{

//If we've received any sort of message...
{
//Translate the message
string Contents = Encoding.UTF8.GetString(thisClient.Buffer, 0, AmountReceived);

//Clear buffer and begin receiving again
thisClient.Buffer = new byte[BUFFER_SIZE];  //Changed to 4096

int TermLoc = 0;

while (true)
{
TermLoc = Contents.IndexOf(FOOTER);

if (TermLoc == -1)
{
break;
}

//Get the message

string[] AccountSplit = Split[0].Split('~');
string[] MessageSplit = Split[1].Split('~');

//Message received format = AccountID~CharacterIDect~ect.
RequestModel Request = new RequestModel();
Request.Client = thisClient.WorkSocket;
Request.AccountName = AccountSplit[0];
Request.CharacterID = AccountSplit[1];
Request.Request = MessageSplit;

//Removed received message and look for more
Contents = Contents.Substring(TermLoc + 2, Contents.Length - (TermLoc + 2));
}
}
}
catch
{
}
}


### #2Theis_Bane

Posted 23 February 2013 - 02:49 PM

Hmmm...okay, maybe I don't exactly understand how a buffer works then.  Here's how I THINK it is working...

Program calls ReceiveMessage, where it then waits for an incoming message.  Once a message is detected, it adds this message into the buffer, then continues through the ReceiveMessage method.  The buffer is stored as a stream of bytes, bytes which represent an encoded string.  The bytes themselves do nothing for me.  What I'm wanting is the string that they represent.

So I decode this message.  Here's where I think I'm confused.  I don't know if this is the whole message or not.  I'm thinking that if the message is longer than my buffer, I only get the first part of the message, hence why I decode this part and stored it in a decoded buffer.  Then, I ReceivedMessage again to get the rest of the message.  Is this not the case?  If part of a message doesn't end up in the buffer the first go around, does it just disappear?  If that's the case, I understand now why I would need to have a bigger buffer, and I wouldn't then need to check for partial messages, reducing the complexity of my method.  Is this correct?

The reason I've included headers and footers goes back to the above paragraph, and that many of the examples I've seen floating around the interwebs includes both.  But...if I understand what you are telling me, I can see how it would only be necessary to have a footer only.

I've adjusted my code to reflect what I think you are saying, and you're right, it's much shorter:

private void ReceiveMessage(IAsyncResult Result)
{
ClientInfo thisClient = (ClientInfo)Result.AsyncState;

try
{

//If we've received any sort of message...
{
//Translate the message
string Contents = Encoding.UTF8.GetString(thisClient.Buffer, 0, AmountReceived);

//Clear buffer and begin receiving again
thisClient.Buffer = new byte[BUFFER_SIZE];  //Changed to 4096

int TermLoc = 0;

while (true)
{
TermLoc = Contents.IndexOf(FOOTER);

if (TermLoc == -1)
{
break;
}

//Get the message

string[] AccountSplit = Split[0].Split('~');
string[] MessageSplit = Split[1].Split('~');

//Message received format = AccountID~CharacterIDect~ect.
RequestModel Request = new RequestModel();
Request.Client = thisClient.WorkSocket;
Request.AccountName = AccountSplit[0];
Request.CharacterID = AccountSplit[1];
Request.Request = MessageSplit;

//Removed received message and look for more
Contents = Contents.Substring(0, TermLoc + 2);
}
}
}
catch
{
}
}


### #1Theis_Bane

Posted 23 February 2013 - 02:36 PM

Hmmm...okay, maybe I don't exactly understand how a buffer works then.  Here's how I THINK it is working...

Program calls ReceiveMessage, where it then waits for an incoming message.  Once a message is detected, it adds this message into the buffer, then continues through the ReceiveMessage method.  The buffer is stored as a stream of bytes, bytes which represent an encoded string.  The bytes themselves do nothing for me.  What I'm wanting is the string that they represent.

So I decode this message.  Here's where I think I'm confused.  I don't know if this is the whole message or not.  I'm thinking that if the message is longer than my buffer, I only get the first part of the message, hence why I decode this part and stored it in a decoded buffer.  Then, I ReceivedMessage again to get the rest of the message.  Is this not the case?  If part of a message doesn't end up in the buffer the first go around, does it just disappear?  If that's the case, I understand now why I would need to have a bigger buffer, and I wouldn't then need to check for partial messages, reducing the complexity of my method.  Is this correct?

The reason I've included headers and footers goes back to the above paragraph, and that many of the examples I've seen floating around the interwebs includes both.  But...if I understand what you are telling me, I can see how it would only be necessary to have a footer only.

PARTNERS