Recv call fails

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
egizz983
Posts: 311
Joined: August 27th, 2016, 12:30 pm

Recv call fails

Post by egizz983 » February 3rd, 2017, 3:38 pm

Hello , have issue with Winsock Recv() calls its always fails and give me 183 error code , i tried to run debugger and when debugging it not failing and runs as it should (debug mode only , release mode debugging still fails). I was thinking about race condition because my main game loop runs Client::UserMsgHandler(); and this functions tries to recv packets type aswell . so i lock this function when i am recv() inside Player::Player(); but that didt change anything , its just fixed bug that i might be facing it in future, that UserMsgHandler would recv packets that should be recved into Player::Player();.

So its failing into Player::Player(); i am sending a request of user data there and then enter a loop where try to recv(); packet type and then whole data of user , before that i lock Client::UserMshHandler from receiving any packet

Code: Select all

client.LockMessageHandler(true);
	while (!LoadComplete)
	{
		PacketType ptype;

		if (!client.RecvPacketType(ptype)) // always fails unless there is break point and debug mode 
		{
			Log("Failed to recv packet");
			//std::exit(1);
		}
this is my repo : https://github.com/egizz983/TT
related files are :
Game.cpp // Main game loop where Client::UserMsgHandler called
Client.cpp // UserMshHandler function
GameStateLoadGame.cpp // loading game state where i initialize std::asycn to constructr next game state "GameStateGamePlay" where Player constructor is called
Player.cpp // Player class constructor where is loop to recv packets

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Recv call fails

Post by albinopapa » February 3rd, 2017, 6:03 pm

WSA Error code: 183
"An operation was attempted on something that is not a socket."

Code: Select all

			const std::string errMsgString = []
			{
				LPWSTR errMsg = nullptr;
				DWORD lastError = WSAGetLastError();
				DWORD flags = 
					FORMAT_MESSAGE_ALLOCATE_BUFFER | 
					FORMAT_MESSAGE_FROM_SYSTEM | 
					FORMAT_MESSAGE_IGNORE_INSERTS;
				DWORD langId = MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT );

				FormatMessage( flags, nullptr, lastError, langId, ( LPWSTR )&errMsg, 0, nullptr );

				std::wstring wErrMsg( errMsg );
				HeapFree( GetProcessHeap(), 0, errMsg );

				return std::string( wErrMsg.begin(), wErrMsg.end() );
			}( );
You can use this lambda for WSA errors if you'd like, it looks up the numerical error code and returns the related error string. FormatMessage() is a Windows API function, that does that.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

egizz983
Posts: 311
Joined: August 27th, 2016, 12:30 pm

Re: Recv call fails

Post by egizz983 » February 3rd, 2017, 6:16 pm

wtf is it mean not a socket ? client calls recv with his sockets , and why does this works in debugger ?

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Recv call fails

Post by albinopapa » February 3rd, 2017, 6:27 pm

It looks like you are calling recv before connecting.

I modified some of the code and this is the log.txt file

Code: Select all

2017/2/3 12:25 Client::RecvAllBytes: Return Check : -1
2017/2/3 12:25 Client::RecvAllBytes: Error code :An operation was attempted on something that is not a socket.


2017/2/3 12:25 GameStateStartUp::Update: Startup:Resources Loaded
2017/2/3 12:25 Client::RecvAllBytes: Return Check : -1
2017/2/3 12:25 Client::RecvAllBytes: Error code :An operation was attempted on something that is not a socket.


2017/2/3 12:26 Client::Connect: Failed to connect
2017/2/3 12:26 GameStateStartUp::Update: Startup:Failed to connect
As you can see, even before getting to Client::Connect, the Client::RecvAllBytes function is being called, so the socket hasn't been initialized, at least that's my thinking
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Recv call fails

Post by albinopapa » February 3rd, 2017, 6:28 pm

Also, it's NOT connecting for me in debug mode.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

egizz983
Posts: 311
Joined: August 27th, 2016, 12:30 pm

Re: Recv call fails

Post by egizz983 » February 3rd, 2017, 6:28 pm

I connect into GameStateStartUp and stay connected all the time , i call recv and send into other states and its connected not sure why would he disconnect inside that thread , so far player are disconnected only if server goes down or when u leave (with Client destructor) , all other cases client is connected and should stay connected

egizz983
Posts: 311
Joined: August 27th, 2016, 12:30 pm

Re: Recv call fails

Post by egizz983 » February 3rd, 2017, 6:35 pm

Btw Alibinopapa i dont think u will be able to connect at all because u need server for that . U can find server at TTS repo , but u would need some shit like mysql to run it . i could make stand alone client but that no point cuz u wont be able to see anything then

egizz983
Posts: 311
Joined: August 27th, 2016, 12:30 pm

Re: Recv call fails

Post by egizz983 » February 3rd, 2017, 7:03 pm

ok its seems problem was

Code: Select all

virtual bool PacketHandler(PacketType type) { return false; }
so when GameStateLoadGame is not override this functions its returns false and disconnect client . that was first problem second problem is

Code: Select all

if (!client.RecvPacketType(ptype))
		{
			Log("Failed to recv packet");
			std::exit(1);
		}
// so if client did catch any data he returns -1 as an error and if he does my client just close so i remove this if conditions so even if he wont be able to recv to keep trying because Client receiving is blocked so only he will recv packet type he needs , maybe later will add like 15 sec time to recv or something

egizz983
Posts: 311
Joined: August 27th, 2016, 12:30 pm

Re: Recv call fails

Post by egizz983 » February 3rd, 2017, 7:05 pm

Btw Alibinopapa its seems u was looking in wrong place :D i probably explain it bad , because for me that lambda function returns "Cannot create a file when that file already exists." from 183 code

Post Reply