File: network_unix.cpp

package info (click to toggle)
clanlib 0.5.4-1-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 10,320 kB
  • ctags: 10,893
  • sloc: cpp: 76,056; xml: 3,281; sh: 2,961; perl: 1,204; asm: 837; makefile: 775
file content (127 lines) | stat: -rw-r--r-- 2,758 bytes parent folder | download
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
	$Id: network_unix.cpp,v 1.2 2001/02/23 00:04:50 cybernerd Exp $

	------------------------------------------------------------------------
	ClanLib, the platform independent game SDK.

	This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
	version 2. See COPYING for details.

	For a total list of contributers see CREDITS.

	------------------------------------------------------------------------
*/

#include "Core/precomp.h"

#ifdef USE_NETWORK

#include <iostream>
#include <netdb.h>
#include <signal.h>

#include <Network/Generic/network_generic.h>
#include <Network/Unix/connection_provider_unix.h>
#include <Network/Unix/network_unix.h>
#include <Core/System/Unix/implementation.h>

void signal_pipe(int)
{
/*
	delete CL_Implementation_Network::delivery;
	exit(0); // bye bye
*/
	// hmm - who cares!
}

CL_Network_Generic *CL_Network_Unix::network = NULL;

extern "C"
{
        DL_PREFIX char *clan_module_identify() {
	        return "Clanlib Network implementation";
	}

        DL_PREFIX char *clan_module_abbreviation() {
	        return "net";
	}

        DL_PREFIX void clan_module_init() {
		CL_Implementation_Network::add_network();
	}
}

char *network_identify() {
  return clan_module_identify();
}

char *network_abbreviation() {
  return clan_module_abbreviation();
}

void network_init() {
  clan_module_init();
}


/*
void CL_Implementation_Network::add_network()
{
	char name[100];
	gethostname(name, 100);

	hostent *ent = gethostbyname(name);

	unsigned long local_ip = *((unsigned long *) *ent->h_addr_list);

	int pipes[4];
	pipe(pipes);
	pipe(&pipes[2]);

	int our_pid = getpid();
	if (fork()==0)
	{
		CL_UnixPipeConnection *delivery = new CL_UnixPipeConnection(pipes[0], pipes[3]);
		CL_Network_Unix::network = new CL_Network_Generic(local_ip, our_pid, delivery);

		CL_System_Generic::keep_alives.add(new CL_Network_Unix);
	}
	else
	{
		signal(SIGPIPE, signal_pipe);
		signal(SIGHUP, signal_pipe);
		signal(SIGCHLD, signal_pipe);
		signal(SIGTERM, signal_pipe);

		CL_ComputerID our_id;
		our_id.ip_addr = local_ip;
		our_id.pid = our_pid;

		CL_UnixPipeConnection delivery_connection(pipes[2], pipes[1]);
		CL_Connections_Unix *connections = new CL_Connections_Unix(&delivery_connection);
		
		delivery = new CL_NetDelivery(connections, our_id);
		delivery->run();
		delete delivery;

		exit(0);
	}
}
*/

void CL_Implementation_Network::add_network()
{
	CL_Connections_Unix *connections = new CL_Connections_Unix(NULL);

	signal(SIGPIPE, signal_pipe);

	CL_Network_Unix::network = new CL_Network_Generic(connections);
}

void CL_Implementation_Network::remove_network()
{
	if (CL_Network_Unix::network != NULL) delete CL_Network_Unix::network;
	CL_Network_Unix::network = NULL;
}

#endif // USE_NETWORK