예제 #1
0
void Server::get_data(SOCKET socket) {
	int recv_result;
	if (m_state != STARTED) {
		printf("Trying to call get_data() while the server isn't ready. STATE was %d\n", m_state);
		return;
	}

	memset(m_data_buffer, 0, m_buflen);
	recv_result = recv(socket, m_data_buffer, m_buflen, NULL);
	if (recv_result == SOCKET_ERROR) {
		printf("Unable to connect to server: %ld\n", WSAGetLastError());
		client_disconnected(socket);
	} else if (recv_result == 0) {
		client_disconnected(socket);
	} else {
#ifdef DEBUG
		printf("From user %u: %s\n", socket, m_data_buffer);
#endif
		if (m_game == NULL) {
			printf("Warning, the server is running, but no game is attached to it.\n");
		} else {
			m_game->data_received(socket, m_data_buffer, recv_result);
		}
	}
}
예제 #2
0
static gboolean
disconnect_idle_cb (BroadwayClient *client)
{
  client->disconnect_idle = 0;
  client_disconnected (client);
  return G_SOURCE_REMOVE;
}
예제 #3
0
cSessionView::cSessionView(cClient *pClient, QWidget *parent) :
    QDockWidget(parent),
    ui(new Ui::cSessionView)
{
  ui->setupUi(this);

  m_pClient = pClient;

  connect(m_pClient, SIGNAL(disconnected()), this, SLOT(client_disconnected()));

  connect(&m_pClient->channel_session(), SIGNAL(session_status(QString, bool,QString)),
          this, SLOT(session_status(QString, bool,QString)));
  connect(&m_pClient->channel_session(), SIGNAL(session_userlist(QString, cUsersInfoList)),
          this, SLOT(session_userlist(QString, cUsersInfoList)));
  connect(&m_pClient->channel_session(), SIGNAL(pad_list(QString, cPadsInfoList)),
          this, SLOT(pad_list(QString, cPadsInfoList)));
  connect(&m_pClient->channel_session(), SIGNAL(chatroom_list(QString,QVector<cChatInfo>&)),
          this, SLOT(chatroom_list(QString, QVector<cChatInfo>&)));
  //connect(&m_pClient->channel_session(), SIGNAL(chatroom_list(QString, cChatsInfoList)),
  //        this, SLOT(chatroom_list(QString, cChatsInfoList)));
  //connect(ui->usersInfoView, SIGNAL(),
  //        &m_pClient->channel_session(), SLOT());

  connect(ui->padsInfoView, SIGNAL(send_join_pad(QString)),
          &m_pClient->channel_session(), SLOT(send_join_pad(QString)));
  connect(ui->chatroomsInfoView, SIGNAL(send_join_chatroom(QString)),
          &m_pClient->channel_session(), SLOT(send_join_chatroom(QString)));
}
예제 #4
0
cPadView::cPadView(cClient *pClient, QString id, QWidget *pParent) :
    QWidget(pParent),
    ui(new Ui::cPadView)
{
  ui->setupUi(this);
  //ui->padTextEdit->setDocument(pDoc);
  m_remoteCursor = new QTextCursor(ui->padTextEdit->document());
  m_padId = id;
  m_pClient = pClient;
  m_padInit = false;

  this->setupPad();

  connect(m_pClient, SIGNAL(disconnected()), this, SLOT(client_disconnected()));

  connect(&m_pClient->channel_session(), SIGNAL(session_status(QString,bool,QString)),
          this, SLOT(session_status(QString,bool,QString)));
  connect(&m_pClient->channel_session(), SIGNAL(pad_status(QString,bool,QString)),
          this, SLOT(pad_status(QString,bool,QString)));

  connect(&m_pClient->channel_session(), SIGNAL(pad_document(QString, QString&)),
          this, SLOT(pad_document(QString, QString&)));
  connect(ui->padTextEdit->document(),SIGNAL(contentsChange(int,int,int)),this,SLOT(pad_tracker(int,int,int)));
  connect(&m_pClient->channel_session(),SIGNAL(pad_changes(QString,QString,int,int,int,QString)),this,SLOT(pad_changes(QString,QString,int,int,int,QString)));
  m_pClient->channel_session().send_get_pad_document(m_padId);
}
예제 #5
0
void Server::send_message(SOCKET id, const char* msg) {
#ifdef DEBUG
	printf("Sending to %d: %s\n", id, msg);
#endif
	int send_result = send(id, msg, strlen(msg), 0);
	if (send_result == SOCKET_ERROR) {
		int error = WSAGetLastError();
		if (error != WSAEWOULDBLOCK) {
			fprintf(stderr, "Error sending to client %d: %lu\n", id, error);
			client_disconnected(id);
		}
	}
}
예제 #6
0
파일: main.c 프로젝트: LADI/ladish
static DBusHandlerResult lashd_client_disconnect_handler(DBusConnection * connection, DBusMessage * message, void * data)
{
  /* If the message isn't a signal the object path handler may use it */
  if (dbus_message_get_type(message) != DBUS_MESSAGE_TYPE_SIGNAL)
  {
    return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  }

  const char *member, *name, *old_name;
  struct lash_client *client;

  if (!(member = dbus_message_get_member(message)))
  {
    log_error("Received JACK signal with NULL member");
    return DBUS_HANDLER_RESULT_HANDLED;
  }

  if (strcmp(member, "NameOwnerChanged") != 0)
  {
    return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  }

  dbus_error_init(&err);

  if (!dbus_message_get_args(message, &err,
                             DBUS_TYPE_STRING, &name,
                             DBUS_TYPE_STRING, &old_name,
                             DBUS_TYPE_INVALID))
  {
    log_error("Cannot get message arguments: %s",
               err.message);
    dbus_error_free(&err);
    return DBUS_HANDLER_RESULT_HANDLED;
  }

  client = server_find_client_by_dbus_name(old_name);
  if (client)
  {
    client_disconnected(client);
    return DBUS_HANDLER_RESULT_HANDLED;
  }

  else
  {
    return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  }
}
예제 #7
0
static void
client_fill_cb (GObject *source_object,
		GAsyncResult *result,
		gpointer user_data)
{
  BroadwayClient *client = user_data;
  gssize res;

  res = g_buffered_input_stream_fill_finish (client->in, result, NULL);
  
  if (res > 0)
    {
      guint32 size;
      gsize count, remaining;
      guint8 *buffer;

      buffer = (guint8 *)g_buffered_input_stream_peek_buffer (client->in, &count);

      remaining = count;
      while (remaining >= sizeof (guint32))
	{
	  memcpy (&size, buffer, sizeof (guint32));

	  if (size <= remaining)
	    {
	      client_handle_request (client, (BroadwayRequest *)buffer);

	      remaining -= size;
	      buffer += size;
	    }
	}
      
      /* This is guaranteed not to block */
      g_input_stream_skip (G_INPUT_STREAM (client->in), count - remaining, NULL, NULL);
      
      g_buffered_input_stream_fill_async (client->in,
					  4*1024,
					  0,
					  NULL,
					  client_fill_cb, client);
    }
  else
    {
      client_disconnected (client);
    }
}
예제 #8
0
void Server::broadcast(const char* msg) {
#ifdef DEBUG
	printf("Broadcasting: %s\n", msg);
#endif
	std::set<SOCKET>::iterator it = m_clients.begin();
	while (it != m_clients.end()) {
		int send_result;
		SOCKET client = *(it++);
		send_result = send(client, msg, strlen(msg), 0);
		if (send_result == SOCKET_ERROR) {
			int error = WSAGetLastError();
			if (error != WSAEWOULDBLOCK) {
				fprintf(stderr, "Error sending to client %d: %lu\n", client, error);
				client_disconnected(client);
			}
		}
	}
}
예제 #9
0
cChatsView::cChatsView(cClient *pClient, QWidget *parent) :
    QGroupBox(parent),
    ui(new Ui::cChatsView)
{
  ui->setupUi(this);

  m_pClient = pClient;  
  connect(m_pClient,SIGNAL(disconnected()),this,SLOT(client_disconnected()));
  connect(&m_pClient->channel_session(), SIGNAL(session_status(QString,bool,QString)),
          this, SLOT(session_status(QString,bool,QString)));
  connect(ui->chatRoomsTableWidget, SIGNAL(cellDoubleClicked(int,int)),
          this, SLOT(table_helper(int,int)));

  connect(&m_pClient->channel_session(), SIGNAL(chatroom_list(QString,QVector<cChatInfo>&)),
          this, SLOT(chatroom_list(QString,QVector<cChatInfo>&)));
    connect(&m_pClient->channel_session(), SIGNAL(chatroom_status(QString, bool, QString)),
          this, SLOT(chatroom_status(QString,bool,QString)));
}
예제 #10
0
파일: server.cpp 프로젝트: vos/qcc
void Server::incomingConnection(int socketDescriptor)
{
    QTcpSocket *socket = new QTcpSocket(this);
    socket->setSocketDescriptor(socketDescriptor);
    addPendingConnection(socket);

#ifdef DEBUG
    qDebug("\nServer::incomingConnection(%i): %s:%i", socketDescriptor,
           qPrintable(socket->peerAddress().toString()), socket->peerPort());
#endif

    connect(socket, SIGNAL(disconnected()), SLOT(client_disconnected()));
    connect(socket, SIGNAL(readyRead()), SLOT(client_readyRead()));

    m_clients.insert(socket, new Client);
    QccPacket(QccPacket::ConnectionAccepted).send(socket); // QccPacket::ConnectionRefused

#ifdef DEBUG
    qDebug("ConnectionAccepted");
#endif
}
예제 #11
0
static void client_old_parse_messages(GebrCommProtocolSocket * socket, struct client *client)
{
	GList *link;
	struct gebr_comm_message *message;

	while ((link = g_list_last(client->socket->protocol->messages)) != NULL) {
		message = (struct gebr_comm_message *)link->data;

		/* check login */
		if (message->hash == gebr_comm_protocol_defs.ini_def.code_hash) {
			GList *arguments;

			GString *accounts_list = g_string_new("");
			GString *queue_list = g_string_new("");
			GString *display_port = g_string_new("");

			/* organize message data */
			if ((arguments = gebr_comm_protocol_socket_oldmsg_split(message->argument, 2)) == NULL)
				goto err;

			GString *version = g_list_nth_data(arguments, 0);
			GString *hostname = g_list_nth_data(arguments, 1);

			g_debug("Current protocol version is: %s", gebr_comm_protocol_get_version());
			g_debug("Received protocol version:   %s", version->str);

			if (strcmp(version->str, gebr_comm_protocol_get_version())) {
				gebr_comm_protocol_socket_oldmsg_send(client->socket, TRUE,
								      gebr_comm_protocol_defs.err_def, 2,
								      "protocol",
								      gebr_comm_protocol_get_version());
				goto err;
			}

			/* set client info */
			client->socket->protocol->logged = TRUE;
			g_string_assign(client->socket->protocol->hostname, hostname->str);
			client->server_location = GEBR_COMM_SERVER_LOCATION_REMOTE;

			const gchar *server_type;
			if (gebrd_get_server_type() == GEBR_COMM_SERVER_TYPE_MOAB) {
				/* Get info from the MOAB cluster */
				server_moab_read_credentials(accounts_list, queue_list);
				server_type = "moab";
			} else
				server_type = "regular";

			GString *mpi_flavors = g_string_new("");
			g_list_foreach(gebrd->mpi_flavors, (GFunc) get_mpi_flavors, mpi_flavors);
			if (mpi_flavors->len > 0)
				mpi_flavors = g_string_erase(mpi_flavors, mpi_flavors->len-1, 1);

			g_debug("------------on daemon, Sending %s", mpi_flavors->str)  ;
			const gchar *model_name;
			const gchar *cpu_clock;
			const gchar *total_memory;
			GebrdCpuInfo *cpuinfo = gebrd_cpu_info_new();
			GebrdMemInfo *meminfo = gebrd_mem_info_new();
			model_name = gebrd_cpu_info_get (cpuinfo, 0, "model name");
			cpu_clock = gebrd_cpu_info_get (cpuinfo, 0, "cpu MHz");
			total_memory = gebrd_mem_info_get (meminfo, "MemTotal");
			gchar *ncores = g_strdup_printf("%d", gebrd_cpu_info_n_procs(cpuinfo));

			gebr_comm_protocol_socket_oldmsg_send(client->socket, FALSE,
							      gebr_comm_protocol_defs.ret_def, 11,
							      gebrd->hostname,
							      server_type,
							      accounts_list->str,
							      model_name,
							      total_memory,
							      gebrd->fs_lock->str,
							      ncores,
							      cpu_clock,
							      gebrd_user_get_daemon_id(gebrd->user),
							      g_get_home_dir(),
							      mpi_flavors->str);
			gebrd_cpu_info_free(cpuinfo);
			gebrd_mem_info_free(meminfo);
			gebr_comm_protocol_socket_oldmsg_split_free(arguments);
			g_string_free(accounts_list, TRUE);
			g_string_free(queue_list, TRUE);
			g_string_free(display_port, TRUE);
			g_free(ncores);
		}
		else if (message->hash == gebr_comm_protocol_defs.gid_def.code_hash) {
			GList *arguments;

			/* organize message data */
			if ((arguments = gebr_comm_protocol_socket_oldmsg_split(message->argument, 3)) == NULL)
				goto err;

			GString *gid = g_list_nth_data(arguments, 0);
			GString *cookie = g_list_nth_data(arguments, 1);
			GString *disp_str = g_list_nth_data(arguments, 2);

			guint display;
			if (g_strcmp0(disp_str->str, "0") == 0)
				display = gebr_comm_get_available_port(6010);
			else
				display = atoi(disp_str->str);
			display = MAX(display-6000, 0);

			g_hash_table_insert(gebrd->display_ports, g_strdup(gid->str), GUINT_TO_POINTER(display));

			g_debug("Received gid %s with cookie %s", gid->str, cookie->str);

			if (cookie->len && gebrd_get_server_type() != GEBR_COMM_SERVER_TYPE_MOAB) {
				gebrd_message(GEBR_LOG_DEBUG, "Authorizing with system(xauth)");
				gchar *tmp = g_strdup_printf(":%d", display);
				if (!run_xauth_command("add", tmp, cookie->str))
					display = 0;
				g_free(tmp);
			}

			gchar *display_str = g_strdup_printf("%d", display ? display + 6000 : 0);
			gebrd_message(GEBR_LOG_INFO, "Sending port %s to client %s!", display_str, gid->str);
			gebr_comm_protocol_socket_oldmsg_send(client->socket, FALSE,
							      gebr_comm_protocol_defs.ret_def, 2,
							      gid->str, display_str);
			g_free(display_str);

			gebr_comm_protocol_socket_oldmsg_split_free(arguments);
		}
		else if (client->socket->protocol->logged == FALSE) {
			/* not logged! */
			goto err;
		} else if (message->hash == gebr_comm_protocol_defs.qut_def.code_hash) {
			client_free(client);
			gebr_comm_message_free(message);
			return;
		} else if (message->hash == gebr_comm_protocol_defs.lst_def.code_hash) {
			job_list(client);
		} else if (message->hash == gebr_comm_protocol_defs.run_def.code_hash) {
			GList *arguments;
			GebrdJob *job;

			/* organize message data */
			if ((arguments = gebr_comm_protocol_socket_oldmsg_split(message->argument, 9)) == NULL)
				goto err;

			GString *gid = g_list_nth_data(arguments, 0);
			GString *id = g_list_nth_data(arguments, 1);
			GString *frac = g_list_nth_data(arguments, 2);
			GString *numproc = g_list_nth_data(arguments, 3);
			GString *nice = g_list_nth_data(arguments, 4);
			GString *flow_xml = g_list_nth_data(arguments, 5);
			GString *paths = g_list_nth_data(arguments, 6);

			/* Moab & MPI settings */
			GString *account = g_list_nth_data(arguments, 7);
			GString *servers_mpi = g_list_nth_data(arguments, 8);

			g_debug("SERVERS MPI %s", servers_mpi->str);

			/* try to run and send return */
			job_new(&job, client, gid, id, frac, numproc, nice, flow_xml, account, paths, servers_mpi);

#ifdef DEBUG
			gchar *env_delay = getenv("GEBRD_RUN_DELAY_SEC");
			if (env_delay != NULL)
				sleep(atoi(env_delay));
#endif

			if (gebrd_get_server_type() == GEBR_COMM_SERVER_TYPE_REGULAR) {
				/* send job message (job is created -promoted from waiting server response- at the client) */
				g_debug("RUN_DEF: run task with rid %s", id->str);
				job_send_clients_job_notify(job);
				job_run_flow(job);
			} else {
				/* ask moab to run */
				job_run_flow(job);
				/* send job message (job is created -promoted from waiting server response- at the client)
				 * at moab we must run the process before sending the JOB message, because at
				 * job_run_flow moab_jid is acquired.
				 */
				job_send_clients_job_notify(job);
			}

			/* frees */
			gebr_comm_protocol_socket_oldmsg_split_free(arguments);
		} else if (message->hash == gebr_comm_protocol_defs.rnq_def.code_hash) {
		} else if (message->hash == gebr_comm_protocol_defs.flw_def.code_hash) {
		} else if (message->hash == gebr_comm_protocol_defs.clr_def.code_hash) {
			GList *arguments;
			GString *rid;
			GebrdJob *job;

			/* organize message data */
			if ((arguments = gebr_comm_protocol_socket_oldmsg_split(message->argument, 1)) == NULL)
				goto err;
			rid = g_list_nth_data(arguments, 0);

			job = job_find(rid);
			if (job != NULL)
				job_clear(job);

			/* frees */
			gebr_comm_protocol_socket_oldmsg_split_free(arguments);
		} else if (message->hash == gebr_comm_protocol_defs.end_def.code_hash) {
			GList *arguments;
			GString *rid;
			GebrdJob *job;

			/* organize message data */
			if ((arguments = gebr_comm_protocol_socket_oldmsg_split(message->argument, 1)) == NULL)
				goto err;
			rid = g_list_nth_data(arguments, 0);

			/* try to run and send return */
			job = job_find(rid);
			if (job != NULL) {
				job_end(job);
			}

			/* frees */
			gebr_comm_protocol_socket_oldmsg_split_free(arguments);
		} else if (message->hash == gebr_comm_protocol_defs.kil_def.code_hash) {
			GList *arguments;
			GString *rid;
			GebrdJob *job;

			/* organize message data */
			if ((arguments = gebr_comm_protocol_socket_oldmsg_split(message->argument, 1)) == NULL)
				goto err;
			rid = g_list_nth_data(arguments, 0);

			/* try to run and send return */
			job = job_find(rid);
			if (job != NULL) {
				job_kill(job);
			}

			/* frees */
			gebr_comm_protocol_socket_oldmsg_split_free(arguments);
		} else if (message->hash == gebr_comm_protocol_defs.path_def.code_hash) {
			GList *arguments;

			if ((arguments = gebr_comm_protocol_socket_oldmsg_split(message->argument, 3)) == NULL)
				goto err;

			GString *new_path = g_list_nth_data(arguments, 0);
			GString *old_path = g_list_nth_data(arguments, 1);
			GString *opt = g_list_nth_data(arguments, 2);

			g_debug("new_path:%s, old_path:%s, opt:%s", new_path->str, old_path->str, opt->str);

			GList *new_paths= parse_comma_separated_string(new_path->str);

			gint option = gebr_comm_protocol_path_str_to_enum(opt->str);
			gint status_id = -1;
			gboolean flag_exists = FALSE;
			gboolean flag_error = FALSE;


			switch (option) {
			case GEBR_COMM_PROTOCOL_PATH_CREATE:
				for (GList *j = new_paths; j; j = j->next) {
					GString *path = j->data;
					if (g_file_test(path->str, G_FILE_TEST_IS_DIR)){
						flag_exists = TRUE;
					}
					else if (*(path->str) && g_mkdir_with_parents(path->str, 0700)) {
						flag_error = TRUE;
						break;
					}
					if (g_access(path->str, W_OK)==-1){
						flag_error = TRUE;
						break;
					}
				}

				if (flag_error)
					status_id = GEBR_COMM_PROTOCOL_STATUS_PATH_ERROR;
				else if (flag_exists)
					status_id = GEBR_COMM_PROTOCOL_STATUS_PATH_EXISTS;
				else
					status_id = GEBR_COMM_PROTOCOL_STATUS_PATH_OK;
				break;
			case GEBR_COMM_PROTOCOL_PATH_RENAME:
				g_debug("Renaming %s to %s", old_path->str, new_path->str);
				gboolean dir_exist = g_file_test(new_path->str, G_FILE_TEST_IS_DIR);
				gboolean create_dir = g_rename(old_path->str, new_path->str) == 0;

				if (!dir_exist && create_dir)
					status_id = GEBR_COMM_PROTOCOL_STATUS_PATH_OK;
				else if (dir_exist && !create_dir)
					status_id = GEBR_COMM_PROTOCOL_STATUS_PATH_EXISTS;
				else if (!dir_exist && !create_dir)
					status_id = GEBR_COMM_PROTOCOL_STATUS_PATH_ERROR;
				else
					status_id = -1;
				break;
			case GEBR_COMM_PROTOCOL_PATH_DELETE:
				for (GList *j = new_paths; j; j = j->next) {
					GString *path = j->data;
					if (g_rmdir(path->str))
						status_id = GEBR_COMM_PROTOCOL_STATUS_PATH_ERROR;
					else
						status_id = GEBR_COMM_PROTOCOL_STATUS_PATH_OK;
				}
				break;
			default:
				g_warn_if_reached();
				break;
			}
			g_debug("on %s, new_path:'%s', old_path:'%s', status_id: '%d'", __func__, new_path->str, old_path->str, status_id);

			/* frees */
			gebr_comm_protocol_socket_oldmsg_split_free(arguments);

			gebr_comm_protocol_socket_oldmsg_send(socket, FALSE,
							      gebr_comm_protocol_defs.ret_def, 2,
							      gebrd->hostname,
							      g_strdup_printf("%d", status_id));
		} else if (message->hash == gebr_comm_protocol_defs.harakiri_def.code_hash) {
			/* Maestro wants me killed */
			g_debug("Harakiri");
			gebrd_quit();
		} else {
			/* unknown message! */
			goto err;
		}
		gebr_comm_message_free(message);
		client->socket->protocol->messages = g_list_delete_link(client->socket->protocol->messages, link);
	}

	return;

err:	gebr_comm_message_free(message);
	client->socket->protocol->messages = g_list_delete_link(client->socket->protocol->messages, link);
	client_disconnected(socket, client);
}
예제 #12
0
/******************************************************************************\
 Called from within the network namespace when a network event arrives for the
 server from one of the clients (including the server's client).
\******************************************************************************/
static void server_callback(int client, n_event_t event)
{
        g_client_msg_t token;

        /* Special client events */
        if (event == N_EV_CONNECTED) {
                N_broadcast_except(N_HOST_CLIENT_ID, "11",
                                   G_SM_CONNECTED, client);
                init_client(client);
                return;
        } else if (event == N_EV_DISCONNECTED) {
                client_disconnected(client);
                return;
        }

        /* Handle messages */
        if (event != N_EV_MESSAGE)
                return;
        token = (g_client_msg_t)N_receive_char();

        /* Debug messages */
        if (g_debug_net.value.n)
                C_trace("%s from client %d", cm_to_string(token), client);

        /* Only certain messages allowed when the game is over */
        if (g_game_over)
                switch (token) {
                case G_CM_CHAT:
                case G_CM_NAME:
                        break;
                default:
                        return;
                }

        switch (token) {
        case G_CM_AFFILIATE:
                cm_affiliate(client);
                break;
        case G_CM_CHAT:
                cm_chat(client);
                break;
        case G_CM_NAME:
                cm_name(client);
                break;
        case G_CM_SHIP_BUY:
                cm_ship_buy(client);
                break;
        case G_CM_SHIP_DROP:
                cm_ship_drop(client);
                break;
        case G_CM_SHIP_NAME:
                cm_ship_name(client);
                break;
        case G_CM_SHIP_MOVE:
                cm_ship_move(client);
                break;
        case G_CM_SHIP_PRICES:
                cm_ship_prices(client);
                break;
        case G_CM_SHIP_RING:
                cm_ship_ring(client);
                break;
        case G_CM_TILE_RING:
                cm_tile_ring(client);
                break;
        default:
                break;
        }
}