2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2011-07-17 14:42:08 +00:00
|
|
|
#ifndef SkNetIO_DEFINED
|
|
|
|
#define SkNetIO_DEFINED
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <sys/socket.h>
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkStream.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2011-08-08 15:12:05 +00:00
|
|
|
|
2011-07-17 14:42:08 +00:00
|
|
|
/* PACKET and HEADER Format */
|
|
|
|
#define PACKET_SIZE 1024
|
|
|
|
#define HEADER_SIZE 20
|
|
|
|
#define CONTENT_SIZE 1004
|
|
|
|
|
|
|
|
#define DEFAULT_PORT 15555
|
2011-08-08 15:12:05 +00:00
|
|
|
#define MAX_WAITING_CLIENTS 3
|
2011-07-17 14:42:08 +00:00
|
|
|
#define NONBLOCKING_SOCKETS
|
|
|
|
|
|
|
|
class SkSocket {
|
|
|
|
public:
|
|
|
|
SkSocket();
|
2012-04-27 12:54:01 +00:00
|
|
|
virtual ~SkSocket();
|
2011-07-17 14:42:08 +00:00
|
|
|
|
|
|
|
enum State {
|
|
|
|
kError_state,
|
|
|
|
kBegin_state,
|
|
|
|
kIncomplete_state,
|
|
|
|
kDone_state
|
|
|
|
};
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-07-17 14:42:08 +00:00
|
|
|
enum DataType {
|
|
|
|
kPipeAppend_type,
|
|
|
|
kPipeReplace_type,
|
|
|
|
kString_type,
|
|
|
|
kInt_type
|
|
|
|
};
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-08-08 15:12:05 +00:00
|
|
|
bool isConnected() { return fConnected; }
|
|
|
|
/**
|
2011-07-17 14:42:08 +00:00
|
|
|
* Write data to the socket. Data is a pointer to the beginning of the data
|
|
|
|
* to be sent and dataSize specifies the number of bytes to send. This
|
|
|
|
* method will spread the data across multiple packets if the data can't all
|
|
|
|
* fit in a single packet. The method will write all the data to each of the
|
|
|
|
* socket's open connections until all the bytes have been successfully sent
|
|
|
|
* and return total the number of bytes written to all clients, unless there
|
|
|
|
* was an error during the transfer, in which case the method returns -1.
|
|
|
|
* For blocking sockets, write will block indefinitely if the socket at the
|
|
|
|
* other end of the connection doesn't receive any data.
|
2012-08-23 18:14:13 +00:00
|
|
|
* NOTE: This method guarantees that all of the data will be sent unless
|
|
|
|
* there was an error, so it may block temporarily when the write buffer is
|
2011-08-12 14:27:47 +00:00
|
|
|
* full
|
2011-07-17 14:42:08 +00:00
|
|
|
*/
|
|
|
|
int writePacket(void* data, size_t size, DataType type = kPipeAppend_type);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-08-08 15:12:05 +00:00
|
|
|
/**
|
2011-07-17 14:42:08 +00:00
|
|
|
* Read a logical packet from socket. The data read will be stored
|
|
|
|
* sequentially in the dataArray. This method will keep running until all
|
|
|
|
* the data in a logical chunk has been read (assembling multiple partial
|
|
|
|
* packets if necessary) and return the number of bytes successfully read,
|
2011-08-08 15:12:05 +00:00
|
|
|
* unless there was an error, in which case the method returns -1. \For
|
2011-07-17 14:42:08 +00:00
|
|
|
* nonblocking sockets, read will return 0 if there's nothing to read. For
|
|
|
|
* blocking sockets, read will block indefinitely if the socket doesn't
|
|
|
|
* receive any data.
|
2012-08-23 18:14:13 +00:00
|
|
|
* NOTE: This method guarantees that all the data in a logical packet will
|
2011-08-12 14:27:47 +00:00
|
|
|
* be read so it may block temporarily if it's waiting for parts of a
|
2011-08-08 15:12:05 +00:00
|
|
|
* packet
|
2011-07-17 14:42:08 +00:00
|
|
|
*/
|
2012-08-23 18:14:13 +00:00
|
|
|
int readPacket(void (*onRead)(int cid, const void* data, size_t size,
|
2011-07-17 14:42:08 +00:00
|
|
|
DataType type, void*), void* context);
|
|
|
|
|
2011-08-08 15:12:05 +00:00
|
|
|
/**
|
|
|
|
* Suspend network transfers until resume() is called. Leaves all
|
2012-08-23 18:14:13 +00:00
|
|
|
* connections in tact.
|
2011-08-08 15:12:05 +00:00
|
|
|
*/
|
2011-07-17 14:42:08 +00:00
|
|
|
void suspendAll() { fReadSuspended = fWriteSuspended = true; }
|
2011-08-08 15:12:05 +00:00
|
|
|
/**
|
|
|
|
* Resume all network transfers.
|
|
|
|
*/
|
2011-07-17 14:42:08 +00:00
|
|
|
void resumeAll() { fReadSuspended = fWriteSuspended = false; }
|
2011-08-08 15:12:05 +00:00
|
|
|
/**
|
|
|
|
* Other helper functions
|
|
|
|
*/
|
2011-07-17 14:42:08 +00:00
|
|
|
void suspendRead() { fReadSuspended = true; }
|
|
|
|
void resumeRead() { fReadSuspended = false; }
|
|
|
|
void suspendWrite() { fWriteSuspended = true; }
|
|
|
|
void resumeWrite() { fWriteSuspended = false; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
struct header {
|
|
|
|
bool done;
|
|
|
|
int bytes;
|
|
|
|
DataType type;
|
|
|
|
};
|
|
|
|
|
2011-08-08 15:12:05 +00:00
|
|
|
/**
|
|
|
|
* Create a socket and return its file descriptor. Returns -1 on failure
|
|
|
|
*/
|
2011-07-17 14:42:08 +00:00
|
|
|
int createSocket();
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-08-08 15:12:05 +00:00
|
|
|
/**
|
|
|
|
* Close the socket specified by the socket file descriptor argument. Will
|
|
|
|
* update fMaxfd and working set properly
|
|
|
|
*/
|
2011-07-17 14:42:08 +00:00
|
|
|
void closeSocket(int sockfd);
|
|
|
|
|
2011-08-08 15:12:05 +00:00
|
|
|
/**
|
|
|
|
* Called when a broken or terminated connection has been detected. Closes
|
|
|
|
* the socket file descriptor and removes it from the master set by default.
|
|
|
|
* Override to handle broken connections differently
|
|
|
|
*/
|
2011-07-17 14:42:08 +00:00
|
|
|
virtual void onFailedConnection(int sockfd);
|
|
|
|
|
2011-08-08 15:12:05 +00:00
|
|
|
/**
|
|
|
|
* Set the socket specified by the socket file descriptor as nonblocking
|
|
|
|
*/
|
2011-07-17 14:42:08 +00:00
|
|
|
void setNonBlocking(int sockfd);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-08-08 15:12:05 +00:00
|
|
|
/**
|
|
|
|
* Add the socket specified by the socket file descriptor to the master
|
|
|
|
* file descriptor set, which is used to in the select() to detect new data
|
|
|
|
* or connections
|
|
|
|
*/
|
2011-07-17 14:42:08 +00:00
|
|
|
void addToMasterSet(int sockfd);
|
|
|
|
|
|
|
|
bool fConnected;
|
|
|
|
bool fReady;
|
|
|
|
bool fReadSuspended;
|
|
|
|
bool fWriteSuspended;
|
|
|
|
int fMaxfd;
|
|
|
|
int fPort;
|
|
|
|
int fSockfd;
|
2012-08-23 18:14:13 +00:00
|
|
|
|
|
|
|
/**
|
2011-08-08 15:12:05 +00:00
|
|
|
* fMasterSet contains all the file descriptors to be used for read/write.
|
2012-08-23 18:14:13 +00:00
|
|
|
* For clients, this only contains the client socket. For servers, this
|
2011-08-08 15:12:05 +00:00
|
|
|
* contains all the file descriptors associated with established connections
|
|
|
|
* to clients
|
|
|
|
*/
|
2011-07-17 14:42:08 +00:00
|
|
|
fd_set fMasterSet;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TCP server. Can accept simultaneous connections to multiple SkTCPClients and
|
|
|
|
* read/write data back and forth using read/writePacket calls. Port number can
|
2011-08-08 15:12:05 +00:00
|
|
|
* be specified, but make sure that client/server use the same port
|
2011-07-17 14:42:08 +00:00
|
|
|
*/
|
|
|
|
class SkTCPServer : public SkSocket {
|
|
|
|
public:
|
|
|
|
SkTCPServer(int port = DEFAULT_PORT);
|
2012-04-27 12:54:01 +00:00
|
|
|
virtual ~SkTCPServer();
|
2011-07-17 14:42:08 +00:00
|
|
|
|
2011-08-08 15:12:05 +00:00
|
|
|
/**
|
|
|
|
* Accept any incoming connections to the server, will accept 1 connection
|
2012-08-23 18:14:13 +00:00
|
|
|
* at a time. Returns -1 on error. For blocking sockets, this method will
|
2011-08-08 15:12:05 +00:00
|
|
|
* block until a client calls connectToServer()
|
|
|
|
*/
|
|
|
|
int acceptConnections();
|
2011-07-17 14:42:08 +00:00
|
|
|
|
2011-08-08 15:12:05 +00:00
|
|
|
/**
|
|
|
|
* Disconnect all connections to clients. Returns -1 on error
|
|
|
|
*/
|
|
|
|
int disconnectAll();
|
2011-07-17 14:42:08 +00:00
|
|
|
private:
|
|
|
|
typedef SkSocket INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TCP client. Will connect to the server specified in the constructor. If a
|
|
|
|
* port number is specified, make sure that it's the same as the port number on
|
|
|
|
* the server
|
|
|
|
*/
|
|
|
|
class SkTCPClient : public SkSocket {
|
|
|
|
public:
|
|
|
|
SkTCPClient(const char* hostname, int port = DEFAULT_PORT);
|
|
|
|
|
2011-08-08 15:12:05 +00:00
|
|
|
/**
|
2012-08-23 18:14:13 +00:00
|
|
|
* Connect to server. Returns -1 on error or failure. Call this to connect
|
|
|
|
* or reconnect to the server. For blocking sockets, this method will block
|
2011-08-08 15:12:05 +00:00
|
|
|
* until the connection is accepted by the server.
|
|
|
|
*/
|
2011-07-17 14:42:08 +00:00
|
|
|
int connectToServer();
|
2011-08-08 15:12:05 +00:00
|
|
|
protected:
|
|
|
|
/**
|
2012-08-23 18:14:13 +00:00
|
|
|
* Client needs to recreate the socket when a connection is broken because
|
|
|
|
* connect can only be called successfully once.
|
2011-08-08 15:12:05 +00:00
|
|
|
*/
|
2011-07-17 14:42:08 +00:00
|
|
|
virtual void onFailedConnection(int sockfd);
|
|
|
|
private:
|
|
|
|
sockaddr_in fServerAddr;
|
|
|
|
typedef SkSocket INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|