velocityx
Member
|
Server lost check
i started checking the tutorials,now i'm working on the client/server tutorial and made several modifications to it,now i wanted to make a server lost check,so if the server is gone to eather say connection lost of clear all neighbors,is there such feature to check for the server in the code or i need to do a ping/pong check
|
|
08-03-2012 09:35 AM |
|
laugan
Member
|
RE: Server lost check
you can put in your Update() state on client side:
Code:
if(connection.state() == CONNECT_INVALID)
{
Gui.msgBox("Connection lost", "Connection lost");
}
and on server side to ~Client() (destructor) you can put anything to send to neighbors
|
|
08-03-2012 10:00 AM |
|
velocityx
Member
|
RE: Server lost check
well i used ur code,first i got the msg right away cause i made the server ip to be picked from a textline and needed to use the login button,then i made a bool in the login if(connection.state() == CONNECT_INVALID &&connected() == true) it didn't work, made the bool to be default == false and on login changed to true,but it dosn't work
|
|
08-03-2012 10:20 AM |
|
laugan
Member
|
RE: Server lost check
the code works for me, so i can't say what r u doing wrong unless i see the code.
|
|
08-03-2012 10:25 AM |
|
velocityx
Member
|
RE: Server lost check
removed the bool cause i think i got something wrong there
Code:
/******************************************************************************/
class LoginClass
{
Text t_account; TextLine account;
Text t_pass ; TextLine pass ;
Text t_serverip; TextLine serverip;
Button login;
static void Login(LoginClass &login)
{
SockAddr server_addr;
Str ip =login.serverip();
Str port = ":65535";
Str ipport = ip+port;
server_addr.fromText(ipport);
if(!Server.clientConnectToServer(server_addr))Exit(ip);
ClientSendLogin(Server, login.account(), login.pass());
}
void create()
{
Gui+=t_serverip.create(Vec2(0.3, 0.4), "ServerIp" ); Gui+=serverip.create(Rect_C(0.3, 0.3, 0.5, 0.08), "127.0.0.1");
Gui+=t_account.create(Vec2(0.3, 0.1), "Account" ); Gui+=account.create(Rect_C(0.3, 0, 0.5, 0.08), "Anonymous");
Gui+=login.create(Rect_C(0.3,-0.3, 0.5, 0.08), "Login").func(Login, T);
}
}
LoginClass Login;
/******************************************************************************/
void InitGui()
{
Login.create();
//Move .create();
}
/******************************************************************************/
void DrawMap()
{
Rect_C map_rect(-0.3, 0.1, 0.5, 0.5);
D.clip(map_rect);
REPA(Neighbors)Neighbors.lockedData(i).draw(map_rect);
Plr.draw(map_rect);
D.clip(null);
map_rect.draw(BLACK, false);
D.text(map_rect.up()+Vec2(0,0.04), S+"Neighbors: "+Neighbors.elms());
}
/******************************************************************************/
Code:
/******************************************************************************/
Connection Server;
flt TimeToSendPosition;
/******************************************************************************/
void InitPre()
{
App.name("Client");
App.x=1;
App.flag=APP_WORK_IN_BACKGROUND|APP_NO_PAUSE_ON_WINDOW_MOVE_SIZE;
Paks.add("engine.pak");
D.mode(800, 600).sync(true).scale(2);
}
bool Init()
{
InitGui();
return true;
}
void Shut()
{
Server.del();
}
/******************************************************************************/
bool Update()
{
if(Kb.bp(KB_ESC))return false;
Gui.update();
if(Kb.b(KB_W)) {Plr.move(Vec2(0, 2));}
if(Kb.b(KB_S)) {Plr.move(Vec2(0, -2));}
if(Kb.b(KB_A)) {Plr.move(Vec2(-2, 0));}
if(Kb.b(KB_D)) {Plr.move(Vec2(2, 0));}
// receive data from server
REP(16) // process 16 commands at once
if(!Server.receive(0))break; // no command receive then break
else switch(Server.data.getByte()) // otherwise process the command
{
case CS_DEL_NEIGHBOR:
{
SockAddr addr; ClientReceiveDelNeighbor(Server.data, addr);
Neighbors.removeKey(addr); // remove neighbor
}break;
case CS_ADD_NEIGHBOR:
{
ClientInfo ci; ClientReceiveAddNeighbor(Server.data, ci);
Neighbor *nieghbor=Neighbors(ci.addr); // create neighbor
}break;
case CS_POSITION:
{
SockAddr addr; Vec2 pos; ClientReceivePosition(Server.data, addr, pos);
if(Neighbor *neighbor=Neighbors.find(addr))neighbor.pos=pos; // set neighbor position
}break;
}
Connection connection;
if(connection.state() == CONNECT_INVALID)
{
Gui.msgBox("Connection lost", "Connection lost");
}
// send player position
TimeToSendPosition-=Time.rd();
if(TimeToSendPosition<=0)
{
TimeToSendPosition=0.001;
ClientSendPosition(Server, Plr.pos);
}
return true;
}
/******************************************************************************/
void Draw()
{
D.clear(WHITE);
Gui.draw();
DrawMap();
}
/******************************************************************************/
|
|
08-03-2012 10:45 AM |
|
laugan
Member
|
RE: Server lost check
you create Connection connection in every update, it should be created once and before everything
put Server into update check
(This post was last modified: 08-03-2012 11:16 AM by laugan.)
|
|
08-03-2012 11:15 AM |
|
velocityx
Member
|
RE: Server lost check
10x,now it works,felt dumb after u told me
|
|
08-03-2012 11:30 AM |
|