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
RevLine 
[56]1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
[724]4 *  Copyright (c) 2005-2013 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
[732]11#if HAVE_CONFIG_H
[555]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>
[724]19#if defined HAVE_UNISTD_H
20#   include <unistd.h>
21#endif
[56]22#include <ctype.h>
23
[512]24#include "common.h"
25
[724]26#include "imlib/specs.h"
27#include "imlib/dprint.h"
28
29#include "net/gserver.h"
30
[481]31#include "netface.h"
32#include "nfserver.h"
33#include "crc.h"
34#include "cache.h"
[2]35
36void remove_client(int client_number) { ; }
37
[123]38CrcManager *net_crcs = NULL;
[2]39extern net_protocol *prot;
40
[124]41class nfs_file : public bFILE
[2]42{
43  jFILE *local;
44  int nfs_fd;
45  int offset;
46  public :
[39]47  nfs_file(char const *filename, char const *mode);
[2]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
[39]51  virtual int unbuffered_write(void const *buf, size_t count);      // returns number of bytes written
[2]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
[39]58bFILE *open_nfs_file(char const *filename, char const *mode)
[2]59{
60  return new nfs_file(filename,mode);
61}
62
63
[39]64nfs_file::nfs_file(char const *filename, char const *mode)
[2]65{
66  local=NULL;
[124]67  nfs_fd=-1;
[2]68
69  int local_only=0;
[39]70  char const *s=mode;
[494]71  for (; *s; s++)    // check to see if writeable file, if so don't go through nfs
[124]72    if (*s=='w' || *s=='W' || *s=='a' || *s=='A')
[2]73      local_only=1;
74
[39]75  char name[256], *c;
76  char const *f = filename;
77  c = name;
[2]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;
[39]85    char const *local_filename = filename;
[2]86    if (filename[0]=='/' && filename[1]=='/')
87    { local_filename+=2;
88      while (*local_filename && *local_filename!='/') local_filename++;
89      local_filename++;
90    }
91
[713]92    int remote_file_num = net_crcs->GetFileNumber(local_filename);
93    uint32_t remote_crc = net_crcs->get_crc(remote_file_num, fail2);
[2]94    if (!fail2)
[124]95    {
[713]96      int local_file_num = crc_manager.GetFileNumber(local_filename);
97      uint32_t local_crc = crc_manager.get_crc(local_file_num, fail1);
[2]98      if (fail1)
99      {
[124]100    bFILE *fp=new jFILE(local_filename,"rb");
101    if (!fp->open_failure())
102    {
[690]103      local_crc = Crc::FromFile(fp);
[124]104      crc_manager.set_crc(local_file_num,local_crc);
105    } else fail3=1;
[494]106    delete fp;
[2]107      }
108
109      if (!fail3)
110      {
[124]111    if (local_crc==remote_crc)
[494]112          local_only=1;
[2]113      }
114    }
115  }
116
[651]117#if HAVE_NETWORK
[2]118  if (local_only)
119  {
[651]120#endif
[2]121    local=new jFILE(filename,mode);
122    if (local->open_failure()) { delete local; local=NULL; }
[651]123#if HAVE_NETWORK
[2]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    {
[124]132      local=new jFILE(nm,mode);
[2]133      if (local->open_failure()) { delete local; local=NULL; }
134      nfs_fd=-1;
135    }
136  }
[651]137#endif
[2]138}
139
140
[124]141int nfs_file::open_failure()
142{
[2]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);
[651]152#if HAVE_NETWORK
[2]153  else if (nfs_fd>=0)
154  {
155    long a=NF_read(nfs_fd,buf,count);
[4]156    if (a>(long)count)
[124]157    {
[2]158      fprintf(stderr,"ooch read too much\n");
159    }
160    return a;
161  }
[651]162#endif
[2]163  else return 0;
164}
165
[39]166int nfs_file::unbuffered_write(void const *buf, size_t count)      // returns number of bytes written
[2]167{
168  if (local)
169    return local->write(buf,count);
[651]170#if HAVE_NETWORK
[2]171  else
[124]172  {
[2]173    fprintf(stderr,"write to nfs file not allowed for now!\n");
174    exit(0);
[124]175  }
[651]176#endif
[2]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);
[651]185#if HAVE_NETWORK
[2]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);
[124]192  }
[651]193#endif
[124]194  return 0;
[2]195}
196
197int nfs_file::unbuffered_tell()
198{
199  if (local)          return local->tell();
[651]200#if HAVE_NETWORK
[2]201  else if (nfs_fd>=0) return NF_tell(nfs_fd);
[651]202#endif
[2]203  else                return 0;
204}
205
206
207int nfs_file::file_size()
208{
209  if (local)          return local->file_size();
[651]210#if HAVE_NETWORK
[2]211  else if (nfs_fd>=0) return NF_filelength(nfs_fd);
[651]212#endif
[2]213  else                return 0;
214}
215
216nfs_file::~nfs_file()
217{
218  flush_writes();
219  if (local)          delete local;
[651]220#if HAVE_NETWORK
[2]221  else if (nfs_fd>=0) NF_close(nfs_fd);
[651]222#endif
[2]223}
224
225int set_file_server(net_address *addr)
226{
[651]227#if HAVE_NETWORK
[2]228  if (NF_set_file_server(addr))
229  {
230    if (net_crcs)
231    {
232      net_crcs->clean_up();
233      delete net_crcs;
234    }
235
[123]236    net_crcs=new CrcManager();
[2]237    if (!net_crcs->load_crc_file(NET_CRC_FILENAME))
[124]238    {
239      delete net_crcs;
[2]240      net_crcs=NULL;
241      return 0;
242    }
243    return 1;
244  }
[651]245#endif
[2]246  return 0;
247}
248
249
[39]250int set_file_server(char const *name)
[2]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.