Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Mars_999

Pages: 1 2 3 [4] 5 6 7
46
Sigh..... this has to be a bug....

I am getting not ready when I try to receive anything when I test the returned status on non-blocking sockets....

This is SFML2.0 and MSVC++ 2010...

switch(socket.receive(packetReceive))
      {
         case sf::Socket::Done:
            std::cout <<  "done\n";
            break;
         case sf::Socket::Disconnected:
            std::cout << "disconnected\n";
            break;
         case sf::Socket::Error:
            std::cout << "error\n";
            break;
         case sf::Socket::NotReady:
            std::cout << "not ready\n";
            break;
      }

47
Anyone?????

48
Anyone wants to, the code posted above is complete and should be drop and compile to test for yourself... after you update the IP to whatever you need....

I am not sure if this is a bug or what...

49
Sigh, this is what I have so far and works, but the issue is I can't get the Client() to allow messages from the Server to be updated each time one is sent... It's like they are blocked....

Code: [Select]
#include "stdafx.h"

const unsigned short PORT = 5000;
const std::string IPADDRESS("127.0.0.1");

bool quit = false;
std::ofstream fout("test.txt");

template<class T>
class SharedType
{
public:
std::vector<T> data;
sf::Mutex mutex;

SharedType(){}
~SharedType(){}
void Push(const T& t)
{
mutex.lock();
data.push_back(t);
mutex.unlock();
}
T Get(void)
{
T t;
mutex.lock();
if(!data.empty())
t = data.back();
mutex.unlock();
return t;
}
void Pull(std::vector<T>& v)
{
mutex.lock();
v = data;
data.clear();
mutex.unlock();
}
void Clear(void)
{
mutex.lock();
data.clear();
mutex.unlock();
}
};
SharedType<std::string> data;

void Server(void)
{
std::list<sf::TcpSocket*> clients;
sf::SocketSelector selector;
sf::TcpListener listener;

listener.listen(PORT);
selector.add(listener);

while(!quit)
{
if(selector.wait(sf::seconds(2.0f)))
{
if(selector.isReady(listener))
{
sf::TcpSocket* client = new sf::TcpSocket;
if(listener.accept(*client) == sf::Socket::Done)
{
clients.push_back(client);
selector.add(*client);
            }
        }
else
{
//receive messages from clients
for(std::list<sf::TcpSocket*>::iterator it = clients.begin(); it != clients.end(); ++it)
{
sf::TcpSocket& client = **it;
if(selector.isReady(client))
{
sf::Packet packet;
std::string clientMsg;
                    if(client.receive(packet) == sf::Socket::Done)
{
packet >> clientMsg;
std::cout << "\nClient said: " << clientMsg << std::endl;
clientMsg.clear();
}
}
}
}
}
//send messages?
for(std::list<sf::TcpSocket*>::iterator it = clients.begin(); it != clients.end(); ++it)
{
sf::TcpSocket& client = **it;
if(selector.isReady(client))
{
sf::Packet packet;
std::string s;
s = data.Get();
data.Clear();
if(!s.empty())
{
packet << s;
if(client.send(packet) == sf::Socket::Done)
{
std::cout << "\nMessage sent to client: " << s << std::endl;
}
}
}
}
}
for(std::list<sf::TcpSocket*>::iterator it = clients.begin(); it != clients.end(); ++it)
{
if(*it)
delete *it;
}
}
void Client(void)
{
std::string s;
sf::Packet packet;
sf::TcpSocket socket;
socket.setBlocking(false);
while(true)
{
if(quit)
break;
socket.connect(IPADDRESS, PORT);
if(socket.getRemotePort())
break;
}
while(!quit)
{
if(socket.receive(packet) == sf::Socket::Done)
{
packet >> s;
std::cout << "\nThe server said: " << s << std::endl;
}
packet.clear();
s.clear();

s = data.Get();
data.Clear();
if(!s.empty())
{
packet << s;
if(socket.send(packet) == sf::Socket::Done)
{
std::cout << "\nMessage sent to server: " << s << std::endl;
}
}
packet.clear();
s.clear();
}
}
void GetInput(void)
{
std::string s;
std::cout << "\nEnter \"exit\" to quit or message to send: ";
std::cin >> s;
data.Push(s);
if(s == "exit")
quit = true;
}
int main(int argc, char* argv[])
{
sf::Thread* thread = 0;

char who;
    std::cout << "Do you want to be a server (s) or a client (c) ? ";
    std::cin  >> who;

    if(who == 's')
thread = new sf::Thread(&Server);
else
thread = new sf::Thread(&Client);

thread->launch();

while(!quit)
{
GetInput();
}
if(thread)
{
thread->wait();
delete thread;
}

std::vector<std::string> temp;
data.Pull(temp);
for(unsigned int i = 0; i < temp.size(); ++i)
fout << temp[i] << std::endl;

return 0;
}

50
So if I set the socket to non blocking I can basically put

if(socket->receive(packet) != sf::Socket::Done)

in the event loop like any other event? e.g. Draw() or Upate() for say game objects?

while(true)//game loop
{
GetPackets();//if(socket->receive(packet) != sf::Socket::Done) is in here...
PhysicsUpdate();
LogicUpdate();
Draw();
TimerUpdate();
}

Hope you are following me, in saying I can just treat the receive() as if it were a polled event in a switch() for sf::Events....

51
Tank or anyone else... I don't see anything in SFML for using a polling system. And I think my issue is all having to do with threading as its nightmare to keep straight! :(

52
Thanks guys for the help....

AFAIK I need to use threads... The whole app stalls when I run the server client stuff and not what I want to happen for obvious reason.

I can connect to each other stuff like that but I need it threaded unless you a simple example where their is no stalling while waiting for client to send or receive ect.. and same for server. When I use the simple example and drop it into my GUI chat interface due to blocking the user can't go back or do anything... This is the issue and then with threading I need to use mutexes correct for the data I am sharing to update...

e.g. a string for the chat log that gets received will need to be locked/unlocked

Thanks!

53
Network / Anyone here have a project made using SFML for networking?
« on: March 31, 2012, 05:53:35 pm »
I am about ready to jump off a cliff... I am having a hell of a time getting a server/client chat log going in my main menu using SFML and threads...

This is TCP, but right now could care less what it uses as long as it works...

So I started looking around and see libs like ClanLIb and such  have networking, but I would rather use SFML. So I though has anyone coded up a nice networking lib for chatting and sending game data using SFML as the only lib needed?

Thanks!

54
Thanks that was it!!!

55
I am not sure why I am getting this... I can't figure it out been at it for a few hours... :(

SFML2.0
 MSVC++ 2010

using a functor

Code: [Select]
//global in .cpp file
sf::Thread threadLoadTextures(NX::ResourceLoadTextures());
sf::Thread threadLoadMusic(NX::ResourceLoadMusic());
sf::Thread threadLoadMaps(NX::ResourceLoadMaps());

void NX::ResourceManager::Init(void)
{
threadLoadTextures.launch();
threadLoadMusic.launch();
threadLoadMaps.launch();
}


56
Network / Re: Tutorial for Chat box in a lobby with SFML out there?
« on: March 26, 2012, 01:28:42 am »
sf::SocketSelector, can I use it to send data also? I am guessing its only for receiving data, but not sure if I can use it for sending data?

Thanks

57
Network / Tutorial for Chat box in a lobby with SFML out there?
« on: March 25, 2012, 04:06:01 pm »
I was wondering if anyone had any code they would be willing to share or know if anyone has coded up a nice little lib that one can just use to do simple chatting in a game lobby or chat window in game...  I would assume it would be multi-threaded?

58
Network / sf::IpAddress::getPublicAddress().toString() crash!
« on: March 18, 2012, 05:21:11 pm »
ARGH!!!!!!!!!!!! Static bindings was the issue... :(  :oops:

BTW why would that cause that one function to crash anyway?

Thanks!

59
Network / sf::IpAddress::getPublicAddress().toString() crash!
« on: March 18, 2012, 05:13:33 pm »
I think I got the call stack


>   msvcr100d.dll!_free_dbg_nolock(void * pUserData, int nBlockUse)  Line 1322 + 0x30 bytes   C++
    msvcr100d.dll!_free_dbg(void * pUserData, int nBlockUse)  Line 1265 + 0xd bytes   C++
    msvcr100d.dll!operator delete(void * pUserData)  Line 54 + 0x10 bytes   C++
    CosmosExpedition.exe!std::allocator<std::_Container_proxy>::deallocate(std::_Container_proxy * _Ptr, unsigned int __formal)  Line 182 + 0x9 bytes   C++
    CosmosExpedition.exe!std::_String_val<char,std::allocator<char> >::~_String_val<char,std::allocator<char> >()  Line 481   C++
    CosmosExpedition.exe!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::~basic_string<char,std::char_traits<char>,std::allocator<char> >()  Line 754 + 0xf bytes   C++
    CosmosExpedition.exe!NX::App::Run()  Line 18   C++
    CosmosExpedition.exe!main(int argc, char * * argv)  Line 14   C++
    CosmosExpedition.exe!__tmainCRTStartup()  Line 555 + 0x19 bytes   C
    CosmosExpedition.exe!mainCRTStartup()  Line 371   C
    kernel32.dll!75be339a()    
    [Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]   
    ntdll.dll!77449ef2()    
    ntdll.dll!77449ec5()

60
Network / sf::IpAddress::getPublicAddress().toString() crash!
« on: March 18, 2012, 05:10:27 pm »
Quote from: "Laurent"
Quote
Code: [Select]
std::cout << sf::IpAddress::getPublicAddress().toString() << std::endl;

Works for me.

Is it getPublicAddress() or toString() which crashes? Why can't you get a call stack when debugging, are you running in release mode or without the debug symbols?

Please give more information, read this carefully:
http://www.sfml-dev.org/forum/viewtopic.php?t=5559


I am running debug, and have no idea why....

But if I do this
sf::IpAddress ip = sf::IpAddress::getPublicAddress();

no crash

this crashes
sf::IpAddress ip = sf::IpAddress::getPublicAddress();
std::cout << ip.toString() << std::endl;

Popup says
This may be due to a corruption of the heap...

or F12 being pressed? Odd no keys are being pressed.

Pages: 1 2 3 [4] 5 6 7