source: abuse/trunk/src/nfclient.cpp @ 659

Last change on this file since 659 was 651, checked in by Sam Hocevar, 11 years ago

build: add a --disable-network compilation flag and get rid of most of
the CELLOS_LV2 ifdefs.

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