source: abuse/branches/lol/src/nfclient.cpp @ 732

Last change on this file since 732 was 732, checked in by Sam Hocevar, 8 years ago

build: SDL2 compilation fixes.

File size: 5.5 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *  Copyright (c) 2005-2013 Sam Hocevar <sam@hocevar.net>
5 *
6 *  This software was released into the Public Domain. As with most public
7 *  domain software, no warranty is made or implied by Crack dot Com, by
8 *  Jonathan Clark, or by Sam Hocevar.
9 */
10
11#if HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#if (defined(__MACH__) || !defined(__APPLE__))
16#   include <sys/types.h>
17#endif
18#include <fcntl.h>
19#if defined HAVE_UNISTD_H
20#   include <unistd.h>
21#endif
22#include <ctype.h>
23
24#include "common.h"
25
26#include "imlib/specs.h"
27#include "imlib/dprint.h"
28
29#include "net/gserver.h"
30
31#include "netface.h"
32#include "nfserver.h"
33#include "crc.h"
34#include "cache.h"
35
36void remove_client(int client_number) { ; }
37
38CrcManager *net_crcs = NULL;
39extern net_protocol *prot;
40
41class nfs_file : public bFILE
42{
43  jFILE *local;
44  int nfs_fd;
45  int offset;
46  public :
47  nfs_file(char const *filename, char const *mode);
48  virtual int open_failure();
49  virtual int unbuffered_read(void *buf, size_t count);       // returns number of bytes read
50  int new_read(void *buf, size_t count);       // returns number of bytes read
51  virtual int unbuffered_write(void const *buf, size_t count);      // returns number of bytes written
52  virtual int unbuffered_seek(long offset, int whence);  // whence=SEEK_SET, SEEK_CUR, SEEK_END, ret=0=success
53  virtual int unbuffered_tell();
54  virtual int file_size();
55  virtual ~nfs_file();
56} ;
57
58bFILE *open_nfs_file(char const *filename, char const *mode)
59{
60  return new nfs_file(filename,mode);
61}
62
63
64nfs_file::nfs_file(char const *filename, char const *mode)
65{
66  local=NULL;
67  nfs_fd=-1;
68
69  int local_only=0;
70  char const *s=mode;
71  for (; *s; s++)    // check to see if writeable file, if so don't go through nfs
72    if (*s=='w' || *s=='W' || *s=='a' || *s=='A')
73      local_only=1;
74
75  char name[256], *c;
76  char const *f = filename;
77  c = name;
78  while (*f) { *c=*(f++); *c=toupper(*c); c++; } *c=0;
79  if (strstr(name,"REGISTER"))
80    local_only=1;
81
82  if (net_crcs && !local_only)
83  {
84    int fail1,fail2,fail3=0;
85    char const *local_filename = filename;
86    if (filename[0]=='/' && filename[1]=='/')
87    { local_filename+=2;
88      while (*local_filename && *local_filename!='/') local_filename++;
89      local_filename++;
90    }
91
92    int remote_file_num = net_crcs->GetFileNumber(local_filename);
93    uint32_t remote_crc = net_crcs->get_crc(remote_file_num, fail2);
94    if (!fail2)
95    {
96      int local_file_num = crc_manager.GetFileNumber(local_filename);
97      uint32_t local_crc = crc_manager.get_crc(local_file_num, fail1);
98      if (fail1)
99      {
100    bFILE *fp=new jFILE(local_filename,"rb");
101    if (!fp->open_failure())
102    {
103      local_crc = Crc::FromFile(fp);
104      crc_manager.set_crc(local_file_num,local_crc);
105    } else fail3=1;
106    delete fp;
107      }
108
109      if (!fail3)
110      {
111    if (local_crc==remote_crc)
112          local_only=1;
113      }
114    }
115  }
116
117#if HAVE_NETWORK
118  if (local_only)
119  {
120#endif
121    local=new jFILE(filename,mode);
122    if (local->open_failure()) { delete local; local=NULL; }
123#if HAVE_NETWORK
124  }
125  else
126  {
127    char nm[256];
128    strcpy(nm,filename);
129    nfs_fd=NF_open_file(nm,mode);
130    if (nfs_fd==-2)
131    {
132      local=new jFILE(nm,mode);
133      if (local->open_failure()) { delete local; local=NULL; }
134      nfs_fd=-1;
135    }
136  }
137#endif
138}
139
140
141int nfs_file::open_failure()
142{
143  if (local==NULL && nfs_fd<0) return 1;
144  else return 0;
145}
146
147
148int nfs_file::unbuffered_read(void *buf, size_t count)      // returns number of bytes read
149{
150  if (local)
151    return local->read(buf,count);
152#if HAVE_NETWORK
153  else if (nfs_fd>=0)
154  {
155    long a=NF_read(nfs_fd,buf,count);
156    if (a>(long)count)
157    {
158      fprintf(stderr,"ooch read too much\n");
159    }
160    return a;
161  }
162#endif
163  else return 0;
164}
165
166int nfs_file::unbuffered_write(void const *buf, size_t count)      // returns number of bytes written
167{
168  if (local)
169    return local->write(buf,count);
170#if HAVE_NETWORK
171  else
172  {
173    fprintf(stderr,"write to nfs file not allowed for now!\n");
174    exit(0);
175  }
176#endif
177  return 0;
178}
179
180
181int nfs_file::unbuffered_seek(long off, int whence) // whence=SEEK_SET, SEEK_CUR, SEEK_END, ret=0=success
182{
183  if (local)
184    return local->seek(off,whence);
185#if HAVE_NETWORK
186  else if (nfs_fd>=0)
187  {
188    if (whence!=SEEK_SET)
189      fprintf(stderr,"JC's a fork\n");
190    else
191      return NF_seek(nfs_fd,off);
192  }
193#endif
194  return 0;
195}
196
197int nfs_file::unbuffered_tell()
198{
199  if (local)          return local->tell();
200#if HAVE_NETWORK
201  else if (nfs_fd>=0) return NF_tell(nfs_fd);
202#endif
203  else                return 0;
204}
205
206
207int nfs_file::file_size()
208{
209  if (local)          return local->file_size();
210#if HAVE_NETWORK
211  else if (nfs_fd>=0) return NF_filelength(nfs_fd);
212#endif
213  else                return 0;
214}
215
216nfs_file::~nfs_file()
217{
218  flush_writes();
219  if (local)          delete local;
220#if HAVE_NETWORK
221  else if (nfs_fd>=0) NF_close(nfs_fd);
222#endif
223}
224
225int set_file_server(net_address *addr)
226{
227#if HAVE_NETWORK
228  if (NF_set_file_server(addr))
229  {
230    if (net_crcs)
231    {
232      net_crcs->clean_up();
233      delete net_crcs;
234    }
235
236    net_crcs=new CrcManager();
237    if (!net_crcs->load_crc_file(NET_CRC_FILENAME))
238    {
239      delete net_crcs;
240      net_crcs=NULL;
241      return 0;
242    }
243    return 1;
244  }
245#endif
246  return 0;
247}
248
249
250int set_file_server(char const *name)
251{
252  if (prot)
253  {
254    net_address *addr=prot->get_node_address(name,DEFAULT_COMM_PORT,0);
255    if (!addr) { dprintf("\nUnable to locate server\n"); return 0; }
256    if (!set_file_server(addr))
257    {
258      delete addr;
259      return 0;
260    } else return 1;
261  } else return 0;
262}
263
Note: See TracBrowser for help on using the repository browser.