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

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

imlib: use vec2i for image::size and unroll all necessary changes
everywhere else in the code.

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