aboutsummaryrefslogtreecommitdiff
path: root/src/espik_common_handler.c
blob: 479babd86c0d721ddf840ed67dc426323faf9bb9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <Ecore.h>

#include "espik_global.h"
#include "espik.h"
#include "espik_error.h"
#include "espik_net.h"
#include "espik_common_handler.h"

#define INPUT_LENGHT 200
  
static int  count_num_recv = 0;

int  server_data (void* data __UNUSED__, int ev_type __UNUSED__,
         Ecore_Con_Event_Server_Data* ev)
{
  unsigned int  i;
  char*    msg;

  msg = strdup((char*)ev->data);
  //msg = convert_unicode_utf((const wchar_t*)ev->data);
  //msg = convert_utf_unicode((char*)ev->data);

  if (!msg)
    return 0;

  count_num_recv++;
  
  printf ("%s", msg);

  for (i = 0 ; i < strlen(msg) ; i++)
  {
    if (msg[i] == ' ')
      msg[i] = '\0';
  }

  if (count_num_recv >= 1)
  {
    //server_dns = strdup (msg+1);
    send_login ();
  }

  if (! (strcmp (msg, "PING")))
    espik_raw_send ("PONG guybrush.melee");

  memset (msg, 0, strlen(msg));
  free (msg);

  return (0);
}

int  kb_get (void* data __UNUSED__, Ecore_Fd_Handler* fd_handler)
{
  char  kb[INPUT_LENGHT];
  int  count;

  do
  {
    memset (kb, 0, INPUT_LENGHT);
    count = read (ecore_main_fd_handler_fd_get (fd_handler), kb,
            INPUT_LENGHT - 2);

    if (count > 1)
    {
      del_backslash (kb);
      printf ("%s", kb);
    }
  }
  while (count >= INPUT_LENGHT - 2);

  if (count == -1)
  {
    perror ("read");
    espik_shutdown();
    return (0);
  }

  if (count == 0)
  {
    espik_shutdown();
    return (0);
  }

  return (1);
}