first commit
This commit is contained in:
73
include/modules/io_net/io_net_client.h
Normal file
73
include/modules/io_net/io_net_client.h
Normal file
@ -0,0 +1,73 @@
|
||||
/* -----------------------------------------------------------------------------
|
||||
GSFramework
|
||||
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
|
||||
----------------------------------------------------------------------------- */
|
||||
|
||||
|
||||
#ifndef __NIONETCLIENT__
|
||||
#define __NIONETCLIENT__
|
||||
|
||||
|
||||
#include "io_net/io_net_client_worker_thread.h"
|
||||
#include "memory/nauto_ptr.h"
|
||||
|
||||
|
||||
namespace GS {
|
||||
namespace IO {
|
||||
|
||||
//
|
||||
class NetHandle : public Handle
|
||||
{
|
||||
friend class Net;
|
||||
|
||||
int remote_id;
|
||||
|
||||
public:
|
||||
|
||||
NetHandle(Base *io, int id) : Handle(io), remote_id(id) {}
|
||||
~NetHandle();
|
||||
};
|
||||
|
||||
/*!
|
||||
@short Networked I/O client.
|
||||
@author Emmanuel Julien (ejulien@nworks.fr)
|
||||
*/
|
||||
class Net : public Base
|
||||
{
|
||||
NetWorkerThread worker;
|
||||
|
||||
bool QueueTask(NetWorkerBaseTask &);
|
||||
|
||||
public:
|
||||
|
||||
virtual uint GetCaps() const;
|
||||
|
||||
virtual Handle *Open(const char *, Mode = ModeRead);
|
||||
virtual void Close(Handle *);
|
||||
|
||||
virtual String Hash(const char *uri);
|
||||
virtual bool Delete(const char *) { return false; }
|
||||
|
||||
virtual size_t Tell(Handle *);
|
||||
virtual size_t Seek(Handle *, ptrdiff_t offset, SeekRef = SeekCurrent);
|
||||
|
||||
virtual size_t Read(Handle *, void *, size_t);
|
||||
virtual size_t Write(Handle *, const void *, size_t) { return 0; }
|
||||
|
||||
virtual bool MkDir(const char *) { return false; }
|
||||
|
||||
//---------------------------------------------------------------------
|
||||
bool IsConnected() const;
|
||||
|
||||
bool Connect(const char *ip, int port);
|
||||
void Disconnect();
|
||||
//---------------------------------------------------------------------
|
||||
|
||||
~Net() { Disconnect(); }
|
||||
};
|
||||
|
||||
} // IO
|
||||
} // GS
|
||||
|
||||
|
||||
#endif // __NIONETCLIENT__
|
||||
131
include/modules/io_net/io_net_client_worker_thread.h
Normal file
131
include/modules/io_net/io_net_client_worker_thread.h
Normal file
@ -0,0 +1,131 @@
|
||||
/* -----------------------------------------------------------------------------
|
||||
GSFramework
|
||||
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
|
||||
----------------------------------------------------------------------------- */
|
||||
|
||||
|
||||
#ifndef __NIONETCLIENTWORKERTHREAD__
|
||||
#define __NIONETCLIENTWORKERTHREAD__
|
||||
|
||||
|
||||
#include "network_enet/enet_network.h"
|
||||
#include "filesystem/io_handle.h"
|
||||
#include "container/narray.h"
|
||||
#include "container/nlist.h"
|
||||
#include "nstring/nstring.h"
|
||||
#include "thread/thread.h"
|
||||
#include "thread/mutex.h"
|
||||
|
||||
|
||||
namespace GS {
|
||||
namespace IO {
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
struct NetWorkerBaseTask
|
||||
{
|
||||
enum TaskType
|
||||
{ TypeOpen = 0, TypeClose, TypeSeek, TypeTell, TypeRead, TypeHash };
|
||||
|
||||
TaskType type;
|
||||
|
||||
// [EJ] a full memory barrier is required when accessing these two variables
|
||||
Threading::Atomic32 processed;
|
||||
Threading::Atomic32 success;
|
||||
};
|
||||
struct NetWorkerOpenTask : public NetWorkerBaseTask
|
||||
{
|
||||
String path;
|
||||
Mode mode;
|
||||
NetWorkerOpenTask(const char *p, Mode m) : path(p), mode(m) { type = TypeOpen; }
|
||||
int handle;
|
||||
};
|
||||
struct NetWorkerCloseTask : public NetWorkerBaseTask
|
||||
{
|
||||
int handle;
|
||||
NetWorkerCloseTask(int h) : handle(h) { type = TypeClose; }
|
||||
};
|
||||
struct NetWorkerSeekTask : public NetWorkerBaseTask
|
||||
{
|
||||
int handle;
|
||||
ptrdiff_t offset;
|
||||
Base::SeekRef seek_ref;
|
||||
NetWorkerSeekTask(int h, ptrdiff_t o, Base::SeekRef ref) : handle(h), offset(o), seek_ref(ref) { type = TypeSeek; }
|
||||
size_t pos;
|
||||
};
|
||||
struct NetWorkerTellTask : public NetWorkerBaseTask
|
||||
{
|
||||
int handle;
|
||||
NetWorkerTellTask(int h) : handle(h) { type = TypeTell; }
|
||||
size_t pos;
|
||||
};
|
||||
struct NetWorkerReadTask : public NetWorkerBaseTask
|
||||
{
|
||||
int handle;
|
||||
void *data;
|
||||
size_t size;
|
||||
NetWorkerReadTask(int h, void *p, size_t s) : handle(h), data(p), size(s) { type = TypeRead; }
|
||||
size_t read_size;
|
||||
};
|
||||
struct NetWorkerHashTask : public NetWorkerBaseTask
|
||||
{
|
||||
String path;
|
||||
NetWorkerHashTask(const char *p) : path(p) { type = TypeHash; }
|
||||
String hash;
|
||||
};
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
/*
|
||||
@short I/O Network worker thread.
|
||||
@author Emmanuel Julien (ejulien@owloh.com)
|
||||
*/
|
||||
class NetWorkerThread : public Threading::Thread, public Network::Enet
|
||||
{
|
||||
String ip;
|
||||
int port;
|
||||
|
||||
void *server_peer;
|
||||
int handshaking;
|
||||
|
||||
Array <char> response;
|
||||
bool WaitServerResponse();
|
||||
void ClearServerResponse();
|
||||
|
||||
void ProcessIOPacket(const void *, size_t);
|
||||
|
||||
virtual void OnPeerConnection(void *peer);
|
||||
virtual void OnPacketReceived(void *peer, const void *, size_t);
|
||||
virtual void OnConnectionClosed(void *peer);
|
||||
|
||||
virtual void Execute();
|
||||
|
||||
Threading::Mutex task_mutex;
|
||||
List <NetWorkerBaseTask *> task_queue;
|
||||
|
||||
bool ProcessOpenTask(NetWorkerOpenTask &);
|
||||
bool ProcessCloseTask(NetWorkerCloseTask &);
|
||||
bool ProcessSeekTask(NetWorkerSeekTask &);
|
||||
bool ProcessTellTask(NetWorkerTellTask &);
|
||||
bool ProcessReadTask(NetWorkerReadTask &);
|
||||
bool ProcessHashTask(NetWorkerHashTask &);
|
||||
void ProcessTask(NetWorkerBaseTask &);
|
||||
|
||||
Threading::Atomic32 running;
|
||||
|
||||
public:
|
||||
|
||||
bool IsConnected() const { return asbool(server_peer); }
|
||||
|
||||
bool QueueTask(NetWorkerBaseTask &);
|
||||
bool CancelTask(NetWorkerBaseTask &);
|
||||
|
||||
bool Start(const char *ip, int port);
|
||||
void Stop();
|
||||
|
||||
NetWorkerThread();
|
||||
};
|
||||
|
||||
} // IO
|
||||
} // GS
|
||||
|
||||
|
||||
#endif // __NIONETCLIENTWORKERTHREAD__
|
||||
118
include/modules/io_net/io_net_server.h
Normal file
118
include/modules/io_net/io_net_server.h
Normal file
@ -0,0 +1,118 @@
|
||||
/* -----------------------------------------------------------------------------
|
||||
GSFramework
|
||||
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
|
||||
----------------------------------------------------------------------------- */
|
||||
|
||||
|
||||
#ifndef __NIONETSERVER__
|
||||
#define __NIONETSERVER__
|
||||
|
||||
|
||||
#include "network_enet/enet_network.h"
|
||||
#include "filesystem/io_handle.h"
|
||||
#include "memory/nauto_ptr.h"
|
||||
#include "container/nlist.h"
|
||||
#include "nstring/nstring.h"
|
||||
#include "time/ntime.h"
|
||||
|
||||
|
||||
namespace GS {
|
||||
namespace NML { class File; }
|
||||
namespace IO {
|
||||
|
||||
//
|
||||
template <typename T> struct Measure
|
||||
{
|
||||
Time time;
|
||||
T value;
|
||||
};
|
||||
|
||||
/*!
|
||||
@short Networked I/O server.
|
||||
@author Emmanuel Julien (ejulien@nworks.fr)
|
||||
*/
|
||||
class NetServer : public Network::Enet
|
||||
{
|
||||
SharedPtr <Base> basefs; // Support IO system.
|
||||
|
||||
Measure <int> bandwidth_measure;
|
||||
int bandwidth;
|
||||
|
||||
// Client handle info
|
||||
struct ClientHandleInfo
|
||||
{
|
||||
int id;
|
||||
String name;
|
||||
|
||||
AutoPtr <Handle> handle;
|
||||
|
||||
ClientHandleInfo() : id(0) {}
|
||||
};
|
||||
|
||||
// Connected peer
|
||||
struct Client
|
||||
{
|
||||
void *peer;
|
||||
int handshake_step;
|
||||
|
||||
AutoList <ClientHandleInfo *> handles;
|
||||
|
||||
Client(void *p) : peer(p), handshake_step(0) {}
|
||||
};
|
||||
|
||||
AutoList <Client *> clients;
|
||||
|
||||
bool ProcessOpenCommand(Client &, StringList &);
|
||||
bool ProcessReadCommand(Client &, StringList &);
|
||||
bool ProcessSeekCommand(Client &, StringList &);
|
||||
bool ProcessTellCommand(Client &, StringList &);
|
||||
bool ProcessCloseCommand(Client &, StringList &);
|
||||
bool ProcessHashCommand(Client &, StringList &);
|
||||
bool ProcessClientRequest(Client &, const String &);
|
||||
|
||||
int GetClientOpenHandleCount(const Client &) const;
|
||||
int GetClientFreeHandleIndex(const Client &) const;
|
||||
Handle *GetClientHandle(const Client &, int handle_id) const;
|
||||
Client *GetClient(void *peer);
|
||||
|
||||
public:
|
||||
|
||||
struct Statistics
|
||||
{
|
||||
bool connected;
|
||||
|
||||
int sent_data; // in bytes
|
||||
int bandwidth; // in bytes
|
||||
|
||||
int packet_loss; // in %
|
||||
|
||||
struct Handle
|
||||
{
|
||||
String name;
|
||||
};
|
||||
|
||||
Array <Handle> handles;
|
||||
|
||||
Statistics() : connected(false), sent_data(0), bandwidth(0), packet_loss(0) {}
|
||||
};
|
||||
|
||||
void GetStatistics(Statistics &);
|
||||
|
||||
const AutoList <Client *> &GetClients() const { return clients; }
|
||||
|
||||
virtual void OnPeerConnection(void *peer);
|
||||
virtual void OnPacketReceived(void *peer, const void *, size_t);
|
||||
virtual void OnConnectionClosed(void *peer);
|
||||
|
||||
bool Start(const char *ip, int port);
|
||||
void Stop();
|
||||
|
||||
NetServer(Base *);
|
||||
~NetServer();
|
||||
};
|
||||
|
||||
} // IONet
|
||||
} // GS
|
||||
|
||||
|
||||
#endif // __NIONETSERVER__
|
||||
51
include/modules/io_net/io_net_server_thread.h
Normal file
51
include/modules/io_net/io_net_server_thread.h
Normal file
@ -0,0 +1,51 @@
|
||||
/* -----------------------------------------------------------------------------
|
||||
GSFramework
|
||||
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
|
||||
----------------------------------------------------------------------------- */
|
||||
|
||||
|
||||
#ifndef __NIONETSERVERTHREAD__
|
||||
#define __NIONETSERVERTHREAD__
|
||||
|
||||
|
||||
#include "io_net/io_net_server.h"
|
||||
#include "thread/mutex.h"
|
||||
#include "thread/thread.h"
|
||||
#include "thread/atomic_value.h"
|
||||
#include "filesystem/io_base.h"
|
||||
#include "memory/nshared_ptr.h"
|
||||
#include "nstring/nstring.h"
|
||||
|
||||
|
||||
namespace GS {
|
||||
namespace IO {
|
||||
|
||||
//
|
||||
class NetServerThread : public Threading::Thread
|
||||
{
|
||||
String ip;
|
||||
int port;
|
||||
|
||||
SharedPtr <Base> basefs;
|
||||
Threading::Atomic32 state;
|
||||
|
||||
Threading::Mutex server_mutex;
|
||||
AutoPtr <NetServer> server;
|
||||
|
||||
public:
|
||||
|
||||
void GetStatistics(NetServer::Statistics &);
|
||||
|
||||
void Execute();
|
||||
bool Start(const char *ip, int port);
|
||||
void Stop();
|
||||
|
||||
NetServerThread(Base *fs) : basefs(fs) {}
|
||||
~NetServerThread();
|
||||
};
|
||||
|
||||
} // IO
|
||||
} // GS
|
||||
|
||||
|
||||
#endif // __NIONETSERVERTHREAD__
|
||||
Reference in New Issue
Block a user