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

Author Topic: How should I use threads in here...  (Read 4914 times)

0 Members and 1 Guest are viewing this topic.

EiTkoCaT

  • Newbie
  • *
  • Posts: 47
    • View Profile
How should I use threads in here...
« on: December 25, 2010, 02:41:44 pm »
Hey, I have this
Code: [Select]
#include <iostream>
#include <sstream>
#include <string>
#include <SFML/Network.hpp>
#include <SFML/System.hpp>
#include "client/dbclient.h"
#include "Mongo.cpp"
#pragma comment( lib, "mongoclient" )

std::vector<sf::SocketTCP> Users;
std::vector<sf::IPAddress> UsersIP;
sf::SocketTCP Server;
sf::SocketTCP Client;
sf::IPAddress ClientAddress;

int connectedClients = 0;
mongo::DBClientConnection c;
bool server_running;

bool LoginSeccedd;
void serverTCP(void* UserData);
sf::Thread ThreadTCP(&serverTCP);

using namespace std;


void startUserrt(sf::SocketTCP Client, sf::IPAddress ClientAddress) {

/*Users.push_back(Client);
UsersIP.push_back(ClientAddress);

connectedClients++;

for (int i=connectedClients; i<connectedClients;i++)
{
cout << ClientAddress[connectedClients] << endl;
}*/

char Message[256];
std::size_t Received;
if (Client.Receive(Message, sizeof(Message), Received) != sf::Socket::Done)
return;
// Show the message

string stringAll, str3, str4;
stringAll = Message;
size_t found;
size_t total = 0;
string current_string[3];

for (int i=0;i<3;i++)
{
cout << &Message[total] << endl;
found = strlen(&Message[total]);
current_string[i] = &Message[total];
total += found + 1;
}

if (current_string[0] == "7L")
{
// Login
cout << "Login requst" << endl;
mongo::BSONObjBuilder query;
query.append( "email" , current_string[1] ).append( "password", current_string[2] );
mongo::BSONObj res = c.findOne( "test.users" , query.obj() );
string userID = res["email"];
cout << userID << endl;
// session_userEmail = userID;
// session_userID = res["_id"];
if (userID == "EOO")
{
char ToSend2[] = "8L 0 not-found\0";
if (Client.Send(ToSend2, sizeof(ToSend2)) != sf::Socket::Done)
return;
cout << ToSend2 << endl;
}
else
{
char ToSend2[] = "8L 1 1\0";
if (Client.Send(ToSend2, sizeof(ToSend2)) != sf::Socket::Done)
return;
cout << ToSend2 << endl;
LoginSeccedd = true;
}
}
else if (current_string[0] == "7R")
{
// Register
cout << "Register requst" << endl;
   mongo::BSONObjBuilder query;
query.append( "email" , current_string[1] ).append( "password", current_string[2] );
mongo::BSONObj res = c.findOne( "test.users" , query.obj() );
string userID = res["email"];
cout << userID << endl;
if (userID == "EOO")
{
char ToSend2[] = "8R 1\0";
// INSERT USER!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!***********************************************************************
if (Client.Send(ToSend2, sizeof(ToSend2)) != sf::Socket::Done)
return;
cout << ToSend2 << endl;
LoginSeccedd = true;
}
else
{
char ToSend2[] = "8R 0 3\0";
if (Client.Send(ToSend2, sizeof(ToSend2)) != sf::Socket::Done)
return;
cout << ToSend2 << endl;
}
}
else
{
cout << "Unknow Request" << endl;
}


if (LoginSeccedd = true)
{
char Message[256];
std::size_t Received;
if (Client.Receive(Message, sizeof(Message), Received) != sf::Socket::Done)
return;
cout << Message << endl;
if (Message == "7MM")
{
// Load Main Menu - send character information
/*string ToSend2 = "8MM";
mongo::BSONObjBuilder query;
query.append( "user_id" , session_userID );

auto_ptr<DBClientCursor> cursor =
c.query("test.characters", query.obj() );
while( cursor->more() ) {
BSONObj p = cursor->next();
cout << p["name"] << endl;
ToSend2 += p["name"];
}
cout << ToSend2 << endl;*/

}
else if (Message == "7CC")
{

}

}
}


void serverTCP(void* UserData) {
while (server_running)
{
// Wait for a connection
   if (Server.Accept(Client, &ClientAddress) != sf::Socket::Done)
return;
cout << "Client connected : " << ClientAddress << std::endl;
startUserrt(Client, ClientAddress);

/*User *current;
current = new User;
current->startUserrt(Client);*/
}
}




void startServer() {
server_running = true;
   if (!Server.Listen(2435))
return;

cout << "Server is listening to port " << 2435 << ", waiting for connections... " << std::endl;
ThreadTCP.Launch();
}

int main()
{
 c.connect("localhost"); //"192.168.58.1");
cout << "connected ok" << endl;
startServer();

    // Wait until the user presses 'enter' key
    std::cout << "Press enter to exit..." << std::endl;
    std::cin.ignore(10000, '\n');
    std::cin.ignore(10000, '\n');

    return EXIT_SUCCESS;

}


This is a server where number of clients logging into it and sending messages. My problem here is not the networking, it's the threads. When a client connect it's not a problem, but it somewhy can't handle more than one client. You see, after a client loged in and the server sends him a message, the client needs to send a message "7MM". But for the check, I'm not sending it. Here's the problem, if client A connects and loged in good-> not sending 7MM and user B also trying to connect, he can't. He have to wait until the function startUserrt of user A will finish itself. So, what do I have to do if I want to make it work for multiple clients? Thanks![/code]

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
How should I use threads in here...
« Reply #1 on: January 07, 2011, 09:28:35 am »
I've never done any network programming but from a quick glance. It looks like you have to a much more asynchronous approach or add some kind of time out against the client.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio