Maintain a circular buffer of recent commands, add to crashlog.
[openttd-joker.git] / src / network / core / core.cpp
blob7901a27be00a42bc906d6628841db29d9fb0e6e3
1 /* $Id: core.cpp 24900 2013-01-08 22:46:42Z planetmaker $ */
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 */
10 /**
11 * @file core.cpp Functions used to initialize/shut down the core network
14 #ifdef ENABLE_NETWORK
16 #include "../../stdafx.h"
17 #include "../../debug.h"
18 #include "os_abstraction.h"
19 #include "packet.h"
21 #include "../../safeguards.h"
24 #ifdef __MORPHOS__
25 /* the library base is required here */
26 struct Library *SocketBase = NULL;
27 #endif
29 /**
30 * Initializes the network core (as that is needed for some platforms
31 * @return true if the core has been initialized, false otherwise
33 bool NetworkCoreInitialize()
35 #if defined(__MORPHOS__) || defined(__AMIGA__)
37 * IMPORTANT NOTE: SocketBase needs to be initialized before we use _any_
38 * network related function, else: crash.
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;
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;
63 #endif /* __AMIGA__ */
64 #endif /* __MORPHOS__ / __AMIGA__ */
66 /* Let's load the network in windows */
67 #ifdef WIN32
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;
76 #endif /* WIN32 */
78 return true;
81 /**
82 * Shuts down the network core (as that is needed for some platforms
84 void NetworkCoreShutdown()
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
94 if (SocketBase != NULL) CloseLibrary(SocketBase);
95 #endif
97 #if defined(WIN32)
98 WSACleanup();
99 #endif
104 * Serializes the GRFIdentifier (GRF ID and MD5 checksum) to the packet
105 * @param p the packet to write the data to
106 * @param grf the GRFIdentifier to serialize
108 void NetworkSocketHandler::SendGRFIdentifier(Packet *p, const GRFIdentifier *grf)
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]);
118 * Deserializes the GRFIdentifier (GRF ID and MD5 checksum) from the packet
119 * @param p the packet to read the data from
120 * @param grf the GRFIdentifier to deserialize
122 void NetworkSocketHandler::ReceiveGRFIdentifier(Packet *p, GRFIdentifier *grf)
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();
131 #endif /* ENABLE_NETWORK */