OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

core.cpp
Go to the documentation of this file.
1 /* $Id: core.cpp 26482 2014-04-23 20:13:33Z rubidium $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
9
14 #ifdef ENABLE_NETWORK
15
16 #include "../../stdafx.h"
17 #include "../../debug.h"
18 #include "os_abstraction.h"
19 #include "packet.h"
20
21 #include "../../safeguards.h"
22
23
24 #ifdef __MORPHOS__
25 /* the library base is required here */
26 struct Library *SocketBase = NULL;
27 #endif
28
33 bool NetworkCoreInitialize()
34 {
35 #if defined(__MORPHOS__) || defined(__AMIGA__)
36 /*
37 * IMPORTANT NOTE: SocketBase needs to be initialized before we use _any_
38 * network related function, else: crash.
39 */
40 DEBUG(net, 3, "[core] loading bsd socket library");
41 SocketBase = OpenLibrary("bsdsocket.library", 4);
42 if (SocketBase == NULL) {
43 DEBUG(net, 0, "[core] can't open bsdsocket.library version 4, network unavailable");
44 return false;
45 }
46
47 #if defined(__AMIGA__)
48 /* for usleep() implementation (only required for legacy AmigaOS builds) */
49 TimerPort = CreateMsgPort();
50 if (TimerPort != NULL) {
51 TimerRequest = (struct timerequest*)CreateIORequest(TimerPort, sizeof(struct timerequest);
52 if (TimerRequest != NULL) {
53 if (OpenDevice("timer.device", UNIT_MICROHZ, (struct IORequest*)TimerRequest, 0) == 0) {
54 TimerBase = TimerRequest->tr_node.io_Device;
55 if (TimerBase == NULL) {
56 /* free resources... */
57 DEBUG(net, 0, "[core] can't initialize timer, network unavailable");
58 return false;
59 }
60 }
61 }
62 }
63 #endif /* __AMIGA__ */
64 #endif /* __MORPHOS__ / __AMIGA__ */
65
66 /* Let's load the network in windows */
67 #ifdef WIN32
68 {
69 WSADATA wsa;
70 DEBUG(net, 3, "[core] loading windows socket library");
71 if (WSAStartup(MAKEWORD(2, 0), &wsa) != 0) {
72 DEBUG(net, 0, "[core] WSAStartup failed, network unavailable");
73 return false;
74 }
75 }
76 #endif /* WIN32 */
77
78 return true;
79 }
80
84 void NetworkCoreShutdown()
85 {
86 #if defined(__MORPHOS__) || defined(__AMIGA__)
87 /* free allocated resources */
88 #if defined(__AMIGA__)
89 if (TimerBase != NULL) CloseDevice((struct IORequest*)TimerRequest); // XXX This smells wrong
90 if (TimerRequest != NULL) DeleteIORequest(TimerRequest);
91 if (TimerPort != NULL) DeleteMsgPort(TimerPort);
92 #endif
93
94 if (SocketBase != NULL) CloseLibrary(SocketBase);
95 #endif
96
97 #if defined(WIN32)
98 WSACleanup();
99 #endif
100 }
101
102
108 void NetworkSocketHandler::SendGRFIdentifier(Packet *p, const GRFIdentifier *grf)
109 {
110 uint j;
111 p->Send_uint32(grf->grfid);
112 for (j = 0; j < sizeof(grf->md5sum); j++) {
113 p->Send_uint8 (grf->md5sum[j]);
114 }
115 }
116
122 void NetworkSocketHandler::ReceiveGRFIdentifier(Packet *p, GRFIdentifier *grf)
123 {
124 uint j;
125 grf->grfid = p->Recv_uint32();
126 for (j = 0; j < sizeof(grf->md5sum); j++) {
127 grf->md5sum[j] = p->Recv_uint8();
128 }
129 }
130
131 #endif /* ENABLE_NETWORK */