Added GSocket for Unix (only GTK for the moment)
Updated wxSocket to use GSocket API Added a progress bar to client.cpp Added CopyTo to wxMemoryOutputStream to copy the internal buffer to a specified buffer. Various changes/fixes to the high-level protocols FTP/HTTP Various Unicode fixes Removed sckint.* git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@3085 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
233
include/wx/gsocket.h
Normal file
233
include/wx/gsocket.h
Normal file
@@ -0,0 +1,233 @@
|
|||||||
|
/* -------------------------------------------------------------------------
|
||||||
|
* Project: GSocket (Generic Socket)
|
||||||
|
* Name: gsocket.h
|
||||||
|
* Purpose: GSocket include file (system independent)
|
||||||
|
* CVSID: $Id$
|
||||||
|
* Log: $Log$
|
||||||
|
* Log: Revision 1.1 1999/07/22 17:51:47 GL
|
||||||
|
* Log: Added GSocket for Unix (only GTK for the moment)
|
||||||
|
* Log: Updated wxSocket to use GSocket API
|
||||||
|
* Log: Added a progress bar to client.cpp
|
||||||
|
* Log: Added CopyTo to wxMemoryOutputStream to copy the internal buffer to a specified buffer.
|
||||||
|
* Log: Various changes/fixes to the high-level protocols FTP/HTTP
|
||||||
|
* Log: Various Unicode fixes
|
||||||
|
* Log: Removed sckint.*
|
||||||
|
* Log:
|
||||||
|
* Log: Revision 1.2 1999/07/18 15:54:28 guilhem
|
||||||
|
* Log: Copyright, etc.
|
||||||
|
* Log:
|
||||||
|
* -------------------------------------------------------------------------
|
||||||
|
*/
|
||||||
|
#ifndef __GSOCKET_H
|
||||||
|
#define __GSOCKET_H
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
#if !defined(__cplusplus)
|
||||||
|
|
||||||
|
typedef int bool;
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef TRUE
|
||||||
|
#define TRUE 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef FALSE
|
||||||
|
#define FALSE 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef struct _GSocket GSocket;
|
||||||
|
typedef struct _GAddress GAddress;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
GSOCK_NOFAMILY = 0,
|
||||||
|
GSOCK_INET,
|
||||||
|
GSOCK_INET6,
|
||||||
|
GSOCK_UNIX
|
||||||
|
} GAddressType;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
GSOCK_STREAMED,
|
||||||
|
GSOCK_UNSTREAMED
|
||||||
|
} GSocketStream;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
GSOCK_NOERROR = 0,
|
||||||
|
GSOCK_INVOP,
|
||||||
|
GSOCK_IOERR,
|
||||||
|
GSOCK_INVADDR,
|
||||||
|
GSOCK_INVSOCK,
|
||||||
|
GSOCK_NOHOST,
|
||||||
|
GSOCK_INVPORT
|
||||||
|
} GSocketError;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
GSOCK_INPUT = 0,
|
||||||
|
GSOCK_OUTPUT = 1,
|
||||||
|
GSOCK_CONNECTION = 2,
|
||||||
|
GSOCK_LOST = 3,
|
||||||
|
GSOCK_MAX_EVENT = 4
|
||||||
|
} GSocketEvent;
|
||||||
|
|
||||||
|
enum {
|
||||||
|
GSOCK_INPUT_FLAG = 1 << GSOCK_INPUT,
|
||||||
|
GSOCK_OUTPUT_FLAG = 1 << GSOCK_OUTPUT,
|
||||||
|
GSOCK_CONNECTION_FLAG = 1 << GSOCK_CONNECTION,
|
||||||
|
GSOCK_LOST_FLAG = 1 << GSOCK_LOST,
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef int GSocketEventFlags;
|
||||||
|
|
||||||
|
typedef void (*GSocketFallback)(GSocket *socket, GSocketEvent event,
|
||||||
|
char *cdata);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Constructors / Destructors */
|
||||||
|
|
||||||
|
GSocket *GSocket_new();
|
||||||
|
void GSocket_destroy(GSocket *socket);
|
||||||
|
|
||||||
|
/* This will disable all IO calls to this socket but errors are still available */
|
||||||
|
void GSocket_Shutdown(GSocket *socket);
|
||||||
|
|
||||||
|
/* Address handling */
|
||||||
|
|
||||||
|
GSocketError GSocket_SetLocal(GSocket *socket, GAddress *address);
|
||||||
|
GSocketError GSocket_SetPeer(GSocket *socket, GAddress *address);
|
||||||
|
GAddress *GSocket_GetLocal(GSocket *socket);
|
||||||
|
GAddress *GSocket_GetPeer(GSocket *socket);
|
||||||
|
|
||||||
|
/* Non-oriented connections handlers */
|
||||||
|
|
||||||
|
GSocketError GSocket_SetNonOriented(GSocket *socket);
|
||||||
|
|
||||||
|
/* Server specific parts */
|
||||||
|
|
||||||
|
/*
|
||||||
|
GSocket_SetServer() setup the socket as a server. It uses the "Local" field
|
||||||
|
of GSocket. "Local" must be set by GSocket_SetLocal() before
|
||||||
|
GSocket_SetServer() is called. In the other case, it returns GSOCK_INVADDR.
|
||||||
|
*/
|
||||||
|
GSocketError GSocket_SetServer(GSocket *socket);
|
||||||
|
|
||||||
|
/*
|
||||||
|
GSocket_WaitConnection() waits for an incoming client connection.
|
||||||
|
*/
|
||||||
|
GSocket *GSocket_WaitConnection(GSocket *socket);
|
||||||
|
|
||||||
|
/* Client specific parts */
|
||||||
|
|
||||||
|
/*
|
||||||
|
GSocket_Connect() establishes a client connection to a server using the "Peer"
|
||||||
|
field of GSocket. "Peer" must be set by GSocket_SetPeer() before
|
||||||
|
GSocket_Connect() is called. In the other case, it returns GSOCK_INVADDR.
|
||||||
|
*/
|
||||||
|
GSocketError GSocket_Connect(GSocket *socket, GSocketStream stream);
|
||||||
|
|
||||||
|
/* Generic IO */
|
||||||
|
|
||||||
|
/* Like recv(), send(), ... */
|
||||||
|
/*
|
||||||
|
NOTE: In case we read from a non-oriented connection, the incoming (outgoing)
|
||||||
|
connection address is stored in the "Local" ("Peer") field.
|
||||||
|
*/
|
||||||
|
int GSocket_Read(GSocket *socket, char *buffer, int size);
|
||||||
|
int GSocket_Write(GSocket *socket, const char *buffer,
|
||||||
|
int size);
|
||||||
|
bool GSocket_DataAvailable(GSocket *socket);
|
||||||
|
|
||||||
|
/* Flags */
|
||||||
|
|
||||||
|
/*
|
||||||
|
GSocket_SetBlocking() puts the socket in non-blocking mode. This is useful
|
||||||
|
if we don't want to wait.
|
||||||
|
*/
|
||||||
|
void GSocket_SetBlocking(GSocket *socket, bool block);
|
||||||
|
|
||||||
|
/*
|
||||||
|
GSocket_GetError() returns the last error occured on the socket stream.
|
||||||
|
*/
|
||||||
|
|
||||||
|
GSocketError GSocket_GetError(GSocket *socket);
|
||||||
|
|
||||||
|
/* Callbacks */
|
||||||
|
|
||||||
|
/*
|
||||||
|
Only one fallback is possible for each event (INPUT, OUTPUT, CONNECTION, LOST)
|
||||||
|
INPUT: The function is called when there is at least a byte in the
|
||||||
|
input buffer
|
||||||
|
OUTPUT: The function is called when the system is sure the next write call
|
||||||
|
will not block
|
||||||
|
CONNECTION: Two cases is possible:
|
||||||
|
Client socket -> the connection is established
|
||||||
|
Server socket -> a client request a connection
|
||||||
|
LOST: the connection is lost
|
||||||
|
|
||||||
|
SetFallback accepts a combination of these flags so a same callback can
|
||||||
|
receive different events.
|
||||||
|
|
||||||
|
An event is generated only once and its state is reseted when the relative
|
||||||
|
IO call is requested.
|
||||||
|
For example: INPUT -> GSocket_Read()
|
||||||
|
CONNECTION -> GSocket_Accept()
|
||||||
|
*/
|
||||||
|
void GSocket_SetFallback(GSocket *socket, GSocketEventFlags event,
|
||||||
|
GSocketFallback fallback, char *cdata);
|
||||||
|
|
||||||
|
/*
|
||||||
|
UnsetFallback will disables all fallbacks specified by "event".
|
||||||
|
NOTE: event may be a combination of flags
|
||||||
|
*/
|
||||||
|
void GSocket_UnsetFallback(GSocket *socket, GSocketEventFlags event);
|
||||||
|
|
||||||
|
/* GAddress */
|
||||||
|
|
||||||
|
GAddress *GAddress_new();
|
||||||
|
GAddress *GAddress_copy(GAddress *address);
|
||||||
|
void GAddress_destroy(GAddress *address);
|
||||||
|
|
||||||
|
void GAddress_SetFamily(GAddress *address, GAddressType type);
|
||||||
|
GAddressType GAddress_GetFamily(GAddress *address);
|
||||||
|
|
||||||
|
/*
|
||||||
|
The use of any of the next functions will set the address family to the adapted
|
||||||
|
one. For example if you use GAddress_INET_SetHostName, address family will be AF_INET
|
||||||
|
implicitely
|
||||||
|
*/
|
||||||
|
|
||||||
|
GSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname);
|
||||||
|
GSocketError GAddress_INET_SetHostAddress(GAddress *address,
|
||||||
|
unsigned long hostaddr);
|
||||||
|
GSocketError GAddress_INET_SetPortName(GAddress *address, const char *port);
|
||||||
|
GSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port);
|
||||||
|
|
||||||
|
GSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname,
|
||||||
|
size_t sbuf);
|
||||||
|
unsigned long GAddress_INET_GetHostAddress(GAddress *address);
|
||||||
|
unsigned short GAddress_INET_GetPort(GAddress *address);
|
||||||
|
|
||||||
|
/* TODO: Define specific parts (INET6, UNIX) */
|
||||||
|
|
||||||
|
GSocketError GAddress_UNIX_SetPath(GAddress *address, const char *path);
|
||||||
|
GSocketError GAddress_UNIX_GetPath(GAddress *address, char *path, size_t sbuf);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* System specific functions
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* On systems needing an event id */
|
||||||
|
void GSocket_SetEventID(GSocket *socket, unsigned long evt_id);
|
||||||
|
|
||||||
|
/* On systems which don't have background refresh */
|
||||||
|
void GSocket_DoEvent(unsigned long evt_id);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
|
/* __GSOCKET_H */
|
@@ -45,6 +45,8 @@ class wxMemoryOutputStream: public wxOutputStream {
|
|||||||
|
|
||||||
wxStreamBuffer *OutputStreamBuffer() const { return m_o_streambuf; }
|
wxStreamBuffer *OutputStreamBuffer() const { return m_o_streambuf; }
|
||||||
|
|
||||||
|
size_t CopyTo(char *buffer, size_t len) const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
wxStreamBuffer *m_o_streambuf;
|
wxStreamBuffer *m_o_streambuf;
|
||||||
|
|
||||||
|
@@ -20,52 +20,42 @@
|
|||||||
|
|
||||||
#if wxUSE_SOCKETS
|
#if wxUSE_SOCKETS
|
||||||
|
|
||||||
#if defined(__WINDOWS__) && defined(WXSOCK_INTERNAL)
|
|
||||||
#include <winsock.h>
|
|
||||||
|
|
||||||
#elif defined(__UNIX__) && defined(WXSOCK_INTERNAL)
|
|
||||||
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "wx/string.h"
|
#include "wx/string.h"
|
||||||
|
#include "gsocket.h"
|
||||||
|
|
||||||
|
|
||||||
class WXDLLEXPORT wxSockAddress : public wxObject {
|
class WXDLLEXPORT wxSockAddress : public wxObject {
|
||||||
DECLARE_ABSTRACT_CLASS(wxSockAddress)
|
DECLARE_ABSTRACT_CLASS(wxSockAddress)
|
||||||
public:
|
public:
|
||||||
typedef enum { IPV4=1, IPV6=2, UNIX=3 } Addr;
|
typedef enum { IPV4=1, IPV6=2, UNIX=3 } Addr;
|
||||||
|
|
||||||
wxSockAddress() {};
|
wxSockAddress();
|
||||||
virtual ~wxSockAddress() {};
|
virtual ~wxSockAddress();
|
||||||
|
|
||||||
virtual void Clear() = 0;
|
virtual void Clear();
|
||||||
|
|
||||||
virtual void Build(struct sockaddr*& addr, size_t& len) = 0;
|
|
||||||
virtual void Disassemble(struct sockaddr *addr, size_t len) = 0;
|
|
||||||
virtual int SockAddrLen() = 0;
|
|
||||||
|
|
||||||
virtual int GetFamily() = 0;
|
|
||||||
virtual int Type() = 0;
|
virtual int Type() = 0;
|
||||||
|
|
||||||
|
GAddress *GetAddress() const { return m_address; }
|
||||||
|
void SetAddress(GAddress *address);
|
||||||
|
const wxSockAddress& operator =(const wxSockAddress& addr);
|
||||||
|
|
||||||
|
void CopyObject(wxObject& dest) const;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
GAddress *m_address;
|
||||||
};
|
};
|
||||||
|
|
||||||
class WXDLLEXPORT wxIPV4address : public wxSockAddress {
|
class WXDLLEXPORT wxIPV4address : public wxSockAddress {
|
||||||
DECLARE_DYNAMIC_CLASS(wxIPV4address)
|
DECLARE_DYNAMIC_CLASS(wxIPV4address)
|
||||||
private:
|
|
||||||
struct sockaddr_in *m_addr;
|
|
||||||
public:
|
public:
|
||||||
wxIPV4address();
|
wxIPV4address();
|
||||||
virtual ~wxIPV4address();
|
virtual ~wxIPV4address();
|
||||||
|
|
||||||
virtual void Clear();
|
bool Hostname(const wxString& name);
|
||||||
// const wxSockAddress& operator =(const wxSockAddress& addr);
|
bool Hostname(unsigned long addr);
|
||||||
|
bool Service(const wxString& name);
|
||||||
virtual bool Hostname(const wxString& name);
|
bool Service(unsigned short port);
|
||||||
virtual bool Hostname(unsigned long addr);
|
bool LocalHost();
|
||||||
virtual bool Service(const wxString& name);
|
|
||||||
virtual bool Service(unsigned short port);
|
|
||||||
virtual bool LocalHost();
|
|
||||||
|
|
||||||
wxString Hostname();
|
wxString Hostname();
|
||||||
unsigned short Service();
|
unsigned short Service();
|
||||||
@@ -73,8 +63,6 @@ public:
|
|||||||
void Build(struct sockaddr*& addr, size_t& len);
|
void Build(struct sockaddr*& addr, size_t& len);
|
||||||
void Disassemble(struct sockaddr *addr, size_t len);
|
void Disassemble(struct sockaddr *addr, size_t len);
|
||||||
|
|
||||||
inline int SockAddrLen();
|
|
||||||
inline int GetFamily();
|
|
||||||
inline int Type() { return wxSockAddress::IPV4; }
|
inline int Type() { return wxSockAddress::IPV4; }
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -87,9 +75,6 @@ public:
|
|||||||
wxIPV6address();
|
wxIPV6address();
|
||||||
~wxIPV6address();
|
~wxIPV6address();
|
||||||
|
|
||||||
void Clear();
|
|
||||||
// const wxSockAddress& operator =(const wxSockAddress& addr);
|
|
||||||
|
|
||||||
bool Hostname(const wxString& name);
|
bool Hostname(const wxString& name);
|
||||||
bool Hostname(unsigned char addr[16]);
|
bool Hostname(unsigned char addr[16]);
|
||||||
bool Service(const wxString& name);
|
bool Service(const wxString& name);
|
||||||
@@ -99,11 +84,6 @@ public:
|
|||||||
wxString Hostname() const;
|
wxString Hostname() const;
|
||||||
unsigned short Service() const;
|
unsigned short Service() const;
|
||||||
|
|
||||||
void Build(struct sockaddr*& addr, size_t& len);
|
|
||||||
void Disassemble(struct sockaddr *addr, size_t len);
|
|
||||||
|
|
||||||
inline int SockAddrLen();
|
|
||||||
inline int GetFamily();
|
|
||||||
inline int Type() { return wxSockAddress::IPV6; }
|
inline int Type() { return wxSockAddress::IPV6; }
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
@@ -119,17 +99,9 @@ public:
|
|||||||
wxUNIXaddress();
|
wxUNIXaddress();
|
||||||
~wxUNIXaddress();
|
~wxUNIXaddress();
|
||||||
|
|
||||||
void Clear();
|
|
||||||
// const wxSockAddress& operator =(const wxSockAddress& addr);
|
|
||||||
|
|
||||||
void Filename(const wxString& name);
|
void Filename(const wxString& name);
|
||||||
wxString Filename();
|
wxString Filename();
|
||||||
|
|
||||||
void Build(struct sockaddr*& addr, size_t& len);
|
|
||||||
void Disassemble(struct sockaddr *addr, size_t len);
|
|
||||||
|
|
||||||
inline int SockAddrLen();
|
|
||||||
inline int GetFamily();
|
|
||||||
inline int Type() { return wxSockAddress::UNIX; }
|
inline int Type() { return wxSockAddress::UNIX; }
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
@@ -1,158 +0,0 @@
|
|||||||
/////////////////////////////////////////////////////////////////////////////
|
|
||||||
// Name: sckint.h
|
|
||||||
// Purpose: Socket internal classes
|
|
||||||
// Author: Guilhem Lavaux
|
|
||||||
// Modified by:
|
|
||||||
// Created: April 1999
|
|
||||||
// RCS-ID: $Id$
|
|
||||||
// Copyright: (c) Guilhem Lavaux
|
|
||||||
// Licence: wxWindows licence
|
|
||||||
/////////////////////////////////////////////////////////////////////////////
|
|
||||||
#ifndef _WX_NETWORK_SOCKET_INT_H
|
|
||||||
#define _WX_NETWORK_SOCKET_INT_H
|
|
||||||
|
|
||||||
#ifdef __GNUG__
|
|
||||||
#pragma interface
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "wx/defs.h"
|
|
||||||
|
|
||||||
#if wxUSE_SOCKETS
|
|
||||||
|
|
||||||
#include "wx/object.h"
|
|
||||||
#include "wx/list.h"
|
|
||||||
#include "wx/socket.h"
|
|
||||||
#include "wx/thread.h"
|
|
||||||
|
|
||||||
// Socket state
|
|
||||||
class SocketState
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
// TRUE if the background notifyier is on.
|
|
||||||
bool notify_state;
|
|
||||||
// Specifies which events we want to be notified.
|
|
||||||
wxSocketBase::wxRequestNotify evt_notify_state;
|
|
||||||
// Socket flags.
|
|
||||||
wxSocketBase::wxSockFlags socket_flags;
|
|
||||||
// Pointer to the C callback function.
|
|
||||||
wxSocketBase::wxSockCbk c_callback;
|
|
||||||
char *c_callback_data;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Socket request
|
|
||||||
class SockRequest
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
// Buffer where to get/put data.
|
|
||||||
char *buffer;
|
|
||||||
// Size of the buffer.
|
|
||||||
size_t size;
|
|
||||||
// Number of bytes really read/written.
|
|
||||||
size_t io_nbytes;
|
|
||||||
// Error.
|
|
||||||
unsigned int error;
|
|
||||||
// Type of the request.
|
|
||||||
wxSocketBase::wxRequestNotify type;
|
|
||||||
// Timeout (in milliseconds).
|
|
||||||
unsigned int timeout;
|
|
||||||
// TRUE if the buffer has been processed.
|
|
||||||
bool done;
|
|
||||||
// TRUE if we must wait for the request completion, in the other case an
|
|
||||||
// event will be sent to the main thread when the request is finished.
|
|
||||||
bool wait;
|
|
||||||
};
|
|
||||||
|
|
||||||
class wxSocketInternal;
|
|
||||||
|
|
||||||
#if wxUSE_THREADS
|
|
||||||
class SocketWaiter: public wxThread {
|
|
||||||
public:
|
|
||||||
SocketWaiter(wxSocketBase *socket, wxSocketInternal *internal);
|
|
||||||
~SocketWaiter();
|
|
||||||
|
|
||||||
// Thread Entry point
|
|
||||||
// ---
|
|
||||||
virtual void *Entry();
|
|
||||||
|
|
||||||
protected:
|
|
||||||
void ProcessReadEvent();
|
|
||||||
void ProcessWriteEvent();
|
|
||||||
|
|
||||||
public:
|
|
||||||
wxSocketBase *m_socket;
|
|
||||||
wxSocketInternal *m_internal;
|
|
||||||
int m_fd;
|
|
||||||
};
|
|
||||||
#endif
|
|
||||||
|
|
||||||
class SocketRequester
|
|
||||||
#if wxUSE_THREADS
|
|
||||||
: public wxThread
|
|
||||||
#endif
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
SocketRequester(wxSocketBase *socket, wxSocketInternal *internal);
|
|
||||||
~SocketRequester();
|
|
||||||
|
|
||||||
void ProcessWaitEvent(SockRequest *req);
|
|
||||||
void ProcessReadEvent(SockRequest *req);
|
|
||||||
void ProcessWriteEvent(SockRequest *req);
|
|
||||||
|
|
||||||
bool WaitFor(wxSocketBase::wxRequestNotify req, int millisec);
|
|
||||||
|
|
||||||
#if wxUSE_THREADS
|
|
||||||
// Thread Entry point
|
|
||||||
// ---
|
|
||||||
virtual void *Entry();
|
|
||||||
#endif
|
|
||||||
|
|
||||||
public:
|
|
||||||
wxSocketBase *m_socket;
|
|
||||||
wxSocketInternal *m_internal;
|
|
||||||
int m_fd;
|
|
||||||
};
|
|
||||||
|
|
||||||
class wxSocketInternal {
|
|
||||||
public:
|
|
||||||
wxSocketInternal(wxSocketBase *socket);
|
|
||||||
~wxSocketInternal();
|
|
||||||
|
|
||||||
// wxSocket thread manager
|
|
||||||
// -----------------------
|
|
||||||
void AcquireData();
|
|
||||||
void ReleaseData();
|
|
||||||
void AcquireFD();
|
|
||||||
void ReleaseFD();
|
|
||||||
|
|
||||||
int GetFD() { return m_fd; }
|
|
||||||
void SetFD(int fd) { m_fd = fd; }
|
|
||||||
|
|
||||||
void ResumeWaiter();
|
|
||||||
void StopWaiter();
|
|
||||||
void ResumeRequester();
|
|
||||||
void StopRequester();
|
|
||||||
|
|
||||||
void QueueRequest(SockRequest *request, bool async);
|
|
||||||
void WaitForEnd(SockRequest *request);
|
|
||||||
|
|
||||||
// Used by SocketRequester
|
|
||||||
SockRequest *WaitForReq();
|
|
||||||
void EndRequest(SockRequest *req);
|
|
||||||
public:
|
|
||||||
wxSocketBase *m_socket;
|
|
||||||
#if wxUSE_THREADS
|
|
||||||
wxMutex m_socket_locker, m_fd_locker, m_request_locker, m_end_requester;
|
|
||||||
wxCondition m_socket_cond;
|
|
||||||
SocketWaiter *m_thread_waiter;
|
|
||||||
#endif
|
|
||||||
SocketRequester *m_thread_requester;
|
|
||||||
wxList m_requests;
|
|
||||||
int m_fd;
|
|
||||||
bool m_invalid_requester;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
// wxUSE_SOCKETS
|
|
||||||
|
|
||||||
#endif
|
|
||||||
// _WX_NETWORK_SOCKET_INT_H
|
|
@@ -63,9 +63,9 @@ protected:
|
|||||||
friend class wxTCPServer;
|
friend class wxTCPServer;
|
||||||
friend class wxTCPClient;
|
friend class wxTCPClient;
|
||||||
friend void Client_OnRequest(wxSocketBase&,
|
friend void Client_OnRequest(wxSocketBase&,
|
||||||
wxSocketBase::wxRequestEvent, char *);
|
GSocketEvent, char *);
|
||||||
friend void Server_OnRequest(wxSocketServer&,
|
friend void Server_OnRequest(wxSocketServer&,
|
||||||
wxSocketBase::wxRequestEvent, char *);
|
GSocketEvent, char *);
|
||||||
public:
|
public:
|
||||||
|
|
||||||
wxTCPConnection(char *buffer, int size);
|
wxTCPConnection(char *buffer, int size);
|
||||||
|
@@ -27,7 +27,6 @@ class WXDLLEXPORT wxSocketOutputStream : public wxOutputStream
|
|||||||
wxSocketOutputStream(wxSocketBase& s);
|
wxSocketOutputStream(wxSocketBase& s);
|
||||||
~wxSocketOutputStream();
|
~wxSocketOutputStream();
|
||||||
|
|
||||||
wxOutputStream& Write(const void *buffer, size_t size);
|
|
||||||
off_t SeekO( off_t WXUNUSED(pos), wxSeekMode WXUNUSED(mode) )
|
off_t SeekO( off_t WXUNUSED(pos), wxSeekMode WXUNUSED(mode) )
|
||||||
{ return -1; }
|
{ return -1; }
|
||||||
off_t TellO()
|
off_t TellO()
|
||||||
@@ -45,7 +44,6 @@ class WXDLLEXPORT wxSocketInputStream : public wxInputStream
|
|||||||
wxSocketInputStream(wxSocketBase& s);
|
wxSocketInputStream(wxSocketBase& s);
|
||||||
~wxSocketInputStream();
|
~wxSocketInputStream();
|
||||||
|
|
||||||
wxInputStream& Read(void *buffer, size_t size);
|
|
||||||
off_t SeekI( off_t WXUNUSED(pos), wxSeekMode WXUNUSED(mode) )
|
off_t SeekI( off_t WXUNUSED(pos), wxSeekMode WXUNUSED(mode) )
|
||||||
{ return -1; }
|
{ return -1; }
|
||||||
off_t TellI()
|
off_t TellI()
|
||||||
@@ -69,4 +67,4 @@ class WXDLLEXPORT wxSocketStream : public wxSocketInputStream,
|
|||||||
// wxUSE_SOCKETS && wxUSE_STREAMS
|
// wxUSE_SOCKETS && wxUSE_STREAMS
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
// __SCK_STREAM_H__
|
// __SCK_STREAM_H__
|
||||||
|
@@ -20,23 +20,6 @@
|
|||||||
|
|
||||||
#if wxUSE_SOCKETS
|
#if wxUSE_SOCKETS
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// Windows(tm) specific
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
#if defined(__WINDOWS__) && defined(WXSOCK_INTERNAL)
|
|
||||||
#include <winsock.h>
|
|
||||||
#include <wx/msw/private.h>
|
|
||||||
#endif // defined(__WINDOWS__) && defined(WXSOCK_INTERNAL)
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// Unix specific
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
#if defined(__UNIX__) && defined(WXSOCK_INTERNAL)
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#endif // defined(__UNIX__) && defined(WXSOCK_INTERNAL)
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// wxSocket headers (generic)
|
// wxSocket headers (generic)
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
@@ -48,48 +31,46 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "wx/sckaddr.h"
|
#include "wx/sckaddr.h"
|
||||||
|
#include "gsocket.h"
|
||||||
|
|
||||||
class WXDLLEXPORT wxSocketEvent;
|
class WXDLLEXPORT wxSocketEvent;
|
||||||
class WXDLLEXPORT wxSocketHandler;
|
|
||||||
class wxSocketInternal;
|
|
||||||
class WXDLLEXPORT wxSocketBase : public wxEvtHandler
|
class WXDLLEXPORT wxSocketBase : public wxEvtHandler
|
||||||
{
|
{
|
||||||
DECLARE_CLASS(wxSocketBase)
|
DECLARE_CLASS(wxSocketBase)
|
||||||
#ifdef __WXMAC__
|
|
||||||
friend void wxMacSocketOnRequestProc(void *refcon , short event) ;
|
|
||||||
#endif
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
enum wxSockFlags { NONE=0, NOWAIT=1, WAITALL=2, SPEED=4 };
|
enum wxSockFlags { NONE=0, NOWAIT=1, WAITALL=2, SPEED=4 };
|
||||||
// Type of request
|
// Type of request
|
||||||
enum { REQ_READ=0x1, REQ_PEEK=0x2, REQ_WRITE=0x4, REQ_LOST=0x8,
|
|
||||||
REQ_ACCEPT=0x10, REQ_CONNECT=0x20, REQ_WAIT=0x40};
|
|
||||||
enum { EVT_READ=0, EVT_PEEK=1, EVT_WRITE=2, EVT_LOST=3, EVT_ACCEPT=4,
|
|
||||||
EVT_CONNECT=5 };
|
|
||||||
|
|
||||||
typedef int wxRequestNotify;
|
|
||||||
typedef int wxRequestEvent;
|
|
||||||
enum wxSockType { SOCK_CLIENT, SOCK_SERVER, SOCK_INTERNAL, SOCK_UNINIT };
|
enum wxSockType { SOCK_CLIENT, SOCK_SERVER, SOCK_INTERNAL, SOCK_UNINIT };
|
||||||
typedef void (*wxSockCbk)(wxSocketBase& sock,wxRequestEvent evt,char *cdata);
|
typedef void (*wxSockCbk)(wxSocketBase& sock,GSocketEvent evt,char *cdata);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
wxSockFlags m_flags;
|
GSocket *m_socket; // wxSocket socket
|
||||||
|
wxSockFlags m_flags; // wxSocket flags
|
||||||
wxSockType m_type; // wxSocket type
|
wxSockType m_type; // wxSocket type
|
||||||
bool m_connected, m_connecting; // State of the socket
|
GSocketEventFlags m_neededreq; // Specify which requet signals we need
|
||||||
int m_fd; // Socket file descriptors
|
|
||||||
wxList m_states; // States list
|
|
||||||
int m_id; // Socket id (for event handler)
|
|
||||||
wxSocketHandler *m_handler; // the current socket handler
|
|
||||||
wxRequestNotify m_neededreq; // Specify which requet signals we need
|
|
||||||
unsigned long m_timeout;
|
|
||||||
size_t m_lcount; // Last IO request size
|
size_t m_lcount; // Last IO request size
|
||||||
int m_error; // Last IO error
|
unsigned long m_timeout; // IO timeout value
|
||||||
wxSocketInternal *m_internal;
|
|
||||||
char *m_unread; // Pushback buffer
|
char *m_unread; // Pushback buffer
|
||||||
size_t m_unrd_size; // Pushback buffer size
|
size_t m_unrd_size; // Pushback buffer size
|
||||||
wxSockCbk m_cbk;
|
size_t m_unrd_cur; // Pushback pointer
|
||||||
char *m_cdata;
|
|
||||||
bool m_notify_state;
|
wxSockCbk m_cbk; // C callback
|
||||||
|
char *m_cdata; // C callback data
|
||||||
|
|
||||||
|
bool m_connected; // Connected ?
|
||||||
|
bool m_notify_state; // Notify state
|
||||||
|
int m_id; // Socket id (for event handler)
|
||||||
|
|
||||||
|
enum {
|
||||||
|
DEFER_READ, DEFER_WRITE, NO_DEFER
|
||||||
|
} m_defering; // Defering state
|
||||||
|
char *m_defer_buffer; // Defering target buffer
|
||||||
|
size_t m_defer_nbytes; // Defering buffer size
|
||||||
|
|
||||||
|
wxList m_states; // Stack of states
|
||||||
|
|
||||||
public:
|
public:
|
||||||
wxSocketBase();
|
wxSocketBase();
|
||||||
@@ -110,14 +91,15 @@ public:
|
|||||||
void CreatePushbackBefore(const char *buffer, size_t size);
|
void CreatePushbackBefore(const char *buffer, size_t size);
|
||||||
|
|
||||||
// Status
|
// Status
|
||||||
inline bool Ok() const { return (m_fd < 0 ? 0 : 1); };
|
inline bool Ok() const { return (m_socket != NULL); };
|
||||||
inline bool Error() const { return (m_error != 0); };
|
inline bool Error() const
|
||||||
|
{ return (GSocket_GetError(m_socket) != GSOCK_NOERROR); };
|
||||||
inline bool IsConnected() const { return m_connected; };
|
inline bool IsConnected() const { return m_connected; };
|
||||||
inline bool IsDisconnected() const { return !IsConnected(); };
|
inline bool IsDisconnected() const { return !IsConnected(); };
|
||||||
inline bool IsNoWait() const { return m_flags & NOWAIT; };
|
inline bool IsNoWait() const { return ((m_flags & NOWAIT) != 0); };
|
||||||
bool IsData() const;
|
bool IsData() const;
|
||||||
inline size_t LastCount() const { return m_lcount; }
|
inline size_t LastCount() const { return m_lcount; }
|
||||||
inline int LastError() const { return m_error; }
|
inline GSocketError LastError() const { return GSocket_GetError(m_socket); }
|
||||||
inline wxSockType GetType() const { return m_type; }
|
inline wxSockType GetType() const { return m_type; }
|
||||||
|
|
||||||
void SetFlags(wxSockFlags _flags);
|
void SetFlags(wxSockFlags _flags);
|
||||||
@@ -144,11 +126,11 @@ public:
|
|||||||
void SetEventHandler(wxEvtHandler& evt_hdlr, int id = -1);
|
void SetEventHandler(wxEvtHandler& evt_hdlr, int id = -1);
|
||||||
|
|
||||||
// Method called when it happens something on the socket
|
// Method called when it happens something on the socket
|
||||||
void SetNotify(wxRequestNotify flags);
|
void SetNotify(GSocketEventFlags flags);
|
||||||
virtual void OnRequest(wxRequestEvent req_evt);
|
virtual void OnRequest(GSocketEvent req_evt);
|
||||||
|
|
||||||
// Public internal callback
|
// Public internal callback
|
||||||
virtual void OldOnNotify(wxRequestEvent WXUNUSED(evt));
|
virtual void OldOnNotify(GSocketEvent WXUNUSED(evt));
|
||||||
|
|
||||||
// Some info on the socket...
|
// Some info on the socket...
|
||||||
virtual bool GetPeer(wxSockAddress& addr_man) const;
|
virtual bool GetPeer(wxSockAddress& addr_man) const;
|
||||||
@@ -158,9 +140,9 @@ public:
|
|||||||
void Notify(bool notify);
|
void Notify(bool notify);
|
||||||
|
|
||||||
// So you can know what the socket driver is looking for ...
|
// So you can know what the socket driver is looking for ...
|
||||||
inline wxRequestNotify NeededReq() const { return m_neededreq; }
|
inline GSocketEventFlags NeededReq() const { return m_neededreq; }
|
||||||
|
|
||||||
static wxRequestNotify EventToNotify(wxRequestEvent evt);
|
static GSocketEventFlags EventToNotify(GSocketEvent evt);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
friend class wxSocketServer;
|
friend class wxSocketServer;
|
||||||
@@ -178,18 +160,13 @@ protected:
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
bool _Wait(long seconds, long microseconds, int type);
|
bool _Wait(long seconds, long microseconds, int type);
|
||||||
|
|
||||||
// Set "my" handler
|
|
||||||
inline virtual void SetHandler(wxSocketHandler *handler)
|
|
||||||
{ m_handler = handler; }
|
|
||||||
|
|
||||||
|
int DeferRead(char *buffer, size_t nbytes);
|
||||||
|
int DeferWrite(const char *buffer, size_t nbytes);
|
||||||
|
void DoDefer(GSocketEvent evt);
|
||||||
|
|
||||||
// Pushback library
|
// Pushback library
|
||||||
size_t GetPushback(char *buffer, size_t size, bool peek);
|
size_t GetPushback(char *buffer, size_t size, bool peek);
|
||||||
|
|
||||||
// To prevent many read or write on the same socket at the same time
|
|
||||||
// ==> cause strange things :-)
|
|
||||||
void WantSpeedBuffer(char *buffer, size_t size, wxRequestEvent req);
|
|
||||||
void WantBuffer(char *buffer, size_t size, wxRequestEvent req);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
@@ -221,45 +198,7 @@ public:
|
|||||||
|
|
||||||
bool WaitOnConnect(long seconds = -1, long microseconds = 0);
|
bool WaitOnConnect(long seconds = -1, long microseconds = 0);
|
||||||
|
|
||||||
virtual void OnRequest(wxRequestEvent flags);
|
virtual void OnRequest(GSocketEvent flags);
|
||||||
};
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
class WXDLLEXPORT wxSocketHandler : public wxObject
|
|
||||||
{
|
|
||||||
DECLARE_CLASS(wxSocketHandler)
|
|
||||||
protected:
|
|
||||||
wxList *socks;
|
|
||||||
|
|
||||||
public:
|
|
||||||
enum SockStatus { SOCK_NONE, SOCK_DATA, SOCK_CONNECT, SOCK_DISCONNECT,
|
|
||||||
SOCK_ERROR };
|
|
||||||
static wxSocketHandler *master;
|
|
||||||
|
|
||||||
wxSocketHandler();
|
|
||||||
virtual ~wxSocketHandler();
|
|
||||||
|
|
||||||
void Register(wxSocketBase* sock);
|
|
||||||
void UnRegister(wxSocketBase* sock);
|
|
||||||
|
|
||||||
unsigned long Count() const;
|
|
||||||
|
|
||||||
// seconds = -1 means indefinite wait
|
|
||||||
// seconds = 0 means no wait
|
|
||||||
// seconds > 0 means specified wait
|
|
||||||
|
|
||||||
int Wait(long seconds = -1, long microseconds = 0);
|
|
||||||
void YieldSock();
|
|
||||||
|
|
||||||
wxSocketServer *CreateServer
|
|
||||||
(wxSockAddress& addr,
|
|
||||||
wxSocketBase::wxSockFlags flags = wxSocketBase::NONE);
|
|
||||||
wxSocketClient *CreateClient
|
|
||||||
(wxSocketBase::wxSockFlags flags = wxSocketBase::NONE);
|
|
||||||
|
|
||||||
// Create or reuse a socket handler
|
|
||||||
static wxSocketHandler& Master() { return *master; }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class WXDLLEXPORT wxSocketEvent : public wxEvent {
|
class WXDLLEXPORT wxSocketEvent : public wxEvent {
|
||||||
@@ -267,13 +206,13 @@ class WXDLLEXPORT wxSocketEvent : public wxEvent {
|
|||||||
public:
|
public:
|
||||||
wxSocketEvent(int id = 0);
|
wxSocketEvent(int id = 0);
|
||||||
|
|
||||||
wxSocketBase::wxRequestEvent SocketEvent() const { return m_skevt; }
|
GSocketEvent SocketEvent() const { return m_skevt; }
|
||||||
wxSocketBase *Socket() const { return m_socket; }
|
wxSocketBase *Socket() const { return m_socket; }
|
||||||
|
|
||||||
void CopyObject(wxObject& obj_d) const;
|
void CopyObject(wxObject& obj_d) const;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
wxSocketBase::wxRequestEvent m_skevt;
|
GSocketEvent m_skevt;
|
||||||
wxSocketBase *m_socket;
|
wxSocketBase *m_socket;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -145,7 +145,7 @@ public:
|
|||||||
wxTextCtrl& operator<<(long i);
|
wxTextCtrl& operator<<(long i);
|
||||||
wxTextCtrl& operator<<(float f);
|
wxTextCtrl& operator<<(float f);
|
||||||
wxTextCtrl& operator<<(double d);
|
wxTextCtrl& operator<<(double d);
|
||||||
wxTextCtrl& operator<<(const char c);
|
wxTextCtrl& operator<<(const wxChar c);
|
||||||
|
|
||||||
// obsolete functions
|
// obsolete functions
|
||||||
#if WXWIN_COMPATIBILITY
|
#if WXWIN_COMPATIBILITY
|
||||||
|
@@ -28,6 +28,7 @@
|
|||||||
#include "wx/url.h"
|
#include "wx/url.h"
|
||||||
#include "wx/protocol/http.h"
|
#include "wx/protocol/http.h"
|
||||||
#include "wx/thread.h"
|
#include "wx/thread.h"
|
||||||
|
#include "wx/progdlg.h"
|
||||||
|
|
||||||
#if defined(__WXMOTIF__) || defined(__WXGTK__)
|
#if defined(__WXMOTIF__) || defined(__WXGTK__)
|
||||||
#include "mondrian.xpm"
|
#include "mondrian.xpm"
|
||||||
@@ -58,6 +59,8 @@ public:
|
|||||||
void OnExecCloseConnection(wxCommandEvent& evt);
|
void OnExecCloseConnection(wxCommandEvent& evt);
|
||||||
void UpdateStatus();
|
void UpdateStatus();
|
||||||
|
|
||||||
|
void Download(wxInputStream *input);
|
||||||
|
|
||||||
DECLARE_EVENT_TABLE()
|
DECLARE_EVENT_TABLE()
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -72,7 +75,7 @@ class MyClient: public wxSocketClient
|
|||||||
public:
|
public:
|
||||||
MyFrame *frame;
|
MyFrame *frame;
|
||||||
|
|
||||||
void OnNotify(wxRequestNotify WXUNUSED(flags)) { frame->UpdateStatus(); }
|
void OnNotify(GSocketEventFlags WXUNUSED(flags)) { frame->UpdateStatus(); }
|
||||||
};
|
};
|
||||||
|
|
||||||
// ID for the menu quit command
|
// ID for the menu quit command
|
||||||
@@ -134,14 +137,10 @@ MyFrame::MyFrame():
|
|||||||
wxFrame(NULL, -1, "wxSocket client demo",
|
wxFrame(NULL, -1, "wxSocket client demo",
|
||||||
wxDefaultPosition, wxSize(300, 200), wxDEFAULT_FRAME_STYLE)
|
wxDefaultPosition, wxSize(300, 200), wxDEFAULT_FRAME_STYLE)
|
||||||
{
|
{
|
||||||
// Init all
|
|
||||||
wxSocketHandler::Master();
|
|
||||||
|
|
||||||
sock = new MyClient();
|
sock = new MyClient();
|
||||||
sock->SetFlags((wxSocketBase::wxSockFlags) (wxSocketBase::WAITALL | wxSocketBase::SPEED));
|
sock->SetFlags((wxSocketBase::wxSockFlags) (wxSocketBase::WAITALL | wxSocketBase::SPEED));
|
||||||
wxSocketHandler::Master().Register(sock);
|
|
||||||
sock->frame = this;
|
sock->frame = this;
|
||||||
sock->SetNotify(wxSocketBase::REQ_LOST);
|
sock->SetNotify(GSOCK_LOST_FLAG);
|
||||||
CreateStatusBar(2);
|
CreateStatusBar(2);
|
||||||
UpdateStatus();
|
UpdateStatus();
|
||||||
}
|
}
|
||||||
@@ -169,6 +168,7 @@ void MyFrame::OnExecOpenConnection(wxCommandEvent& WXUNUSED(evt))
|
|||||||
addr.Service(3000);
|
addr.Service(3000);
|
||||||
sock->SetNotify(0);
|
sock->SetNotify(0);
|
||||||
sock->Connect(addr, TRUE);
|
sock->Connect(addr, TRUE);
|
||||||
|
sock->SetFlags(wxSocketBase::NONE);
|
||||||
if (!sock->IsConnected())
|
if (!sock->IsConnected())
|
||||||
wxMessageBox("Can't connect to the specified host", "Alert !");
|
wxMessageBox("Can't connect to the specified host", "Alert !");
|
||||||
|
|
||||||
@@ -177,8 +177,7 @@ void MyFrame::OnExecOpenConnection(wxCommandEvent& WXUNUSED(evt))
|
|||||||
|
|
||||||
void MyFrame::OnExecCloseConnection(wxCommandEvent& WXUNUSED(evt))
|
void MyFrame::OnExecCloseConnection(wxCommandEvent& WXUNUSED(evt))
|
||||||
{
|
{
|
||||||
if (sock)
|
sock->Close();
|
||||||
sock->Close();
|
|
||||||
UpdateStatus();
|
UpdateStatus();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -271,6 +270,43 @@ void MyFrame::OnExecTest1(wxCommandEvent& WXUNUSED(evt))
|
|||||||
delete dlgbox;
|
delete dlgbox;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void MyFrame::Download(wxInputStream *input)
|
||||||
|
{
|
||||||
|
wxProgressDialog progress("Downloading ...", "0% downloaded");
|
||||||
|
wxBufferedInputStream buf_in(*input);
|
||||||
|
wxFileOutputStream f_out("test.url");
|
||||||
|
|
||||||
|
size_t file_size = input->StreamSize();
|
||||||
|
size_t downloaded;
|
||||||
|
int BUFSIZE = (file_size > 100) ? (file_size / 100) : file_size;
|
||||||
|
int bytes_read = BUFSIZE;
|
||||||
|
wxString message;
|
||||||
|
int percents;
|
||||||
|
|
||||||
|
char *buf;
|
||||||
|
|
||||||
|
// TODO: Support for streams which don't support StreamSize
|
||||||
|
|
||||||
|
buf = new char[BUFSIZE];
|
||||||
|
|
||||||
|
downloaded = 0;
|
||||||
|
bytes_read = BUFSIZE;
|
||||||
|
while (downloaded < file_size && bytes_read != 0) {
|
||||||
|
bytes_read = buf_in.Read(buf, BUFSIZE).LastRead();
|
||||||
|
f_out.Write(buf, bytes_read);
|
||||||
|
downloaded += bytes_read;
|
||||||
|
|
||||||
|
percents = downloaded * 100 / file_size;
|
||||||
|
|
||||||
|
message = _T("");
|
||||||
|
message << percents << _T("% downloaded");
|
||||||
|
progress.Update(percents, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
delete[] buf;
|
||||||
|
}
|
||||||
|
|
||||||
void MyFrame::OnExecUrlTest(wxCommandEvent& WXUNUSED(evt))
|
void MyFrame::OnExecUrlTest(wxCommandEvent& WXUNUSED(evt))
|
||||||
{
|
{
|
||||||
wxString urlname = wxGetTextFromUser("Enter an URL to get",
|
wxString urlname = wxGetTextFromUser("Enter an URL to get",
|
||||||
@@ -284,11 +320,8 @@ void MyFrame::OnExecUrlTest(wxCommandEvent& WXUNUSED(evt))
|
|||||||
error.Printf(_T("Error in getting data from the URL. (error = %d)"), url.GetError());
|
error.Printf(_T("Error in getting data from the URL. (error = %d)"), url.GetError());
|
||||||
wxMessageBox(error, "Alert !");
|
wxMessageBox(error, "Alert !");
|
||||||
} else {
|
} else {
|
||||||
wxFileOutputStream *str_out = new wxFileOutputStream("test.url");
|
Download(datas);
|
||||||
str_out->Write(*datas);
|
|
||||||
|
|
||||||
wxMessageBox(_T("Success !! Click on OK to see the text."), "OK");
|
|
||||||
delete datas;
|
delete datas;
|
||||||
delete str_out;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -111,7 +111,7 @@ void MyFrame::OnSockRequest(wxSocketEvent& evt)
|
|||||||
printf("OnSockRequest OK\n");
|
printf("OnSockRequest OK\n");
|
||||||
printf("OnSockRequest (event = %d)\n",evt.SocketEvent());
|
printf("OnSockRequest (event = %d)\n",evt.SocketEvent());
|
||||||
switch (evt.SocketEvent()) {
|
switch (evt.SocketEvent()) {
|
||||||
case wxSocketBase::EVT_READ:
|
case GSOCK_INPUT:
|
||||||
unsigned char c;
|
unsigned char c;
|
||||||
|
|
||||||
sock->Read((char *)&c, 1);
|
sock->Read((char *)&c, 1);
|
||||||
@@ -119,7 +119,7 @@ void MyFrame::OnSockRequest(wxSocketEvent& evt)
|
|||||||
ExecTest1(sock);
|
ExecTest1(sock);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case wxSocketBase::EVT_LOST:
|
case GSOCK_LOST:
|
||||||
printf("Destroying socket\n");
|
printf("Destroying socket\n");
|
||||||
wxPendingDelete.Append(sock);
|
wxPendingDelete.Append(sock);
|
||||||
UpdateStatus(-1);
|
UpdateStatus(-1);
|
||||||
@@ -127,7 +127,6 @@ void MyFrame::OnSockRequest(wxSocketEvent& evt)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
printf("OnSockRequest Exiting\n");
|
printf("OnSockRequest Exiting\n");
|
||||||
sock->SetNotify(wxSocketBase::REQ_READ | wxSocketBase::REQ_LOST);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void MyFrame::OnSockRequestServer(wxSocketEvent& evt)
|
void MyFrame::OnSockRequestServer(wxSocketEvent& evt)
|
||||||
@@ -151,10 +150,10 @@ void MyFrame::OnSockRequestServer(wxSocketEvent& evt)
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
UpdateStatus(1);
|
UpdateStatus(1);
|
||||||
sock2->SetFlags(wxSocketBase::SPEED);
|
sock2->SetFlags(wxSocketBase::NONE);
|
||||||
sock2->Notify(TRUE);
|
sock2->Notify(TRUE);
|
||||||
sock2->SetEventHandler(*this, SKDEMO_SOCKET);
|
sock2->SetEventHandler(*this, SKDEMO_SOCKET);
|
||||||
server->SetNotify(wxSocketBase::REQ_ACCEPT);
|
sock2->SetNotify(GSOCK_INPUT_FLAG | GSOCK_LOST_FLAG);
|
||||||
}
|
}
|
||||||
|
|
||||||
// My frame Constructor
|
// My frame Constructor
|
||||||
@@ -166,11 +165,9 @@ MyFrame::MyFrame(wxFrame *frame):
|
|||||||
addr.Service(3000);
|
addr.Service(3000);
|
||||||
|
|
||||||
// Init all
|
// Init all
|
||||||
wxSocketHandler::Master();
|
|
||||||
|
|
||||||
sock = new wxSocketServer(addr);
|
sock = new wxSocketServer(addr);
|
||||||
wxSocketHandler::Master().Register(sock);
|
sock->SetNotify(GSOCK_CONNECTION_FLAG);
|
||||||
sock->SetNotify(wxSocketBase::REQ_ACCEPT);
|
|
||||||
sock->SetEventHandler(*this, SKDEMO_SOCKET_SERV);
|
sock->SetEventHandler(*this, SKDEMO_SOCKET_SERV);
|
||||||
sock->SetFlags(wxSocketBase::SPEED);
|
sock->SetFlags(wxSocketBase::SPEED);
|
||||||
sock->Notify(TRUE);
|
sock->Notify(TRUE);
|
||||||
|
@@ -56,7 +56,21 @@ bool wxFFile::Open(const wxChar *filename, const char *mode)
|
|||||||
{
|
{
|
||||||
wxASSERT_MSG( !m_fp, _T("should close or detach the old file first") );
|
wxASSERT_MSG( !m_fp, _T("should close or detach the old file first") );
|
||||||
|
|
||||||
|
#if wxUSE_UNICODE
|
||||||
|
char *tmp_fname;
|
||||||
|
size_t fname_len;
|
||||||
|
|
||||||
|
fname_len = wxStrlen(filename)+1;
|
||||||
|
tmp_fname = new char[fname_len];
|
||||||
|
wxWX2MB(tmp_fname, filename, fname_len);
|
||||||
|
|
||||||
|
m_fp = fopen(tmp_fname, mode);
|
||||||
|
|
||||||
|
delete tmp_fname;
|
||||||
|
#else
|
||||||
m_fp = fopen(filename, mode);
|
m_fp = fopen(filename, mode);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
if ( !m_fp )
|
if ( !m_fp )
|
||||||
{
|
{
|
||||||
|
@@ -69,6 +69,7 @@ wxFTP::wxFTP()
|
|||||||
m_passwd += wxGetHostName();
|
m_passwd += wxGetHostName();
|
||||||
|
|
||||||
SetNotify(0);
|
SetNotify(0);
|
||||||
|
SetFlags(NONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
wxFTP::~wxFTP()
|
wxFTP::~wxFTP()
|
||||||
@@ -81,11 +82,6 @@ wxFTP::~wxFTP()
|
|||||||
////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////
|
||||||
bool wxFTP::Connect(wxSockAddress& addr, bool WXUNUSED(wait))
|
bool wxFTP::Connect(wxSockAddress& addr, bool WXUNUSED(wait))
|
||||||
{
|
{
|
||||||
if (!m_handler) {
|
|
||||||
m_lastError = wxPROTO_NOHNDLR;
|
|
||||||
return FALSE;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!wxProtocol::Connect(addr)) {
|
if (!wxProtocol::Connect(addr)) {
|
||||||
m_lastError = wxPROTO_NETERR;
|
m_lastError = wxPROTO_NETERR;
|
||||||
return FALSE;
|
return FALSE;
|
||||||
@@ -256,7 +252,7 @@ public:
|
|||||||
size_t StreamSize() const { return m_ftpsize; }
|
size_t StreamSize() const { return m_ftpsize; }
|
||||||
virtual ~wxInputFTPStream(void)
|
virtual ~wxInputFTPStream(void)
|
||||||
{
|
{
|
||||||
if (LastError() != wxStream_NOERROR)
|
if (LastError() == wxStream_NOERROR)
|
||||||
m_ftp->GetResult('2');
|
m_ftp->GetResult('2');
|
||||||
else
|
else
|
||||||
m_ftp->Abort();
|
m_ftp->Abort();
|
||||||
@@ -284,15 +280,15 @@ wxSocketClient *wxFTP::GetPort()
|
|||||||
{
|
{
|
||||||
wxIPV4address addr;
|
wxIPV4address addr;
|
||||||
wxSocketClient *client;
|
wxSocketClient *client;
|
||||||
struct sockaddr sin;
|
|
||||||
int a[6];
|
int a[6];
|
||||||
wxString straddr;
|
wxString straddr;
|
||||||
int addr_pos;
|
int addr_pos;
|
||||||
|
wxUint16 port;
|
||||||
|
wxUint32 hostaddr;
|
||||||
|
|
||||||
if (!SendCommand(_T("PASV"), '2'))
|
if (!SendCommand(_T("PASV"), '2'))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
sin.sa_family = AF_INET;
|
|
||||||
addr_pos = m_lastResult.Find(_T('('));
|
addr_pos = m_lastResult.Find(_T('('));
|
||||||
if (addr_pos == -1) {
|
if (addr_pos == -1) {
|
||||||
m_lastError = wxPROTO_PROTERR;
|
m_lastError = wxPROTO_PROTERR;
|
||||||
@@ -300,16 +296,15 @@ wxSocketClient *wxFTP::GetPort()
|
|||||||
}
|
}
|
||||||
straddr = m_lastResult(addr_pos+1, m_lastResult.Length());
|
straddr = m_lastResult(addr_pos+1, m_lastResult.Length());
|
||||||
wxSscanf((const wxChar *)straddr,_T("%d,%d,%d,%d,%d,%d"),&a[2],&a[3],&a[4],&a[5],&a[0],&a[1]);
|
wxSscanf((const wxChar *)straddr,_T("%d,%d,%d,%d,%d,%d"),&a[2],&a[3],&a[4],&a[5],&a[0],&a[1]);
|
||||||
sin.sa_data[2] = (char)a[2];
|
|
||||||
sin.sa_data[3] = (char)a[3];
|
|
||||||
sin.sa_data[4] = (char)a[4];
|
|
||||||
sin.sa_data[5] = (char)a[5];
|
|
||||||
sin.sa_data[0] = (char)a[0];
|
|
||||||
sin.sa_data[1] = (char)a[1];
|
|
||||||
|
|
||||||
addr.Disassemble(&sin, sizeof(sin));
|
hostaddr = (wxUint16)a[5] << 24 | (wxUint16)a[4] << 16 |
|
||||||
|
(wxUint16)a[3] << 8 | a[2];
|
||||||
|
addr.Hostname(hostaddr);
|
||||||
|
|
||||||
client = m_handler->CreateClient();
|
port = (wxUint16)a[0] << 8 | a[1];
|
||||||
|
addr.Service(port);
|
||||||
|
|
||||||
|
client = new wxSocketClient();
|
||||||
if (!client->Connect(addr)) {
|
if (!client->Connect(addr)) {
|
||||||
delete client;
|
delete client;
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@@ -46,7 +46,7 @@ wxHTTP::wxHTTP()
|
|||||||
m_addr = NULL;
|
m_addr = NULL;
|
||||||
m_read = FALSE;
|
m_read = FALSE;
|
||||||
|
|
||||||
SetNotify(REQ_LOST);
|
SetNotify(GSOCK_LOST_FLAG);
|
||||||
}
|
}
|
||||||
|
|
||||||
wxHTTP::~wxHTTP()
|
wxHTTP::~wxHTTP()
|
||||||
@@ -120,8 +120,8 @@ bool wxHTTP::ParseHeaders()
|
|||||||
m_read = TRUE;
|
m_read = TRUE;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
m_error = GetLine(this, line);
|
m_perr = GetLine(this, line);
|
||||||
if (m_error != wxPROTO_NOERR)
|
if (m_perr != wxPROTO_NOERR)
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
|
||||||
if (line.Length() == 0)
|
if (line.Length() == 0)
|
||||||
@@ -154,7 +154,7 @@ bool wxHTTP::Connect(const wxString& host)
|
|||||||
if (!addr->Hostname(host)) {
|
if (!addr->Hostname(host)) {
|
||||||
delete m_addr;
|
delete m_addr;
|
||||||
m_addr = NULL;
|
m_addr = NULL;
|
||||||
m_error = wxPROTO_NETERR;
|
m_perr = wxPROTO_NETERR;
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -166,22 +166,25 @@ bool wxHTTP::Connect(const wxString& host)
|
|||||||
|
|
||||||
bool wxHTTP::Connect(wxSockAddress& addr, bool WXUNUSED(wait))
|
bool wxHTTP::Connect(wxSockAddress& addr, bool WXUNUSED(wait))
|
||||||
{
|
{
|
||||||
struct sockaddr *raw_addr;
|
if (m_addr) {
|
||||||
size_t len;
|
delete m_addr;
|
||||||
|
m_addr = NULL;
|
||||||
m_addr = (wxSockAddress *)(addr.GetClassInfo()->CreateObject());
|
Close();
|
||||||
|
}
|
||||||
addr.Build(raw_addr, len);
|
|
||||||
m_addr->Disassemble(raw_addr, len);
|
|
||||||
|
|
||||||
|
m_addr = (wxSockAddress *) addr.Clone();
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxHTTP::BuildRequest(const wxString& path, wxHTTP_Req req)
|
bool wxHTTP::BuildRequest(const wxString& path, wxHTTP_Req req)
|
||||||
{
|
{
|
||||||
wxChar *tmp_buf;
|
wxChar *tmp_buf;
|
||||||
wxChar buf[200];
|
wxChar buf[200]; // 200 is arbitrary.
|
||||||
wxWX2MBbuf pathbuf(200);
|
#if wxUNICODE
|
||||||
|
wxWX2MBbuf pathbuf((size_t)200);
|
||||||
|
#else
|
||||||
|
const wxWX2MBbuf pathbuf;
|
||||||
|
#endif
|
||||||
wxString tmp_str;
|
wxString tmp_str;
|
||||||
|
|
||||||
switch (req) {
|
switch (req) {
|
||||||
@@ -197,14 +200,14 @@ bool wxHTTP::BuildRequest(const wxString& path, wxHTTP_Req req)
|
|||||||
Notify(FALSE);
|
Notify(FALSE);
|
||||||
|
|
||||||
tmp_str = wxURL::ConvertToValidURI(path);
|
tmp_str = wxURL::ConvertToValidURI(path);
|
||||||
wxSprintf(buf, _T("%s %s\n\r"), tmp_buf, tmp_str.GetData());
|
wxSprintf(buf, _T("%s %s HTTP/1.0\n\r"), tmp_buf, tmp_str.GetData());
|
||||||
pathbuf = wxConvLibc.cWX2MB(buf);
|
pathbuf = wxConvLibc.cWX2MB(buf);
|
||||||
Write(pathbuf, strlen(pathbuf));
|
Write(pathbuf, strlen(MBSTRINGCAST pathbuf));
|
||||||
SendHeaders();
|
SendHeaders();
|
||||||
Write("\n\r", 2);
|
Write("\n\r", 2);
|
||||||
|
|
||||||
m_error = GetLine(this, tmp_str);
|
m_perr = GetLine(this, tmp_str);
|
||||||
if (m_error != wxPROTO_NOERR) {
|
if (m_perr != wxPROTO_NOERR) {
|
||||||
RestoreState();
|
RestoreState();
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
@@ -228,7 +231,7 @@ bool wxHTTP::BuildRequest(const wxString& path, wxHTTP_Req req)
|
|||||||
case 200:
|
case 200:
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
m_error = wxPROTO_NOFILE;
|
m_perr = wxPROTO_NOFILE;
|
||||||
RestoreState();
|
RestoreState();
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
@@ -242,12 +245,29 @@ class wxHTTPStream : public wxSocketInputStream {
|
|||||||
public:
|
public:
|
||||||
wxHTTP *m_http;
|
wxHTTP *m_http;
|
||||||
size_t m_httpsize;
|
size_t m_httpsize;
|
||||||
|
unsigned long m_read_bytes;
|
||||||
|
|
||||||
wxHTTPStream(wxHTTP *http) : wxSocketInputStream(*http), m_http(http) {}
|
wxHTTPStream(wxHTTP *http) : wxSocketInputStream(*http), m_http(http) {}
|
||||||
size_t StreamSize() const { return m_httpsize; }
|
size_t StreamSize() const { return m_httpsize; }
|
||||||
virtual ~wxHTTPStream(void) { m_http->Abort(); }
|
virtual ~wxHTTPStream(void) { m_http->Abort(); }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
size_t OnSysRead(void *buffer, size_t bufsize);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
size_t wxHTTPStream::OnSysRead(void *buffer, size_t bufsize)
|
||||||
|
{
|
||||||
|
size_t ret;
|
||||||
|
|
||||||
|
if (m_httpsize > 0 && m_read_bytes >= m_httpsize)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
ret = wxSocketInputStream::OnSysRead(buffer, bufsize);
|
||||||
|
m_read_bytes += ret;
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
bool wxHTTP::Abort(void)
|
bool wxHTTP::Abort(void)
|
||||||
{
|
{
|
||||||
return wxSocketClient::Close();
|
return wxSocketClient::Close();
|
||||||
@@ -258,7 +278,7 @@ wxInputStream *wxHTTP::GetInputStream(const wxString& path)
|
|||||||
wxHTTPStream *inp_stream = new wxHTTPStream(this);
|
wxHTTPStream *inp_stream = new wxHTTPStream(this);
|
||||||
|
|
||||||
if (!m_addr || m_connected) {
|
if (!m_addr || m_connected) {
|
||||||
m_error = wxPROTO_CONNERR;
|
m_perr = wxPROTO_CONNERR;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -268,11 +288,15 @@ wxInputStream *wxHTTP::GetInputStream(const wxString& path)
|
|||||||
if (!BuildRequest(path, wxHTTP_GET))
|
if (!BuildRequest(path, wxHTTP_GET))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
wxPrintf(_T("Len = %s\n"), WXSTRINGCAST GetHeader(_T("Content-Length")));
|
|
||||||
if (!GetHeader(_T("Content-Length")).IsEmpty())
|
if (!GetHeader(_T("Content-Length")).IsEmpty())
|
||||||
inp_stream->m_httpsize = wxAtoi(WXSTRINGCAST GetHeader(_T("Content-Length")));
|
inp_stream->m_httpsize = wxAtoi(WXSTRINGCAST GetHeader(_T("Content-Length")));
|
||||||
|
else
|
||||||
|
inp_stream->m_httpsize = (size_t)-1;
|
||||||
|
|
||||||
|
inp_stream->m_read_bytes = 0;
|
||||||
|
|
||||||
|
Notify(FALSE);
|
||||||
|
|
||||||
SetFlags(WAITALL);
|
|
||||||
return inp_stream;
|
return inp_stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -47,6 +47,7 @@
|
|||||||
#include "wx/file.h"
|
#include "wx/file.h"
|
||||||
#include "wx/textfile.h"
|
#include "wx/textfile.h"
|
||||||
#include "wx/utils.h"
|
#include "wx/utils.h"
|
||||||
|
#include "wx/wxchar.h"
|
||||||
#include "wx/log.h"
|
#include "wx/log.h"
|
||||||
|
|
||||||
// other standard headers
|
// other standard headers
|
||||||
@@ -322,7 +323,7 @@ void wxLog::TimeStamp(wxString *str)
|
|||||||
wxChar buf[256];
|
wxChar buf[256];
|
||||||
time_t timeNow;
|
time_t timeNow;
|
||||||
(void)time(&timeNow);
|
(void)time(&timeNow);
|
||||||
wxStrftime(buf, WXSIZEOF(buf), ms_timestamp, localtime(&timeNow));
|
// wxStrftime(buf, WXSIZEOF(buf), ms_timestamp, localtime(&timeNow));
|
||||||
|
|
||||||
str->Empty();
|
str->Empty();
|
||||||
*str << buf << _T(": ");
|
*str << buf << _T(": ");
|
||||||
@@ -896,7 +897,7 @@ wxLog *wxLog::ms_pLogger = (wxLog *)NULL;
|
|||||||
bool wxLog::ms_doLog = TRUE;
|
bool wxLog::ms_doLog = TRUE;
|
||||||
bool wxLog::ms_bAutoCreate = TRUE;
|
bool wxLog::ms_bAutoCreate = TRUE;
|
||||||
|
|
||||||
const wxChar *wxLog::ms_timestamp = "%X"; // time only, no date
|
const wxChar *wxLog::ms_timestamp = _T("%X"); // time only, no date
|
||||||
|
|
||||||
wxTraceMask wxLog::ms_ulTraceMask = (wxTraceMask)0;
|
wxTraceMask wxLog::ms_ulTraceMask = (wxTraceMask)0;
|
||||||
wxArrayString wxLog::ms_aTraceMasks;
|
wxArrayString wxLog::ms_aTraceMasks;
|
||||||
|
@@ -99,5 +99,16 @@ off_t wxMemoryOutputStream::OnSysTell() const
|
|||||||
return m_o_streambuf->Tell();
|
return m_o_streambuf->Tell();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t wxMemoryOutputStream::CopyTo(char *buffer, size_t len) const
|
||||||
|
{
|
||||||
|
if (!buffer)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
if (len > StreamSize())
|
||||||
|
len = StreamSize();
|
||||||
|
|
||||||
|
memcpy(buffer, m_o_streambuf->GetBufferStart(), len);
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@@ -22,9 +22,6 @@
|
|||||||
|
|
||||||
#if wxUSE_SOCKETS
|
#if wxUSE_SOCKETS
|
||||||
|
|
||||||
#ifndef WX_PRECOMP
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
@@ -33,43 +30,10 @@
|
|||||||
#include <memory.h>
|
#include <memory.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "wx/defs.h"
|
#include <wx/defs.h>
|
||||||
#include "wx/object.h"
|
#include <wx/object.h>
|
||||||
|
#include <wx/gsocket.h>
|
||||||
#if defined(__WXMAC__)
|
#include <wx/sckaddr.h>
|
||||||
#include "/wx/mac/macsock.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(__WINDOWS__)
|
|
||||||
#include <winsock.h>
|
|
||||||
#endif // __WINDOWS__
|
|
||||||
|
|
||||||
#if defined(__UNIX__)
|
|
||||||
#ifdef VMS
|
|
||||||
#include <socket.h>
|
|
||||||
#include <in.h>
|
|
||||||
#else
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <arpa/inet.h>
|
|
||||||
#endif
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <netdb.h>
|
|
||||||
|
|
||||||
#ifdef __SUN__
|
|
||||||
extern "C"
|
|
||||||
{
|
|
||||||
struct hostent *gethostbyname(const char *name);
|
|
||||||
int gethostname(char *name, int namelen);
|
|
||||||
};
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif // __UNIX__
|
|
||||||
|
|
||||||
#include "wx/sckaddr.h"
|
|
||||||
|
|
||||||
#define CHECK_ADDRTYPE(var, type)
|
|
||||||
|
|
||||||
#if !USE_SHARED_LIBRARY
|
#if !USE_SHARED_LIBRARY
|
||||||
IMPLEMENT_ABSTRACT_CLASS(wxSockAddress, wxObject)
|
IMPLEMENT_ABSTRACT_CLASS(wxSockAddress, wxObject)
|
||||||
@@ -82,343 +46,178 @@ IMPLEMENT_DYNAMIC_CLASS(wxUNIXaddress, wxSockAddress)
|
|||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
wxIPV4address::wxIPV4address()
|
// ---------------------------------------------------------------------------
|
||||||
|
// wxIPV4address
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
wxSockAddress::wxSockAddress()
|
||||||
|
{
|
||||||
|
m_address = GAddress_new();
|
||||||
|
}
|
||||||
|
|
||||||
|
wxSockAddress::~wxSockAddress()
|
||||||
|
{
|
||||||
|
GAddress_destroy(m_address);
|
||||||
|
}
|
||||||
|
|
||||||
|
void wxSockAddress::SetAddress(GAddress *address)
|
||||||
|
{
|
||||||
|
GAddress_destroy(m_address);
|
||||||
|
m_address = GAddress_copy(address);
|
||||||
|
}
|
||||||
|
|
||||||
|
const wxSockAddress& wxSockAddress::operator=(const wxSockAddress& addr)
|
||||||
|
{
|
||||||
|
SetAddress(addr.GetAddress());
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
void wxSockAddress::CopyObject(wxObject& dest) const
|
||||||
|
{
|
||||||
|
wxSockAddress *addr = (wxSockAddress *)&dest;
|
||||||
|
|
||||||
|
wxObject::CopyObject(dest);
|
||||||
|
addr->SetAddress(GetAddress());
|
||||||
|
}
|
||||||
|
|
||||||
|
void wxSockAddress::Clear()
|
||||||
|
{
|
||||||
|
GAddress_destroy(m_address);
|
||||||
|
m_address = GAddress_new();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// wxIPV4address
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
wxIPV4address::wxIPV4address()
|
||||||
|
: wxSockAddress()
|
||||||
{
|
{
|
||||||
m_addr = new sockaddr_in;
|
|
||||||
Clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
wxIPV4address::~wxIPV4address()
|
wxIPV4address::~wxIPV4address()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
int wxIPV4address::SockAddrLen()
|
|
||||||
{
|
|
||||||
return sizeof(*m_addr);
|
|
||||||
}
|
|
||||||
|
|
||||||
int wxIPV4address::GetFamily()
|
|
||||||
{
|
|
||||||
return AF_INET;
|
|
||||||
}
|
|
||||||
|
|
||||||
void wxIPV4address::Clear()
|
|
||||||
{
|
|
||||||
memset(m_addr, 0, sizeof(*m_addr));
|
|
||||||
m_addr->sin_family = AF_INET;
|
|
||||||
m_addr->sin_addr.s_addr = INADDR_ANY;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
const wxSockAddress& wxIPV4address::operator =(const wxSockAddress& addr)
|
|
||||||
{
|
|
||||||
wxIPV4address *ip_addr = (wxIPV4address *)&addr;
|
|
||||||
CHECK_ADDRTYPE(addr, wxIPV4address);
|
|
||||||
m_addr = ip_addr->m_addr;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
bool wxIPV4address::Hostname(const wxString& name)
|
bool wxIPV4address::Hostname(const wxString& name)
|
||||||
{
|
{
|
||||||
struct hostent *theHostent;
|
return (GAddress_INET_SetHostName(m_address, name.fn_str()) == GSOCK_NOERROR);
|
||||||
struct in_addr *addr;
|
|
||||||
|
|
||||||
if (name.IsNull())
|
|
||||||
return FALSE;
|
|
||||||
|
|
||||||
if (!name.IsNumber()) {
|
|
||||||
if ((theHostent = gethostbyname(name.fn_str())) == 0) {
|
|
||||||
return FALSE;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
#ifdef __WXMAC__
|
|
||||||
long len_addr = inet_addr(name.fn_str()).s_addr ;
|
|
||||||
#else
|
|
||||||
long len_addr = inet_addr(name.fn_str());
|
|
||||||
#endif
|
|
||||||
if (len_addr == -1)
|
|
||||||
return FALSE;
|
|
||||||
m_addr->sin_addr.s_addr = len_addr;
|
|
||||||
return TRUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
addr = (struct in_addr *) *(theHostent->h_addr_list);
|
|
||||||
|
|
||||||
m_addr->sin_addr.s_addr = addr[0].s_addr;
|
|
||||||
return TRUE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxIPV4address::Hostname(unsigned long addr)
|
bool wxIPV4address::Hostname(unsigned long addr)
|
||||||
{
|
{
|
||||||
m_addr->sin_addr.s_addr = htonl(addr);
|
/* Need API */
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxIPV4address::Service(const wxString& name)
|
bool wxIPV4address::Service(const wxString& name)
|
||||||
{
|
{
|
||||||
struct servent *theServent;
|
return (GAddress_INET_SetPortName(m_address, name.fn_str()) == GSOCK_NOERROR);
|
||||||
|
|
||||||
if (name.IsNull())
|
|
||||||
return FALSE;
|
|
||||||
|
|
||||||
if (!name.IsNumber()) {
|
|
||||||
if ((theServent = getservbyname(name.fn_str(), "tcp")) == 0)
|
|
||||||
return FALSE;
|
|
||||||
} else {
|
|
||||||
if ((theServent = getservbyport(wxAtoi(name), "tcp")) == 0) {
|
|
||||||
m_addr->sin_port = htons(wxAtoi(name));
|
|
||||||
return TRUE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
m_addr->sin_port = theServent->s_port;
|
|
||||||
return TRUE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxIPV4address::Service(unsigned short port)
|
bool wxIPV4address::Service(unsigned short port)
|
||||||
{
|
{
|
||||||
m_addr->sin_port = htons(port);
|
return (GAddress_INET_SetPort(m_address, port) == GSOCK_NOERROR);
|
||||||
return TRUE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxIPV4address::LocalHost()
|
bool wxIPV4address::LocalHost()
|
||||||
{
|
{
|
||||||
static char buf[256];
|
return (GAddress_INET_SetHostName(m_address, "localhost") == GSOCK_NOERROR);
|
||||||
|
|
||||||
if (gethostname(buf, sizeof(buf)) < 0)
|
|
||||||
return Hostname("localhost");
|
|
||||||
else
|
|
||||||
return Hostname(buf);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
wxString wxIPV4address::Hostname()
|
wxString wxIPV4address::Hostname()
|
||||||
{
|
{
|
||||||
struct hostent *h_ent;
|
char hostname[1024];
|
||||||
|
|
||||||
h_ent = gethostbyaddr((char *)&(m_addr->sin_addr), sizeof(m_addr->sin_addr),
|
hostname[0] = 0;
|
||||||
GetFamily());
|
GAddress_INET_GetHostName(m_address, hostname, 1024);
|
||||||
|
return wxString(hostname);
|
||||||
if (!h_ent)
|
|
||||||
return wxString("");
|
|
||||||
else
|
|
||||||
return wxString(h_ent->h_name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned short wxIPV4address::Service()
|
unsigned short wxIPV4address::Service()
|
||||||
{
|
{
|
||||||
return ntohs(m_addr->sin_port);
|
return GAddress_INET_GetPort(m_address);
|
||||||
}
|
|
||||||
|
|
||||||
void wxIPV4address::Build(struct sockaddr *&addr, size_t& len)
|
|
||||||
{
|
|
||||||
addr = (struct sockaddr *)m_addr;
|
|
||||||
len = sizeof(*m_addr);
|
|
||||||
}
|
|
||||||
|
|
||||||
void wxIPV4address::Disassemble(struct sockaddr *addr, size_t len)
|
|
||||||
{
|
|
||||||
if (len != sizeof(*m_addr))
|
|
||||||
return;
|
|
||||||
*m_addr = *(struct sockaddr_in *)addr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef IPV6_ENABLE
|
#ifdef IPV6_ENABLE
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// wxIPV6address
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
wxIPV6address::wxIPV6address()
|
wxIPV6address::wxIPV6address()
|
||||||
|
: wxSockAddress()
|
||||||
{
|
{
|
||||||
m_addr = new sockaddr_in6;
|
|
||||||
Clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
wxIPV6address::~wxIPV6address()
|
wxIPV6address::~wxIPV6address()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
int wxIPV6address::SockAddrLen()
|
|
||||||
{
|
|
||||||
return sizeof(*m_addr);
|
|
||||||
}
|
|
||||||
|
|
||||||
int wxIPV6address::GetFamily()
|
|
||||||
{
|
|
||||||
return AF_INET6;
|
|
||||||
}
|
|
||||||
|
|
||||||
void wxIPV6address::Clear()
|
|
||||||
{
|
|
||||||
memset(m_addr, 0, sizeof(*m_addr));
|
|
||||||
m_addr->sin6_family = AF_INET6;
|
|
||||||
m_addr->sin6_addr.s_addr = INADDR_ANY;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
const wxSockAddress& wxIPV6address::operator =(const wxSockAddress& addr)
|
|
||||||
{
|
|
||||||
wxIPV6address *ip_addr = (wxIPV6address *)&addr;
|
|
||||||
|
|
||||||
CHECK_ADDRTYPE(addr, wxIPV6address);
|
|
||||||
m_addr = ip_addr->m_addr;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
bool wxIPV6address::Hostname(const wxString& name)
|
bool wxIPV6address::Hostname(const wxString& name)
|
||||||
{
|
{
|
||||||
struct hostent *theHostent;
|
return (GAddress_INET_SetHostName(m_address, name.fn_str()) == GSOCK_NOERROR);
|
||||||
struct in_addr *addr;
|
|
||||||
|
|
||||||
if (name.IsNull())
|
|
||||||
return FALSE;
|
|
||||||
|
|
||||||
if (!name.IsNumber()) {
|
|
||||||
hostent = gethostbyname2((char*) name, AF_INET6);
|
|
||||||
if (!theHostent)
|
|
||||||
return FALSE;
|
|
||||||
} else {
|
|
||||||
// Don't how to do
|
|
||||||
return FALSE;
|
|
||||||
}
|
|
||||||
|
|
||||||
addr = (struct in6_addr *) *(theHostent->h_addr_list);
|
|
||||||
|
|
||||||
m_addr->sin6_addr.s6_addr = addr[0].s6_addr;
|
|
||||||
return TRUE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxIPV6address::Hostname(unsigned char addr[16])
|
bool wxIPV6address::Hostname(unsigned char addr[16])
|
||||||
{
|
{
|
||||||
m_addr->sin6_addr.s6_addr = addr;
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxIPV6address::Service(const char *name)
|
bool wxIPV6address::Service(const char *name)
|
||||||
{
|
{
|
||||||
struct servent *theServent;
|
return (GAddress_INET_SetPortName(m_address, name.fn_str()) == GSOCK_NOERROR);
|
||||||
|
|
||||||
if (!name || !strlen(name))
|
|
||||||
return FALSE;
|
|
||||||
|
|
||||||
if (!isdigit(*name)) {
|
|
||||||
if ((theServent = getservbyname((char*) name, "tcp")) == 0)
|
|
||||||
return FALSE;
|
|
||||||
} else {
|
|
||||||
if ((theServent = getservbyport(atoi(name), "tcp")) == 0) {
|
|
||||||
m_addr->sin_port = htons(atoi(name));
|
|
||||||
return TRUE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
m_addr->sin_port = theServent->s_port;
|
|
||||||
return TRUE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxIPV6address::Service(unsigned short port)
|
bool wxIPV6address::Service(unsigned short port)
|
||||||
{
|
{
|
||||||
m_addr->sin_port = htons(port);
|
return (GAddress_INET_SetPort(m_address, port) == GSOCK_NOERROR);
|
||||||
return TRUE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxIPV6address::LocalHost()
|
bool wxIPV6address::LocalHost()
|
||||||
{
|
{
|
||||||
static char buf[256];
|
return (GAddress_INET_SetHostName(m_address, "localhost") == GSOCK_NOERROR);
|
||||||
|
|
||||||
if (gethostname(buf, sizeof(buf)) < 0)
|
|
||||||
return Hostname("localhost");
|
|
||||||
else
|
|
||||||
return Hostname(buf);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const wxString& wxIPV6address::Hostname()
|
const wxString& wxIPV6address::Hostname()
|
||||||
{
|
{
|
||||||
struct hostent *h_ent;
|
return wxString(GAddress_INET_GetHostName(m_address));
|
||||||
|
|
||||||
h_ent = gethostbyaddr((char *)&(m_addr->sin_addr), sizeof(m_addr->sin_addr),
|
|
||||||
GetFamily());
|
|
||||||
return wxString(h_ent->h_name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned short wxIPV6address::Service()
|
unsigned short wxIPV6address::Service()
|
||||||
{
|
{
|
||||||
return ntohs(m_addr->sin_port);
|
return GAddress_INET_GetPort(m_address);
|
||||||
}
|
|
||||||
|
|
||||||
void wxIPV6address::Build(struct sockaddr& *addr, size_t& len)
|
|
||||||
{
|
|
||||||
len = sizeof(*m_addr);
|
|
||||||
addr = m_addr;
|
|
||||||
}
|
|
||||||
|
|
||||||
void wxIPV6address::Disassemble(struct sockaddr& *addr, size_t len)
|
|
||||||
{
|
|
||||||
if (len != sizeof(*m_addr))
|
|
||||||
return;
|
|
||||||
*m_addr = *(struct sockaddr_in6 *)addr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef __UNIX__
|
#ifdef __UNIX__
|
||||||
#include <sys/un.h>
|
// ---------------------------------------------------------------------------
|
||||||
|
// wxUNIXaddress
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
wxUNIXaddress::wxUNIXaddress()
|
wxUNIXaddress::wxUNIXaddress()
|
||||||
|
: wxSockAddress()
|
||||||
{
|
{
|
||||||
m_addr = new sockaddr_un;
|
|
||||||
Clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
wxUNIXaddress::~wxUNIXaddress()
|
wxUNIXaddress::~wxUNIXaddress()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
int wxUNIXaddress::SockAddrLen()
|
|
||||||
{
|
|
||||||
return sizeof(*m_addr);
|
|
||||||
}
|
|
||||||
|
|
||||||
int wxUNIXaddress::GetFamily()
|
|
||||||
{
|
|
||||||
return AF_UNIX;
|
|
||||||
}
|
|
||||||
|
|
||||||
void wxUNIXaddress::Clear()
|
|
||||||
{
|
|
||||||
memset(m_addr, 0, sizeof(m_addr));
|
|
||||||
m_addr->sun_family = AF_UNIX;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
const wxSockAddress& wxUNIXaddress::operator =(const wxSockAddress& addr)
|
|
||||||
{
|
|
||||||
wxUNIXaddress *unx_addr = (wxUNIXaddress *)&addr;
|
|
||||||
CHECK_ADDRTYPE(addr, wxUNIXaddress);
|
|
||||||
m_addr = unx_addr->m_addr;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
void wxUNIXaddress::Filename(const wxString& fname)
|
void wxUNIXaddress::Filename(const wxString& fname)
|
||||||
{
|
{
|
||||||
sprintf(m_addr->sun_path, "%s", MBSTRINGCAST fname.mb_str());
|
GAddress_UNIX_SetPath(m_address, fname.fn_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
wxString wxUNIXaddress::Filename()
|
wxString wxUNIXaddress::Filename()
|
||||||
{
|
{
|
||||||
return wxString(m_addr->sun_path);
|
char path[1024];
|
||||||
|
|
||||||
|
path[0] = 0;
|
||||||
|
GAddress_UNIX_GetPath(m_address, path, 1024);
|
||||||
|
return wxString(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
void wxUNIXaddress::Build(struct sockaddr*& addr, size_t& len)
|
|
||||||
{
|
|
||||||
addr = (struct sockaddr *)m_addr;
|
|
||||||
len = sizeof(*m_addr);
|
|
||||||
}
|
|
||||||
|
|
||||||
void wxUNIXaddress::Disassemble(struct sockaddr *addr, size_t len)
|
|
||||||
{
|
|
||||||
if (len != sizeof(*m_addr))
|
|
||||||
return;
|
|
||||||
*m_addr = *(struct sockaddr_un *)addr;
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@@ -1,609 +0,0 @@
|
|||||||
///////////////////////////////////////////////////////////////////////////////
|
|
||||||
// Name: sckint.cpp
|
|
||||||
// Purpose: Socket internal classes
|
|
||||||
// Authors: Guilhem Lavaux
|
|
||||||
// Created: April 1999
|
|
||||||
// Updated:
|
|
||||||
// Copyright: (C) 1999, 1998, 1997, Guilhem Lavaux
|
|
||||||
// RCS_ID: $Id$
|
|
||||||
// License: see wxWindows license
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
|
||||||
#ifdef __GNUG__
|
|
||||||
#pragma implementation "sckint.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// For compilers that support precompilation, includes "wx.h".
|
|
||||||
#include "wx/wxprec.h"
|
|
||||||
|
|
||||||
#ifdef __BORLANDC__
|
|
||||||
#pragma hdrstop
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if wxUSE_SOCKETS
|
|
||||||
|
|
||||||
#define WXSOCK_INTERNAL
|
|
||||||
#include "wx/object.h"
|
|
||||||
#include "wx/list.h"
|
|
||||||
#include "wx/socket.h"
|
|
||||||
#include "wx/thread.h"
|
|
||||||
#include "wx/sckint.h"
|
|
||||||
#include "wx/sckint.h"
|
|
||||||
#include "wx/utils.h"
|
|
||||||
|
|
||||||
// IRIX requires bstring.h be included to use select()
|
|
||||||
#ifdef sgi
|
|
||||||
#include <bstring.h>
|
|
||||||
#endif // IRIX
|
|
||||||
|
|
||||||
#ifndef __WXSTUBS__
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <ctype.h>
|
|
||||||
|
|
||||||
// -----------------------
|
|
||||||
// System specific headers
|
|
||||||
// -----------------------
|
|
||||||
|
|
||||||
#ifdef __WXMAC__
|
|
||||||
// in order to avoid problems with our c library and double definitions
|
|
||||||
#define close closesocket
|
|
||||||
#define ioctl ioctlsocket
|
|
||||||
|
|
||||||
#include <wx/mac/macsock.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(__WINDOWS__)
|
|
||||||
#include <winsock.h>
|
|
||||||
#endif // __WINDOWS__
|
|
||||||
|
|
||||||
#if defined(__UNIX__)
|
|
||||||
|
|
||||||
#ifdef VMS
|
|
||||||
#include <socket.h>
|
|
||||||
#else
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#endif
|
|
||||||
#include <sys/ioctl.h>
|
|
||||||
|
|
||||||
#include <sys/time.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
#ifdef sun
|
|
||||||
#include <sys/filio.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif // __UNIX__
|
|
||||||
|
|
||||||
#include <signal.h>
|
|
||||||
#include <errno.h>
|
|
||||||
|
|
||||||
#ifdef __VISUALC__
|
|
||||||
#include <io.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Constants
|
|
||||||
#define READ_MASK wxSocketBase::REQ_READ | wxSocketBase::REQ_ACCEPT | wxSocketBase::REQ_LOST
|
|
||||||
#define WRITE_MASK wxSocketBase::REQ_WRITE | wxSocketBase::REQ_CONNECT
|
|
||||||
|
|
||||||
// --------------------------------------------------------------
|
|
||||||
// --------- SocketWaiter ---------------------------------------
|
|
||||||
// --------------------------------------------------------------
|
|
||||||
|
|
||||||
#if wxUSE_THREADS
|
|
||||||
SocketWaiter::SocketWaiter(wxSocketBase *socket,
|
|
||||||
wxSocketInternal *internal)
|
|
||||||
: wxThread(),
|
|
||||||
m_socket(socket), m_internal(internal), m_fd(internal->GetFD())
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
SocketWaiter::~SocketWaiter()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
void SocketWaiter::ProcessReadEvent()
|
|
||||||
{
|
|
||||||
int ret;
|
|
||||||
char c;
|
|
||||||
|
|
||||||
m_internal->AcquireFD();
|
|
||||||
ret = recv(m_fd, &c, 1, MSG_PEEK);
|
|
||||||
m_internal->ReleaseFD();
|
|
||||||
|
|
||||||
// We are a server => emit a EVT_ACCEPT event.
|
|
||||||
if (ret == -1 && m_socket->GetType() == wxSocketBase::SOCK_SERVER) {
|
|
||||||
m_socket->OnRequest(wxSocketBase::EVT_ACCEPT);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Else, no error => there is something to be read else
|
|
||||||
// we've lost the connection.
|
|
||||||
if (ret > 0) {
|
|
||||||
m_socket->OnRequest(wxSocketBase::EVT_READ);
|
|
||||||
} else {
|
|
||||||
m_socket->OnRequest(wxSocketBase::EVT_LOST);
|
|
||||||
m_internal->ReleaseData(); // In that case, we mustn't forget to unlock the mutex.
|
|
||||||
Exit(NULL);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void SocketWaiter::ProcessWriteEvent()
|
|
||||||
{
|
|
||||||
if (m_socket->IsConnected())
|
|
||||||
m_socket->OnRequest(wxSocketBase::EVT_CONNECT);
|
|
||||||
else
|
|
||||||
m_socket->OnRequest(wxSocketBase::EVT_WRITE);
|
|
||||||
}
|
|
||||||
|
|
||||||
void *SocketWaiter::Entry()
|
|
||||||
{
|
|
||||||
struct timeval tv;
|
|
||||||
fd_set sockrd_set, sockwr_set;
|
|
||||||
wxSocketEvent event;
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
while (1) {
|
|
||||||
// We won't wait.
|
|
||||||
tv.tv_sec = 0;
|
|
||||||
tv.tv_usec = 0;
|
|
||||||
|
|
||||||
FD_ZERO(&sockrd_set);
|
|
||||||
FD_ZERO(&sockwr_set);
|
|
||||||
|
|
||||||
m_internal->AcquireData();
|
|
||||||
|
|
||||||
if ((m_socket->NeededReq() & READ_MASK) != 0)
|
|
||||||
FD_SET(m_fd, &sockrd_set);
|
|
||||||
if ((m_socket->NeededReq() & WRITE_MASK) != 0)
|
|
||||||
FD_SET(m_fd, &sockwr_set);
|
|
||||||
|
|
||||||
m_internal->AcquireFD();
|
|
||||||
ret = select(m_fd+1, &sockrd_set, &sockwr_set, NULL, &tv);
|
|
||||||
m_internal->ReleaseFD();
|
|
||||||
|
|
||||||
if (FD_ISSET(m_fd, &sockrd_set))
|
|
||||||
ProcessReadEvent();
|
|
||||||
|
|
||||||
if (FD_ISSET(m_fd, &sockwr_set))
|
|
||||||
ProcessWriteEvent();
|
|
||||||
|
|
||||||
m_internal->ReleaseData();
|
|
||||||
|
|
||||||
#if wxUSE_THREADS
|
|
||||||
#ifdef Yield
|
|
||||||
#undef Yield
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// We wait for 100 ms to prevent the CPU from burning.
|
|
||||||
wxUsleep(100);
|
|
||||||
|
|
||||||
// Check whether we should exit.
|
|
||||||
if (TestDestroy())
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// --------------------------------------------------------------
|
|
||||||
// --------- SocketRequester ------------------------------------
|
|
||||||
// --------------------------------------------------------------
|
|
||||||
|
|
||||||
SocketRequester::SocketRequester(wxSocketBase *socket,
|
|
||||||
wxSocketInternal *internal)
|
|
||||||
:
|
|
||||||
#if wxUSE_THREADS
|
|
||||||
wxThread(),
|
|
||||||
#endif
|
|
||||||
m_socket(socket), m_internal(internal), m_fd(internal->GetFD())
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
SocketRequester::~SocketRequester()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
bool SocketRequester::WaitFor(wxSocketBase::wxRequestNotify req, int millisec)
|
|
||||||
{
|
|
||||||
int ret;
|
|
||||||
struct timeval tv;
|
|
||||||
fd_set sockrd_set, sockwr_set;
|
|
||||||
|
|
||||||
// We won't wait.
|
|
||||||
tv.tv_sec = millisec / 1000;
|
|
||||||
tv.tv_usec = (millisec % 1000) * 1000;
|
|
||||||
|
|
||||||
FD_ZERO(&sockrd_set);
|
|
||||||
FD_ZERO(&sockwr_set);
|
|
||||||
if ((req & READ_MASK) != 0)
|
|
||||||
FD_SET(m_fd, &sockrd_set);
|
|
||||||
if ((req & WRITE_MASK) != 0)
|
|
||||||
FD_SET(m_fd, &sockwr_set);
|
|
||||||
|
|
||||||
m_internal->AcquireFD();
|
|
||||||
ret = select(m_fd+1, &sockrd_set, &sockwr_set, NULL, &tv);
|
|
||||||
m_internal->ReleaseFD();
|
|
||||||
|
|
||||||
return (ret != 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void SocketRequester::ProcessReadEvent(SockRequest *req)
|
|
||||||
{
|
|
||||||
int ret;
|
|
||||||
size_t len;
|
|
||||||
|
|
||||||
// We'll wait for the first byte, in case a "timeout event" occurs it returns // immediately
|
|
||||||
if (!WaitFor(wxSocketBase::REQ_READ, req->timeout)) {
|
|
||||||
m_internal->EndRequest(req);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
m_internal->AcquireFD();
|
|
||||||
ret = recv(m_fd, req->buffer, req->size,
|
|
||||||
(req->type == wxSocketBase::REQ_PEEK) ? MSG_PEEK : 0);
|
|
||||||
m_internal->ReleaseFD();
|
|
||||||
|
|
||||||
// An error occured, we exit.
|
|
||||||
if (ret < 0) {
|
|
||||||
req->error = errno;
|
|
||||||
m_internal->EndRequest(req);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
len = ret;
|
|
||||||
|
|
||||||
// If the buffer isn't full (and we want it to be full), we don't unqueue it.
|
|
||||||
if ((len < req->size) && (m_socket->GetFlags() & wxSocketBase::WAITALL)) {
|
|
||||||
req->size -= len;
|
|
||||||
req->io_nbytes += len;
|
|
||||||
req->buffer += len;
|
|
||||||
|
|
||||||
if (len == 0)
|
|
||||||
m_internal->EndRequest(req);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// The End.
|
|
||||||
req->io_nbytes += len;
|
|
||||||
m_internal->EndRequest(req);
|
|
||||||
}
|
|
||||||
|
|
||||||
void SocketRequester::ProcessWriteEvent(SockRequest *req)
|
|
||||||
{
|
|
||||||
int ret;
|
|
||||||
size_t len;
|
|
||||||
|
|
||||||
if (!WaitFor(wxSocketBase::REQ_WRITE, req->timeout)) {
|
|
||||||
m_internal->EndRequest(req);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
m_internal->AcquireFD();
|
|
||||||
ret = send(m_fd, req->buffer, req->size, 0);
|
|
||||||
m_internal->ReleaseFD();
|
|
||||||
if (ret < 0) {
|
|
||||||
req->error = errno;
|
|
||||||
m_internal->EndRequest(req);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
len = ret;
|
|
||||||
if ((len < req->size) && ((m_socket->GetFlags() & wxSocketBase::WAITALL) != 0)) {
|
|
||||||
req->size -= len;
|
|
||||||
req->io_nbytes += len;
|
|
||||||
req->buffer += len;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
req->io_nbytes += len;
|
|
||||||
m_internal->EndRequest(req);
|
|
||||||
}
|
|
||||||
|
|
||||||
void SocketRequester::ProcessWaitEvent(SockRequest *req)
|
|
||||||
{
|
|
||||||
if (WaitFor(req->type, req->timeout))
|
|
||||||
req->io_nbytes = 1; // We put 1 in the counter to tell the requester
|
|
||||||
// there is no timeout.
|
|
||||||
else
|
|
||||||
req->io_nbytes = 0;
|
|
||||||
|
|
||||||
m_internal->EndRequest(req);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#if wxUSE_THREADS
|
|
||||||
void *SocketRequester::Entry()
|
|
||||||
{
|
|
||||||
SockRequest *req;
|
|
||||||
|
|
||||||
m_internal->m_request_locker.Lock();
|
|
||||||
while (1) {
|
|
||||||
// Wait for a new request or a destroy message.
|
|
||||||
req = m_internal->WaitForReq();
|
|
||||||
m_internal->m_end_requester.Lock();
|
|
||||||
if (req == NULL) {
|
|
||||||
m_internal->m_invalid_requester = TRUE;
|
|
||||||
m_internal->m_end_requester.Unlock();
|
|
||||||
m_internal->m_request_locker.Unlock();
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
m_internal->m_end_requester.Unlock();
|
|
||||||
|
|
||||||
if ((req->type & wxSocketBase::REQ_WAIT) != 0) {
|
|
||||||
ProcessWaitEvent(req);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (req->type) {
|
|
||||||
case wxSocketBase::REQ_READ:
|
|
||||||
case wxSocketBase::REQ_PEEK:
|
|
||||||
ProcessReadEvent(req);
|
|
||||||
break;
|
|
||||||
case wxSocketBase::REQ_WRITE:
|
|
||||||
ProcessWriteEvent(req);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// --------------------------------------------------------------
|
|
||||||
// --------- wxSocketInternal -----------------------------------
|
|
||||||
// --------------------------------------------------------------
|
|
||||||
|
|
||||||
wxSocketInternal::wxSocketInternal(wxSocketBase *socket)
|
|
||||||
{
|
|
||||||
m_socket = socket;
|
|
||||||
m_thread_requester = NULL;
|
|
||||||
m_thread_waiter = NULL;
|
|
||||||
m_invalid_requester = TRUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
wxSocketInternal::~wxSocketInternal()
|
|
||||||
{
|
|
||||||
StopRequester();
|
|
||||||
wxASSERT(m_thread_requester == NULL);
|
|
||||||
StopWaiter();
|
|
||||||
wxASSERT(m_thread_waiter == NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
// ----------------------------------------------------------------------
|
|
||||||
// WaitForReq: it is called by SocketRequester and should return the next
|
|
||||||
// socket request if available
|
|
||||||
// ----------------------------------------------------------------------
|
|
||||||
SockRequest *wxSocketInternal::WaitForReq()
|
|
||||||
{
|
|
||||||
#if wxUSE_THREADS
|
|
||||||
wxNode *node;
|
|
||||||
|
|
||||||
// First try.
|
|
||||||
node = m_requests.First();
|
|
||||||
if (node == NULL) {
|
|
||||||
m_socket_cond.Wait(m_request_locker, 10, 0);
|
|
||||||
|
|
||||||
// Second try, if it is unsuccessul we give up.
|
|
||||||
node = m_requests.First();
|
|
||||||
if (node == NULL)
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (SockRequest *)node->Data();
|
|
||||||
#else
|
|
||||||
return NULL;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
// ----------------------------------------------------------------------
|
|
||||||
// EndRequest: Should be called to finalize a request
|
|
||||||
// ----------------------------------------------------------------------
|
|
||||||
void wxSocketInternal::EndRequest(SockRequest *req)
|
|
||||||
{
|
|
||||||
wxNode *node = NULL;
|
|
||||||
|
|
||||||
req->done = TRUE;
|
|
||||||
|
|
||||||
node = m_requests.Member((wxObject *)req);
|
|
||||||
if (node != NULL)
|
|
||||||
delete node;
|
|
||||||
}
|
|
||||||
|
|
||||||
void wxSocketInternal::AcquireData()
|
|
||||||
{
|
|
||||||
#if wxUSE_THREADS
|
|
||||||
m_socket_locker.Lock();
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
void wxSocketInternal::ReleaseData()
|
|
||||||
{
|
|
||||||
#if wxUSE_THREADS
|
|
||||||
m_socket_locker.Unlock();
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
void wxSocketInternal::AcquireFD()
|
|
||||||
{
|
|
||||||
#if wxUSE_THREADS
|
|
||||||
m_fd_locker.Lock();
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
void wxSocketInternal::ReleaseFD()
|
|
||||||
{
|
|
||||||
#if wxUSE_THREADS
|
|
||||||
m_fd_locker.Unlock();
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
void wxSocketInternal::ResumeRequester()
|
|
||||||
{
|
|
||||||
#if wxUSE_THREADS
|
|
||||||
wxThreadError err;
|
|
||||||
|
|
||||||
wxASSERT(m_invalid_requester);
|
|
||||||
|
|
||||||
m_end_requester.Lock();
|
|
||||||
|
|
||||||
if (m_thread_requester != NULL) {
|
|
||||||
m_thread_requester->Delete(); // We must join it.
|
|
||||||
delete m_thread_requester;
|
|
||||||
}
|
|
||||||
|
|
||||||
m_invalid_requester = FALSE;
|
|
||||||
|
|
||||||
m_end_requester.Unlock();
|
|
||||||
|
|
||||||
m_thread_requester = new SocketRequester(m_socket, this);
|
|
||||||
|
|
||||||
err = m_thread_requester->Create();
|
|
||||||
wxASSERT(err == wxTHREAD_NO_ERROR);
|
|
||||||
|
|
||||||
err = m_thread_requester->Run();
|
|
||||||
wxASSERT(err == wxTHREAD_NO_ERROR);
|
|
||||||
#else
|
|
||||||
if (!m_invalid_requester)
|
|
||||||
return;
|
|
||||||
m_thread_requester = new SocketRequester(m_socket, this);
|
|
||||||
m_invalid_requester = FALSE;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
void wxSocketInternal::StopRequester()
|
|
||||||
{
|
|
||||||
#if wxUSE_THREADS
|
|
||||||
m_end_requester.Lock();
|
|
||||||
if (m_invalid_requester) {
|
|
||||||
m_end_requester.Unlock();
|
|
||||||
if (m_thread_requester) {
|
|
||||||
m_thread_requester->Delete();
|
|
||||||
delete m_thread_requester;
|
|
||||||
m_thread_requester = NULL;
|
|
||||||
}
|
|
||||||
m_invalid_requester = TRUE;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
m_end_requester.Unlock();
|
|
||||||
|
|
||||||
wxASSERT(m_thread_requester != NULL);
|
|
||||||
|
|
||||||
m_request_locker.Lock();
|
|
||||||
|
|
||||||
// Send a signal to the requester.
|
|
||||||
m_socket_cond.Signal();
|
|
||||||
|
|
||||||
m_request_locker.Unlock();
|
|
||||||
|
|
||||||
// Finish the destruction of the requester.
|
|
||||||
m_thread_requester->Delete();
|
|
||||||
|
|
||||||
delete m_thread_requester;
|
|
||||||
m_thread_requester = NULL;
|
|
||||||
m_invalid_requester = TRUE;
|
|
||||||
#else
|
|
||||||
delete m_thread_requester;
|
|
||||||
m_thread_requester = NULL;
|
|
||||||
m_invalid_requester = TRUE;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
void wxSocketInternal::ResumeWaiter()
|
|
||||||
{
|
|
||||||
#if wxUSE_THREADS
|
|
||||||
wxThreadError err;
|
|
||||||
|
|
||||||
if (m_thread_waiter != NULL)
|
|
||||||
return;
|
|
||||||
|
|
||||||
m_thread_waiter = new SocketWaiter(m_socket, this);
|
|
||||||
|
|
||||||
m_thread_waiter->SetPriority(WXTHREAD_MIN_PRIORITY);
|
|
||||||
|
|
||||||
err = m_thread_waiter->Create();
|
|
||||||
wxASSERT(err == wxTHREAD_NO_ERROR);
|
|
||||||
|
|
||||||
err = m_thread_waiter->Run();
|
|
||||||
wxASSERT(err == wxTHREAD_NO_ERROR);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
void wxSocketInternal::StopWaiter()
|
|
||||||
{
|
|
||||||
#if wxUSE_THREADS
|
|
||||||
if (m_thread_waiter == NULL)
|
|
||||||
return;
|
|
||||||
|
|
||||||
m_thread_waiter->Delete();
|
|
||||||
|
|
||||||
delete m_thread_waiter;
|
|
||||||
m_thread_waiter = NULL;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
// ----------------------------------------------------------------------
|
|
||||||
// QueueRequest:
|
|
||||||
// ----------------------------------------------------------------------
|
|
||||||
void wxSocketInternal::QueueRequest(SockRequest *request, bool async)
|
|
||||||
{
|
|
||||||
if (m_invalid_requester)
|
|
||||||
ResumeRequester();
|
|
||||||
|
|
||||||
#if wxUSE_THREADS
|
|
||||||
if (async) {
|
|
||||||
|
|
||||||
m_request_locker.Lock();
|
|
||||||
request->done = FALSE;
|
|
||||||
m_requests.Append((wxObject *)request);
|
|
||||||
m_socket_cond.Signal();
|
|
||||||
m_request_locker.Unlock();
|
|
||||||
|
|
||||||
// Wake up
|
|
||||||
|
|
||||||
if (request->wait) {
|
|
||||||
if (wxThread::IsMain())
|
|
||||||
while (!request->done) {
|
|
||||||
wxYield();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
while (!request->done) {
|
|
||||||
wxThread::Yield();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
m_request_locker.Lock();
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if ((request->type & wxSocketBase::REQ_WAIT) != 0) {
|
|
||||||
m_thread_requester->ProcessWaitEvent(request);
|
|
||||||
} else {
|
|
||||||
|
|
||||||
request->done = FALSE;
|
|
||||||
|
|
||||||
switch (request->type) {
|
|
||||||
case wxSocketBase::REQ_PEEK:
|
|
||||||
case wxSocketBase::REQ_READ:
|
|
||||||
m_thread_requester->ProcessReadEvent(request);
|
|
||||||
break;
|
|
||||||
case wxSocketBase::REQ_WRITE:
|
|
||||||
m_thread_requester->ProcessWriteEvent(request);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
request->done = TRUE;
|
|
||||||
#if wxUSE_THREADS
|
|
||||||
m_request_locker.Unlock();
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
void wxSocketInternal::WaitForEnd(SockRequest *request)
|
|
||||||
{
|
|
||||||
#if wxUSE_THREADS
|
|
||||||
// TODOTODO
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
// __WXSTUBS__
|
|
||||||
|
|
||||||
#endif
|
|
||||||
// wxUSE_SOCKETS
|
|
@@ -60,10 +60,10 @@ enum {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
void Server_OnRequest(wxSocketServer& server,
|
void Server_OnRequest(wxSocketServer& server,
|
||||||
wxSocketBase::wxRequestEvent evt,
|
GSocketEvent evt,
|
||||||
char *cdata);
|
char *cdata);
|
||||||
void Client_OnRequest(wxSocketBase& sock,
|
void Client_OnRequest(wxSocketBase& sock,
|
||||||
wxSocketBase::wxRequestEvent evt,
|
GSocketEvent evt,
|
||||||
char *cdata);
|
char *cdata);
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
@@ -91,13 +91,12 @@ wxConnectionBase *wxTCPClient::MakeConnection (const wxString& host,
|
|||||||
const wxString& topic)
|
const wxString& topic)
|
||||||
{
|
{
|
||||||
wxIPV4address addr;
|
wxIPV4address addr;
|
||||||
wxSocketHandler *hsock = &wxSocketHandler::Master();
|
wxSocketClient *client = new wxSocketClient();
|
||||||
wxSocketClient *client = hsock->CreateClient();
|
|
||||||
wxSocketStream *stream = new wxSocketStream(*client);
|
wxSocketStream *stream = new wxSocketStream(*client);
|
||||||
wxDataInputStream data_is(*stream);
|
wxDataInputStream data_is(*stream);
|
||||||
wxDataOutputStream data_os(*stream);
|
wxDataOutputStream data_os(*stream);
|
||||||
|
|
||||||
client->SetNotify(wxSocketBase::REQ_READ | wxSocketBase::REQ_LOST);
|
client->SetNotify(GSOCK_INPUT_FLAG | GSOCK_LOST_FLAG);
|
||||||
addr.Service(server_name);
|
addr.Service(server_name);
|
||||||
addr.Hostname(host);
|
addr.Hostname(host);
|
||||||
|
|
||||||
@@ -156,15 +155,14 @@ wxTCPServer::wxTCPServer (void)
|
|||||||
bool wxTCPServer::Create(const wxString& server_name)
|
bool wxTCPServer::Create(const wxString& server_name)
|
||||||
{
|
{
|
||||||
wxIPV4address addr;
|
wxIPV4address addr;
|
||||||
wxSocketHandler *hsock = &wxSocketHandler::Master();
|
|
||||||
wxSocketServer *server;
|
wxSocketServer *server;
|
||||||
|
|
||||||
addr.Service(server_name);
|
addr.Service(server_name);
|
||||||
|
|
||||||
// Create a socket listening on specified port
|
// Create a socket listening on specified port
|
||||||
server = hsock->CreateServer(addr);
|
server = new wxSocketServer(addr);
|
||||||
server->Callback((wxSocketBase::wxSockCbk)Server_OnRequest);
|
server->Callback((wxSocketBase::wxSockCbk)Server_OnRequest);
|
||||||
server->SetNotify(wxSocketBase::REQ_ACCEPT);
|
server->SetNotify(GSOCK_CONNECTION_FLAG);
|
||||||
|
|
||||||
server->CallbackData((char *)this);
|
server->CallbackData((char *)this);
|
||||||
|
|
||||||
@@ -338,7 +336,7 @@ bool wxTCPConnection::Advise (const wxString& item,
|
|||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Client_OnRequest(wxSocketBase& sock, wxSocketBase::wxRequestEvent evt,
|
void Client_OnRequest(wxSocketBase& sock, GSocketEvent evt,
|
||||||
char *cdata)
|
char *cdata)
|
||||||
{
|
{
|
||||||
int msg = 0;
|
int msg = 0;
|
||||||
@@ -350,7 +348,7 @@ void Client_OnRequest(wxSocketBase& sock, wxSocketBase::wxRequestEvent evt,
|
|||||||
wxString item;
|
wxString item;
|
||||||
|
|
||||||
// The socket handler signals us that we lost the connection: destroy all.
|
// The socket handler signals us that we lost the connection: destroy all.
|
||||||
if (evt == wxSocketBase::EVT_LOST) {
|
if (evt == GSOCK_LOST) {
|
||||||
sock.Close();
|
sock.Close();
|
||||||
connection->OnDisconnect();
|
connection->OnDisconnect();
|
||||||
return;
|
return;
|
||||||
@@ -466,20 +464,20 @@ void Client_OnRequest(wxSocketBase& sock, wxSocketBase::wxRequestEvent evt,
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Server_OnRequest(wxSocketServer& server,
|
void Server_OnRequest(wxSocketServer& server,
|
||||||
wxSocketBase::wxRequestEvent evt, char *cdata)
|
GSocketEvent evt, char *cdata)
|
||||||
{
|
{
|
||||||
wxTCPServer *ipcserv = (wxTCPServer *)cdata;
|
wxTCPServer *ipcserv = (wxTCPServer *)cdata;
|
||||||
wxSocketStream *stream;
|
wxSocketStream *stream;
|
||||||
wxDataInputStream *codeci;
|
wxDataInputStream *codeci;
|
||||||
wxDataOutputStream *codeco;
|
wxDataOutputStream *codeco;
|
||||||
|
|
||||||
if (evt != wxSocketBase::EVT_ACCEPT)
|
if (evt != GSOCK_CONNECTION)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
/* Accept the connection, getting a new socket */
|
/* Accept the connection, getting a new socket */
|
||||||
wxSocketBase *sock = server.Accept();
|
wxSocketBase *sock = server.Accept();
|
||||||
sock->Notify(FALSE);
|
sock->Notify(FALSE);
|
||||||
sock->SetNotify(wxSocketBase::REQ_READ | wxSocketBase::REQ_LOST);
|
sock->SetNotify(GSOCK_INPUT_FLAG | GSOCK_LOST_FLAG);
|
||||||
|
|
||||||
stream = new wxSocketStream(*sock);
|
stream = new wxSocketStream(*sock);
|
||||||
codeci = new wxDataInputStream(*stream);
|
codeci = new wxDataInputStream(*stream);
|
||||||
|
@@ -42,15 +42,21 @@ wxSocketOutputStream::~wxSocketOutputStream()
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
wxOutputStream& wxSocketOutputStream::Write(const void *buffer, size_t size)
|
|
||||||
{
|
|
||||||
m_lastcount = m_o_socket->Write((const char *)buffer, size).LastCount();
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t wxSocketOutputStream::OnSysWrite(const void *buffer, size_t size)
|
size_t wxSocketOutputStream::OnSysWrite(const void *buffer, size_t size)
|
||||||
{
|
{
|
||||||
return m_o_socket->Write((const char *)buffer, size).LastCount();
|
size_t ret;
|
||||||
|
|
||||||
|
ret = m_o_socket->Write((const char *)buffer, size).LastCount();
|
||||||
|
switch (m_o_socket->LastError()) {
|
||||||
|
case GSOCK_NOERROR:
|
||||||
|
m_lasterror = wxStream_NOERROR;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
m_lasterror = wxStream_READ_ERR;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
@@ -66,15 +72,20 @@ wxSocketInputStream::~wxSocketInputStream()
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
wxInputStream& wxSocketInputStream::Read(void *buffer, size_t size)
|
|
||||||
{
|
|
||||||
m_lastcount = m_i_socket->Read((char *)buffer, size).LastCount();
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t wxSocketInputStream::OnSysRead(void *buffer, size_t size)
|
size_t wxSocketInputStream::OnSysRead(void *buffer, size_t size)
|
||||||
{
|
{
|
||||||
return m_i_socket->Read((char *)buffer, size).LastCount();
|
size_t ret;
|
||||||
|
|
||||||
|
ret = m_i_socket->Read((char *)buffer, size).LastCount();
|
||||||
|
switch (m_i_socket->LastError()) {
|
||||||
|
case GSOCK_NOERROR:
|
||||||
|
m_lasterror = wxStream_NOERROR;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
m_lasterror = wxStream_WRITE_ERR;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
File diff suppressed because it is too large
Load Diff
@@ -480,6 +480,9 @@ size_t wxInputStream::GetWBack(char *buf, size_t bsize)
|
|||||||
{
|
{
|
||||||
size_t s_toget = m_wbacksize-m_wbackcur;
|
size_t s_toget = m_wbacksize-m_wbackcur;
|
||||||
|
|
||||||
|
if (!m_wback)
|
||||||
|
return 0;
|
||||||
|
|
||||||
if (bsize < s_toget)
|
if (bsize < s_toget)
|
||||||
s_toget = bsize;
|
s_toget = bsize;
|
||||||
|
|
||||||
@@ -697,8 +700,8 @@ wxInputStream& wxBufferedInputStream::Read(void *buffer, size_t size)
|
|||||||
char *buf = (char *)buffer;
|
char *buf = (char *)buffer;
|
||||||
|
|
||||||
retsize = GetWBack(buf, size);
|
retsize = GetWBack(buf, size);
|
||||||
|
m_lastcount = retsize;
|
||||||
if (retsize == size) {
|
if (retsize == size) {
|
||||||
m_lastcount = size;
|
|
||||||
m_lasterror = wxStream_NOERROR;
|
m_lasterror = wxStream_NOERROR;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@@ -753,6 +756,7 @@ wxBufferedOutputStream::~wxBufferedOutputStream()
|
|||||||
|
|
||||||
wxOutputStream& wxBufferedOutputStream::Write(const void *buffer, size_t size)
|
wxOutputStream& wxBufferedOutputStream::Write(const void *buffer, size_t size)
|
||||||
{
|
{
|
||||||
|
m_lastcount = 0;
|
||||||
m_o_streambuf->Write(buffer, size);
|
m_o_streambuf->Write(buffer, size);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
@@ -148,7 +148,7 @@ wxTextCtrl& wxTextCtrlBase::operator<<(long i)
|
|||||||
return *TEXTCTRL(this);
|
return *TEXTCTRL(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
wxTextCtrl& wxTextCtrlBase::operator<<(const char c)
|
wxTextCtrl& wxTextCtrlBase::operator<<(const wxChar c)
|
||||||
{
|
{
|
||||||
return operator<<(wxString(c));
|
return operator<<(wxString(c));
|
||||||
}
|
}
|
||||||
|
@@ -208,7 +208,6 @@ bool wxURL::FetchProtocol()
|
|||||||
|
|
||||||
m_protoinfo = info;
|
m_protoinfo = info;
|
||||||
m_protocol = (wxProtocol *)m_protoinfo->m_cinfo->CreateObject();
|
m_protocol = (wxProtocol *)m_protoinfo->m_cinfo->CreateObject();
|
||||||
wxSocketHandler::Master().Register(m_protocol);
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
info = info->next;
|
info = info->next;
|
||||||
@@ -323,7 +322,7 @@ wxString wxURL::ConvertToValidURI(const wxString& uri)
|
|||||||
|
|
||||||
if (!isalpha(c) && c != _T('.') && c != _T('+') && c != _T('.') &&
|
if (!isalpha(c) && c != _T('.') && c != _T('+') && c != _T('.') &&
|
||||||
c != _T('/')) {
|
c != _T('/')) {
|
||||||
hexa_code.Printf(_T("%02X"), c);
|
hexa_code.Printf(_T("%%%02X"), c);
|
||||||
out_str += hexa_code;
|
out_str += hexa_code;
|
||||||
} else
|
} else
|
||||||
out_str += c;
|
out_str += c;
|
||||||
|
@@ -138,7 +138,10 @@ wxFileOutputStream::~wxFileOutputStream()
|
|||||||
size_t wxFileOutputStream::OnSysWrite(const void *buffer, size_t size)
|
size_t wxFileOutputStream::OnSysWrite(const void *buffer, size_t size)
|
||||||
{
|
{
|
||||||
size_t ret = m_file->Write(buffer, size);
|
size_t ret = m_file->Write(buffer, size);
|
||||||
m_lasterror = wxStream_EOF; // TODO
|
if (m_file->Error())
|
||||||
|
m_lasterror = wxStream_WRITE_ERR;
|
||||||
|
else
|
||||||
|
m_lasterror = wxStream_NOERROR;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -29,6 +29,8 @@ libwx_gtk_la_SOURCES = \
|
|||||||
win_gtk.c \
|
win_gtk.c \
|
||||||
extended.c \
|
extended.c \
|
||||||
parser.c \
|
parser.c \
|
||||||
|
gsocket.c \
|
||||||
|
gsockgtk.c \
|
||||||
\
|
\
|
||||||
cmndata.cpp \
|
cmndata.cpp \
|
||||||
config.cpp \
|
config.cpp \
|
||||||
@@ -80,7 +82,6 @@ libwx_gtk_la_SOURCES = \
|
|||||||
resource.cpp \
|
resource.cpp \
|
||||||
sckaddr.cpp \
|
sckaddr.cpp \
|
||||||
sckfile.cpp \
|
sckfile.cpp \
|
||||||
sckint.cpp \
|
|
||||||
sckipc.cpp \
|
sckipc.cpp \
|
||||||
sckstrm.cpp \
|
sckstrm.cpp \
|
||||||
serbase.cpp \
|
serbase.cpp \
|
||||||
|
100
src/gtk/gsockgtk.c
Normal file
100
src/gtk/gsockgtk.c
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <gdk/gdk.h>
|
||||||
|
#include <glib.h>
|
||||||
|
#include <wx/gsocket.h>
|
||||||
|
#include "../unix/gsockunx.h"
|
||||||
|
|
||||||
|
void _GSocket_GDK_Input(gpointer data, gint source, GdkInputCondition condition)
|
||||||
|
{
|
||||||
|
GSocket *socket = (GSocket *)data;
|
||||||
|
|
||||||
|
switch (condition) {
|
||||||
|
case GDK_INPUT_READ:
|
||||||
|
_GSocket_Detected_Read(socket);
|
||||||
|
break;
|
||||||
|
case GDK_INPUT_WRITE:
|
||||||
|
_GSocket_Detected_Write(socket);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void _GSocket_GUI_Init(GSocket *socket)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
gint *m_id;
|
||||||
|
|
||||||
|
socket->m_gui_dependent = (char *)malloc(sizeof(gint)*3);
|
||||||
|
m_id = (gint *)(socket->m_gui_dependent);
|
||||||
|
|
||||||
|
for (i=0;i<3;i++)
|
||||||
|
m_id[i] = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void _GSocket_GUI_Destroy(GSocket *socket)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
gint *m_id;
|
||||||
|
|
||||||
|
m_id = (gint *)(socket->m_gui_dependent);
|
||||||
|
|
||||||
|
for (i=0;i<3;i++)
|
||||||
|
if (m_id[i] == -1)
|
||||||
|
gdk_input_remove(m_id[i]);
|
||||||
|
|
||||||
|
free(socket->m_gui_dependent);
|
||||||
|
}
|
||||||
|
|
||||||
|
void _GSocket_Install_Fallback(GSocket *socket, GSocketEvent event)
|
||||||
|
{
|
||||||
|
GdkInputCondition flag;
|
||||||
|
int c;
|
||||||
|
gint *m_id;
|
||||||
|
|
||||||
|
m_id = (gint *)(socket->m_gui_dependent);
|
||||||
|
|
||||||
|
switch (event) {
|
||||||
|
case GSOCK_CONNECTION:
|
||||||
|
case GSOCK_LOST:
|
||||||
|
case GSOCK_INPUT: c = 0; flag = GDK_INPUT_READ; break;
|
||||||
|
case GSOCK_OUTPUT: c = 1;flag = GDK_INPUT_WRITE; break;
|
||||||
|
default: return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_id[c] != -1)
|
||||||
|
gdk_input_remove(m_id[c]);
|
||||||
|
|
||||||
|
m_id[c] = gdk_input_add(socket->m_fd, flag,
|
||||||
|
_GSocket_GDK_Input, (gpointer)socket);
|
||||||
|
}
|
||||||
|
|
||||||
|
void _GSocket_Uninstall_Fallback(GSocket *socket, GSocketEvent event)
|
||||||
|
{
|
||||||
|
int c;
|
||||||
|
gint *m_id;
|
||||||
|
|
||||||
|
m_id = (gint *)(socket->m_gui_dependent);
|
||||||
|
|
||||||
|
switch (event) {
|
||||||
|
case GSOCK_CONNECTION:
|
||||||
|
case GSOCK_LOST:
|
||||||
|
case GSOCK_INPUT: c = 0; break;
|
||||||
|
case GSOCK_OUTPUT: c = 1; break;
|
||||||
|
default: return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_id[c] != -1)
|
||||||
|
gdk_input_remove(m_id[c]);
|
||||||
|
|
||||||
|
m_id[c] = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned long GSocket_GetEventID(GSocket *socket)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void GSocket_DoEvent(unsigned long evt_id)
|
||||||
|
{
|
||||||
|
}
|
@@ -29,6 +29,8 @@ libwx_gtk_la_SOURCES = \
|
|||||||
win_gtk.c \
|
win_gtk.c \
|
||||||
extended.c \
|
extended.c \
|
||||||
parser.c \
|
parser.c \
|
||||||
|
gsocket.c \
|
||||||
|
gsockgtk.c \
|
||||||
\
|
\
|
||||||
cmndata.cpp \
|
cmndata.cpp \
|
||||||
config.cpp \
|
config.cpp \
|
||||||
@@ -80,7 +82,6 @@ libwx_gtk_la_SOURCES = \
|
|||||||
resource.cpp \
|
resource.cpp \
|
||||||
sckaddr.cpp \
|
sckaddr.cpp \
|
||||||
sckfile.cpp \
|
sckfile.cpp \
|
||||||
sckint.cpp \
|
|
||||||
sckipc.cpp \
|
sckipc.cpp \
|
||||||
sckstrm.cpp \
|
sckstrm.cpp \
|
||||||
serbase.cpp \
|
serbase.cpp \
|
||||||
|
100
src/gtk1/gsockgtk.c
Normal file
100
src/gtk1/gsockgtk.c
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <gdk/gdk.h>
|
||||||
|
#include <glib.h>
|
||||||
|
#include <wx/gsocket.h>
|
||||||
|
#include "../unix/gsockunx.h"
|
||||||
|
|
||||||
|
void _GSocket_GDK_Input(gpointer data, gint source, GdkInputCondition condition)
|
||||||
|
{
|
||||||
|
GSocket *socket = (GSocket *)data;
|
||||||
|
|
||||||
|
switch (condition) {
|
||||||
|
case GDK_INPUT_READ:
|
||||||
|
_GSocket_Detected_Read(socket);
|
||||||
|
break;
|
||||||
|
case GDK_INPUT_WRITE:
|
||||||
|
_GSocket_Detected_Write(socket);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void _GSocket_GUI_Init(GSocket *socket)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
gint *m_id;
|
||||||
|
|
||||||
|
socket->m_gui_dependent = (char *)malloc(sizeof(gint)*3);
|
||||||
|
m_id = (gint *)(socket->m_gui_dependent);
|
||||||
|
|
||||||
|
for (i=0;i<3;i++)
|
||||||
|
m_id[i] = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void _GSocket_GUI_Destroy(GSocket *socket)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
gint *m_id;
|
||||||
|
|
||||||
|
m_id = (gint *)(socket->m_gui_dependent);
|
||||||
|
|
||||||
|
for (i=0;i<3;i++)
|
||||||
|
if (m_id[i] == -1)
|
||||||
|
gdk_input_remove(m_id[i]);
|
||||||
|
|
||||||
|
free(socket->m_gui_dependent);
|
||||||
|
}
|
||||||
|
|
||||||
|
void _GSocket_Install_Fallback(GSocket *socket, GSocketEvent event)
|
||||||
|
{
|
||||||
|
GdkInputCondition flag;
|
||||||
|
int c;
|
||||||
|
gint *m_id;
|
||||||
|
|
||||||
|
m_id = (gint *)(socket->m_gui_dependent);
|
||||||
|
|
||||||
|
switch (event) {
|
||||||
|
case GSOCK_CONNECTION:
|
||||||
|
case GSOCK_LOST:
|
||||||
|
case GSOCK_INPUT: c = 0; flag = GDK_INPUT_READ; break;
|
||||||
|
case GSOCK_OUTPUT: c = 1;flag = GDK_INPUT_WRITE; break;
|
||||||
|
default: return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_id[c] != -1)
|
||||||
|
gdk_input_remove(m_id[c]);
|
||||||
|
|
||||||
|
m_id[c] = gdk_input_add(socket->m_fd, flag,
|
||||||
|
_GSocket_GDK_Input, (gpointer)socket);
|
||||||
|
}
|
||||||
|
|
||||||
|
void _GSocket_Uninstall_Fallback(GSocket *socket, GSocketEvent event)
|
||||||
|
{
|
||||||
|
int c;
|
||||||
|
gint *m_id;
|
||||||
|
|
||||||
|
m_id = (gint *)(socket->m_gui_dependent);
|
||||||
|
|
||||||
|
switch (event) {
|
||||||
|
case GSOCK_CONNECTION:
|
||||||
|
case GSOCK_LOST:
|
||||||
|
case GSOCK_INPUT: c = 0; break;
|
||||||
|
case GSOCK_OUTPUT: c = 1; break;
|
||||||
|
default: return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_id[c] != -1)
|
||||||
|
gdk_input_remove(m_id[c]);
|
||||||
|
|
||||||
|
m_id[c] = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned long GSocket_GetEventID(GSocket *socket)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void GSocket_DoEvent(unsigned long evt_id)
|
||||||
|
{
|
||||||
|
}
|
940
src/unix/gsocket.c
Normal file
940
src/unix/gsocket.c
Normal file
@@ -0,0 +1,940 @@
|
|||||||
|
/* -------------------------------------------------------------------------
|
||||||
|
* Project: GSocket (Generic Socket) for WX
|
||||||
|
* Name: gsocket.c
|
||||||
|
* Purpose: GSocket main Unix file
|
||||||
|
* CVSID: $Id$
|
||||||
|
* Log: $Log$
|
||||||
|
* Log: Revision 1.1 1999/07/22 17:51:54 GL
|
||||||
|
* Log: Added GSocket for Unix (only GTK for the moment)
|
||||||
|
* Log: Updated wxSocket to use GSocket API
|
||||||
|
* Log: Added a progress bar to client.cpp
|
||||||
|
* Log: Added CopyTo to wxMemoryOutputStream to copy the internal buffer to a specified buffer.
|
||||||
|
* Log: Various changes/fixes to the high-level protocols FTP/HTTP
|
||||||
|
* Log: Various Unicode fixes
|
||||||
|
* Log: Removed sckint.*
|
||||||
|
* Log:
|
||||||
|
* Log: Revision 1.2 1999/07/18 15:52:34 guilhem
|
||||||
|
* Log: * Copyright, etc.
|
||||||
|
* Log:
|
||||||
|
* -------------------------------------------------------------------------
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
#ifdef vms
|
||||||
|
#include <socket.h>
|
||||||
|
#else
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#endif
|
||||||
|
#include <sys/un.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/time.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <netdb.h>
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#ifdef sun
|
||||||
|
#include <sys/filio.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef sgi
|
||||||
|
#include <bstring.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <signal.h>
|
||||||
|
|
||||||
|
#include <wx/gsocket.h>
|
||||||
|
#include "gsockunx.h"
|
||||||
|
|
||||||
|
/* Constructors / Destructors */
|
||||||
|
|
||||||
|
GSocket *GSocket_new()
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
GSocket *socket;
|
||||||
|
|
||||||
|
socket = (GSocket *)malloc(sizeof(GSocket));
|
||||||
|
|
||||||
|
socket->m_fd = -1;
|
||||||
|
for (i=0;i<GSOCK_MAX_EVENT;i++) {
|
||||||
|
socket->m_fbacks[i] = NULL;
|
||||||
|
socket->m_iocalls[i] = FALSE;
|
||||||
|
}
|
||||||
|
socket->m_local = NULL;
|
||||||
|
socket->m_peer = NULL;
|
||||||
|
socket->m_error = GSOCK_NOERROR;
|
||||||
|
socket->m_server = FALSE;
|
||||||
|
socket->m_stream = TRUE;
|
||||||
|
socket->m_gui_dependent = NULL;
|
||||||
|
socket->m_blocking = FALSE;
|
||||||
|
|
||||||
|
_GSocket_GUI_Init(socket);
|
||||||
|
|
||||||
|
return socket;
|
||||||
|
}
|
||||||
|
|
||||||
|
void GSocket_destroy(GSocket *socket)
|
||||||
|
{
|
||||||
|
assert(socket != NULL);
|
||||||
|
|
||||||
|
_GSocket_GUI_Destroy(socket);
|
||||||
|
|
||||||
|
if (socket->m_fd != -1)
|
||||||
|
GSocket_Shutdown(socket);
|
||||||
|
|
||||||
|
if (socket->m_local)
|
||||||
|
GAddress_destroy(socket->m_local);
|
||||||
|
|
||||||
|
if (socket->m_peer)
|
||||||
|
GAddress_destroy(socket->m_peer);
|
||||||
|
|
||||||
|
free(socket);
|
||||||
|
}
|
||||||
|
|
||||||
|
void GSocket_Shutdown(GSocket *socket)
|
||||||
|
{
|
||||||
|
int evt;
|
||||||
|
|
||||||
|
assert(socket != NULL);
|
||||||
|
|
||||||
|
if (socket->m_fd != -1) {
|
||||||
|
shutdown(socket->m_fd, 2);
|
||||||
|
close(socket->m_fd);
|
||||||
|
socket->m_fd = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (evt=0;evt<GSOCK_MAX_EVENT;evt++)
|
||||||
|
_GSocket_Uninstall_Fallback(socket, evt);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Address handling */
|
||||||
|
|
||||||
|
GSocketError GSocket_SetLocal(GSocket *socket, GAddress *address)
|
||||||
|
{
|
||||||
|
if (socket == NULL || (socket->m_fd != -1 && !socket->m_server))
|
||||||
|
return GSOCK_INVSOCK;
|
||||||
|
|
||||||
|
if (address == NULL || address->m_family == GSOCK_NOFAMILY)
|
||||||
|
return GSOCK_INVADDR;
|
||||||
|
|
||||||
|
if (socket->m_local)
|
||||||
|
GAddress_destroy(socket->m_local);
|
||||||
|
|
||||||
|
socket->m_local = GAddress_copy(address);
|
||||||
|
|
||||||
|
return GSOCK_NOERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
GSocketError GSocket_SetPeer(GSocket *socket, GAddress *address)
|
||||||
|
{
|
||||||
|
if (socket == NULL)
|
||||||
|
return GSOCK_INVSOCK;
|
||||||
|
|
||||||
|
if (address == NULL || address->m_family == GSOCK_NOFAMILY) {
|
||||||
|
socket->m_error = GSOCK_INVADDR;
|
||||||
|
return GSOCK_INVADDR;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (socket->m_peer)
|
||||||
|
GAddress_destroy(socket->m_peer);
|
||||||
|
|
||||||
|
socket->m_peer = GAddress_copy(address);
|
||||||
|
|
||||||
|
return GSOCK_NOERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
GAddress *GSocket_GetLocal(GSocket *socket)
|
||||||
|
{
|
||||||
|
GAddress *address;
|
||||||
|
struct sockaddr addr;
|
||||||
|
socklen_t size;
|
||||||
|
|
||||||
|
assert(socket != NULL);
|
||||||
|
|
||||||
|
if (socket->m_local)
|
||||||
|
return GAddress_copy(socket->m_local);
|
||||||
|
|
||||||
|
if (socket->m_fd == -1) {
|
||||||
|
socket->m_error = GSOCK_INVSOCK;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
size = sizeof(addr);
|
||||||
|
|
||||||
|
if (getsockname(socket->m_fd, &addr, &size) < 0) {
|
||||||
|
socket->m_error = GSOCK_IOERR;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
address = GAddress_new();
|
||||||
|
_GAddress_translate_from(address, &addr, size);
|
||||||
|
|
||||||
|
return address;
|
||||||
|
}
|
||||||
|
|
||||||
|
GAddress *GSocket_GetPeer(GSocket *socket)
|
||||||
|
{
|
||||||
|
assert(socket != NULL);
|
||||||
|
|
||||||
|
if (socket->m_peer)
|
||||||
|
return GAddress_copy(socket->m_peer);
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Server specific parts */
|
||||||
|
|
||||||
|
/*
|
||||||
|
GSocket_SetServer() setup the socket as a server. It uses the "Local" field
|
||||||
|
of GSocket. "Local" must be set by GSocket_SetLocal() before
|
||||||
|
GSocket_SetServer() is called. GSOCK_INVSOCK if socket has been initialized.
|
||||||
|
In the other cases, it returns GSOCK_INVADDR.
|
||||||
|
*/
|
||||||
|
GSocketError GSocket_SetServer(GSocket *sck)
|
||||||
|
{
|
||||||
|
int type;
|
||||||
|
|
||||||
|
assert(sck != NULL);
|
||||||
|
|
||||||
|
if (sck->m_fd != -1) {
|
||||||
|
sck->m_error = GSOCK_INVSOCK;
|
||||||
|
return GSOCK_INVSOCK;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!sck->m_local) {
|
||||||
|
sck->m_error = GSOCK_INVADDR;
|
||||||
|
return GSOCK_INVADDR;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sck->m_stream)
|
||||||
|
type = SOCK_STREAM;
|
||||||
|
else
|
||||||
|
type = SOCK_DGRAM;
|
||||||
|
|
||||||
|
sck->m_fd = socket(sck->m_local->m_realfamily, type, 0);
|
||||||
|
|
||||||
|
if (sck->m_fd == -1) {
|
||||||
|
sck->m_error = GSOCK_IOERR;
|
||||||
|
return GSOCK_IOERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bind(sck->m_fd, sck->m_local->m_addr, sck->m_local->m_len) < 0) {
|
||||||
|
close(sck->m_fd);
|
||||||
|
sck->m_fd = -1;
|
||||||
|
sck->m_error = GSOCK_IOERR;
|
||||||
|
return GSOCK_IOERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (listen(sck->m_fd, 5) < 0) {
|
||||||
|
close(sck->m_fd);
|
||||||
|
sck->m_fd = -1;
|
||||||
|
sck->m_error = GSOCK_IOERR;
|
||||||
|
return GSOCK_IOERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
sck->m_server = TRUE;
|
||||||
|
|
||||||
|
return GSOCK_NOERROR;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
GSocket_WaitConnection() waits for an incoming client connection.
|
||||||
|
*/
|
||||||
|
GSocket *GSocket_WaitConnection(GSocket *socket)
|
||||||
|
{
|
||||||
|
GSocket *connection;
|
||||||
|
|
||||||
|
assert(socket != NULL);
|
||||||
|
|
||||||
|
if (socket->m_fd == -1 || !socket->m_server) {
|
||||||
|
socket->m_error = GSOCK_INVSOCK;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
_GSocket_Enable(socket, GSOCK_CONNECTION);
|
||||||
|
|
||||||
|
connection = GSocket_new();
|
||||||
|
|
||||||
|
connection->m_fd = accept(socket->m_fd, NULL, NULL);
|
||||||
|
if (connection->m_fd == -1) {
|
||||||
|
GSocket_destroy(connection);
|
||||||
|
socket->m_error = GSOCK_IOERR;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
connection->m_stream = TRUE;
|
||||||
|
connection->m_server = FALSE;
|
||||||
|
connection->m_oriented = TRUE;
|
||||||
|
|
||||||
|
return connection;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Non oriented connections */
|
||||||
|
|
||||||
|
GSocketError GSocket_SetNonOriented(GSocket *sck)
|
||||||
|
{
|
||||||
|
assert(sck != NULL);
|
||||||
|
|
||||||
|
if (sck->m_fd != -1) {
|
||||||
|
sck->m_error = GSOCK_INVSOCK;
|
||||||
|
return GSOCK_INVSOCK;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!sck->m_local) {
|
||||||
|
sck->m_error = GSOCK_INVADDR;
|
||||||
|
return GSOCK_INVADDR;
|
||||||
|
}
|
||||||
|
|
||||||
|
sck->m_stream = FALSE;
|
||||||
|
sck->m_server = FALSE;
|
||||||
|
sck->m_oriented = FALSE;
|
||||||
|
|
||||||
|
sck->m_fd = socket(sck->m_local->m_realfamily, SOCK_DGRAM, 0);
|
||||||
|
|
||||||
|
if (bind(sck->m_fd, sck->m_local->m_addr, sck->m_local->m_len) < 0) {
|
||||||
|
close(sck->m_fd);
|
||||||
|
sck->m_fd = -1;
|
||||||
|
sck->m_error = GSOCK_IOERR;
|
||||||
|
return GSOCK_IOERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
return GSOCK_NOERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Client specific parts */
|
||||||
|
|
||||||
|
/*
|
||||||
|
GSocket_Connect() establishes a client connection to a server using the "Peer"
|
||||||
|
field of GSocket. "Peer" must be set by GSocket_SetPeer() before
|
||||||
|
GSocket_Connect() is called. In the other case, it returns GSOCK_INVADDR.
|
||||||
|
*/
|
||||||
|
GSocketError GSocket_Connect(GSocket *sck, GSocketStream stream)
|
||||||
|
{
|
||||||
|
int type;
|
||||||
|
|
||||||
|
assert(sck != NULL);
|
||||||
|
|
||||||
|
if (sck->m_fd != -1) {
|
||||||
|
sck->m_error = GSOCK_INVSOCK;
|
||||||
|
return GSOCK_INVSOCK;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!sck->m_peer) {
|
||||||
|
sck->m_error = GSOCK_INVADDR;
|
||||||
|
return GSOCK_INVADDR;
|
||||||
|
}
|
||||||
|
|
||||||
|
sck->m_stream = (stream == GSOCK_STREAMED);
|
||||||
|
sck->m_oriented = TRUE;
|
||||||
|
|
||||||
|
if (sck->m_stream)
|
||||||
|
type = SOCK_STREAM;
|
||||||
|
else
|
||||||
|
type = SOCK_DGRAM;
|
||||||
|
|
||||||
|
sck->m_fd = socket(sck->m_peer->m_realfamily, type, 0);
|
||||||
|
|
||||||
|
if (sck->m_fd == -1) {
|
||||||
|
sck->m_error = GSOCK_IOERR;
|
||||||
|
return GSOCK_IOERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (connect(sck->m_fd, sck->m_peer->m_addr,
|
||||||
|
sck->m_peer->m_len) != 0) {
|
||||||
|
close(sck->m_fd);
|
||||||
|
sck->m_fd = -1;
|
||||||
|
sck->m_error = GSOCK_IOERR;
|
||||||
|
return GSOCK_IOERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
sck->m_server = FALSE;
|
||||||
|
|
||||||
|
return GSOCK_NOERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Generic IO */
|
||||||
|
|
||||||
|
/* Like recv(), send(), ... */
|
||||||
|
int GSocket_Read(GSocket *socket, char *buffer, int size)
|
||||||
|
{
|
||||||
|
assert(socket != NULL);
|
||||||
|
|
||||||
|
if (socket->m_fd == -1 || socket->m_server) {
|
||||||
|
socket->m_error = GSOCK_INVSOCK;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
_GSocket_Enable(socket, GSOCK_INPUT);
|
||||||
|
|
||||||
|
if (socket->m_oriented)
|
||||||
|
return _GSocket_Recv_Stream(socket, buffer, size);
|
||||||
|
else
|
||||||
|
return _GSocket_Recv_Dgram(socket, buffer, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
int GSocket_Write(GSocket *socket, const char *buffer,
|
||||||
|
int size)
|
||||||
|
{
|
||||||
|
assert(socket != NULL);
|
||||||
|
|
||||||
|
if (socket->m_fd == -1 || socket->m_server) {
|
||||||
|
socket->m_error = GSOCK_INVSOCK;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
_GSocket_Enable(socket, GSOCK_OUTPUT);
|
||||||
|
|
||||||
|
if (socket->m_oriented)
|
||||||
|
return _GSocket_Send_Stream(socket, buffer, size);
|
||||||
|
else
|
||||||
|
return _GSocket_Send_Dgram(socket, buffer, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool GSocket_DataAvailable(GSocket *socket)
|
||||||
|
{
|
||||||
|
fd_set read_set;
|
||||||
|
struct timeval tv;
|
||||||
|
|
||||||
|
assert(socket != NULL);
|
||||||
|
|
||||||
|
if (socket->m_fd == -1 || socket->m_server) {
|
||||||
|
socket->m_error = GSOCK_INVSOCK;
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
FD_ZERO(&read_set);
|
||||||
|
FD_SET(socket->m_fd, &read_set);
|
||||||
|
|
||||||
|
tv.tv_sec = 0;
|
||||||
|
tv.tv_usec = 0;
|
||||||
|
|
||||||
|
select(socket->m_fd+1, &read_set, NULL, NULL, &tv);
|
||||||
|
|
||||||
|
return FD_ISSET(socket->m_fd, &read_set);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Flags */
|
||||||
|
|
||||||
|
/*
|
||||||
|
GSocket_SetBlocking() puts the socket in non-blocking mode. This is useful
|
||||||
|
if we don't want to wait.
|
||||||
|
*/
|
||||||
|
void GSocket_SetBlocking(GSocket *socket, bool block)
|
||||||
|
{
|
||||||
|
assert(socket != NULL);
|
||||||
|
|
||||||
|
socket->m_blocking = block;
|
||||||
|
|
||||||
|
if (socket->m_fd != -1)
|
||||||
|
ioctl(socket->m_fd, FIONBIO, &block);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
GSocket_GetError() returns the last error occured on the socket stream.
|
||||||
|
*/
|
||||||
|
|
||||||
|
GSocketError GSocket_GetError(GSocket *socket)
|
||||||
|
{
|
||||||
|
assert(socket != NULL);
|
||||||
|
|
||||||
|
return socket->m_error;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Callbacks */
|
||||||
|
|
||||||
|
/*
|
||||||
|
Only one fallback is possible for each event (INPUT, OUTPUT, CONNECTION)
|
||||||
|
INPUT: The function is called when there is at least a byte in the
|
||||||
|
input buffer
|
||||||
|
OUTPUT: The function is called when the system is sure the next write call
|
||||||
|
will not block
|
||||||
|
CONNECTION: Two cases is possible:
|
||||||
|
Client socket -> the connection is established
|
||||||
|
Server socket -> a client request a connection
|
||||||
|
LOST: the connection is lost
|
||||||
|
|
||||||
|
SetFallback accepts a combination of these flags so a same callback can
|
||||||
|
receive different events.
|
||||||
|
|
||||||
|
An event is generated only once and its state is reseted when the relative
|
||||||
|
IO call is requested.
|
||||||
|
For example: INPUT -> GSocket_Read()
|
||||||
|
CONNECTION -> GSocket_Accept()
|
||||||
|
*/
|
||||||
|
void GSocket_SetFallback(GSocket *socket, GSocketEventFlags event,
|
||||||
|
GSocketFallback fallback, char *cdata)
|
||||||
|
{
|
||||||
|
int count;
|
||||||
|
|
||||||
|
assert (socket != NULL);
|
||||||
|
|
||||||
|
for (count=0;count<GSOCK_MAX_EVENT;count++) {
|
||||||
|
if ((event & (1 << count)) != 0) {
|
||||||
|
socket->m_fbacks[count] = fallback;
|
||||||
|
socket->m_data[count] = cdata;
|
||||||
|
|
||||||
|
_GSocket_Install_Fallback(socket, count);
|
||||||
|
_GSocket_Enable(socket, count);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
UnsetFallback will disables all fallbacks specified by "event".
|
||||||
|
NOTE: event may be a combination of flags
|
||||||
|
*/
|
||||||
|
void GSocket_UnsetFallback(GSocket *socket, GSocketEventFlags event)
|
||||||
|
{
|
||||||
|
int count = 0;
|
||||||
|
|
||||||
|
assert(socket != NULL);
|
||||||
|
|
||||||
|
for (count=0;count<GSOCK_MAX_EVENT;count++) {
|
||||||
|
if ((event & (1 << count)) != 0) {
|
||||||
|
_GSocket_Disable(socket, count);
|
||||||
|
socket->m_fbacks[count] = NULL;
|
||||||
|
_GSocket_Uninstall_Fallback(socket, count);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#define CALL_FALLBACK(socket, event) \
|
||||||
|
if (socket->m_iocalls[event] && \
|
||||||
|
socket->m_fbacks[event]) {\
|
||||||
|
_GSocket_Disable(socket, event); \
|
||||||
|
socket->m_fbacks[event](socket, event, \
|
||||||
|
socket->m_data[event]); \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define MASK_SIGNAL() \
|
||||||
|
{ \
|
||||||
|
void (*old_handler)(int); \
|
||||||
|
\
|
||||||
|
old_handler = signal(SIGPIPE, SIG_IGN);
|
||||||
|
|
||||||
|
#define UNMASK_SIGNAL() \
|
||||||
|
signal(SIGPIPE, old_handler); \
|
||||||
|
}
|
||||||
|
|
||||||
|
void _GSocket_Enable(GSocket *socket, GSocketEvent event)
|
||||||
|
{
|
||||||
|
socket->m_iocalls[event] = TRUE;
|
||||||
|
if (socket->m_fbacks[event])
|
||||||
|
_GSocket_Install_Fallback(socket, event);
|
||||||
|
}
|
||||||
|
|
||||||
|
void _GSocket_Disable(GSocket *socket, GSocketEvent event)
|
||||||
|
{
|
||||||
|
socket->m_iocalls[event] = FALSE;
|
||||||
|
if (socket->m_fbacks[event])
|
||||||
|
_GSocket_Uninstall_Fallback(socket, event);
|
||||||
|
}
|
||||||
|
|
||||||
|
int _GSocket_Recv_Stream(GSocket *socket, char *buffer, int size)
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
MASK_SIGNAL();
|
||||||
|
ret = recv(socket->m_fd, buffer, size, 0);
|
||||||
|
UNMASK_SIGNAL();
|
||||||
|
if (ret == -1) {
|
||||||
|
socket->m_error = GSOCK_IOERR;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int _GSocket_Recv_Dgram(GSocket *socket, char *buffer, int size)
|
||||||
|
{
|
||||||
|
struct sockaddr from;
|
||||||
|
int fromlen, ret;
|
||||||
|
|
||||||
|
fromlen = sizeof(from);
|
||||||
|
|
||||||
|
MASK_SIGNAL();
|
||||||
|
ret = recvfrom(socket->m_fd, buffer, size, 0, &from, &fromlen);
|
||||||
|
UNMASK_SIGNAL();
|
||||||
|
if (ret == -1) {
|
||||||
|
socket->m_error = GSOCK_IOERR;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!socket->m_peer)
|
||||||
|
socket->m_peer = GAddress_new();
|
||||||
|
_GAddress_translate_from(socket->m_peer, &from, fromlen);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int _GSocket_Send_Stream(GSocket *socket, const char *buffer, int size)
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
MASK_SIGNAL();
|
||||||
|
ret = send(socket->m_fd, buffer, size, 0);
|
||||||
|
UNMASK_SIGNAL();
|
||||||
|
if (ret == -1) {
|
||||||
|
socket->m_error = GSOCK_IOERR;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int _GSocket_Send_Dgram(GSocket *socket, const char *buffer, int size)
|
||||||
|
{
|
||||||
|
struct sockaddr *addr;
|
||||||
|
int len, ret;
|
||||||
|
|
||||||
|
if (!socket->m_peer) {
|
||||||
|
socket->m_error = GSOCK_INVADDR;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
_GAddress_translate_to(socket->m_peer, &addr, &len);
|
||||||
|
|
||||||
|
MASK_SIGNAL();
|
||||||
|
ret = sendto(socket->m_fd, buffer, size, 0, addr, len);
|
||||||
|
UNMASK_SIGNAL();
|
||||||
|
if (ret == -1) {
|
||||||
|
socket->m_error = GSOCK_IOERR;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(addr);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
void _GSocket_Detected_Read(GSocket *socket)
|
||||||
|
{
|
||||||
|
char c;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
if (socket->m_stream) {
|
||||||
|
ret = recv(socket->m_fd, &c, 1, MSG_PEEK);
|
||||||
|
|
||||||
|
if (ret < 0 && socket->m_server) {
|
||||||
|
CALL_FALLBACK(socket, GSOCK_CONNECTION);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ret > 0) {
|
||||||
|
CALL_FALLBACK(socket, GSOCK_INPUT);
|
||||||
|
} else {
|
||||||
|
CALL_FALLBACK(socket, GSOCK_LOST);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void _GSocket_Detected_Write(GSocket *socket)
|
||||||
|
{
|
||||||
|
CALL_FALLBACK(socket, GSOCK_OUTPUT);
|
||||||
|
}
|
||||||
|
|
||||||
|
#undef CALL_FALLBACK
|
||||||
|
#undef MASK_SIGNAL
|
||||||
|
#undef UNMASK_SIGNAL
|
||||||
|
|
||||||
|
/*
|
||||||
|
* -------------------------------------------------------------------------
|
||||||
|
* GAddress
|
||||||
|
* -------------------------------------------------------------------------
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define CHECK_ADDRESS(address, family, retval) \
|
||||||
|
{ \
|
||||||
|
if (address->m_family == GSOCK_NOFAMILY) \
|
||||||
|
_GAddress_Init_##family(address); \
|
||||||
|
if (address->m_family != GSOCK_##family) {\
|
||||||
|
address->m_error = GSOCK_INVADDR; \
|
||||||
|
return retval; \
|
||||||
|
} \
|
||||||
|
}
|
||||||
|
|
||||||
|
GAddress *GAddress_new()
|
||||||
|
{
|
||||||
|
GAddress *address;
|
||||||
|
|
||||||
|
address = (GAddress *)malloc(sizeof(GAddress));
|
||||||
|
|
||||||
|
address->m_family = GSOCK_NOFAMILY;
|
||||||
|
address->m_addr = NULL;
|
||||||
|
address->m_len = 0;
|
||||||
|
|
||||||
|
return address;
|
||||||
|
}
|
||||||
|
|
||||||
|
GAddress *GAddress_copy(GAddress *address)
|
||||||
|
{
|
||||||
|
GAddress *addr2;
|
||||||
|
|
||||||
|
assert(address != NULL);
|
||||||
|
|
||||||
|
addr2 = (GAddress *)malloc(sizeof(GAddress));
|
||||||
|
memcpy(addr2, address, sizeof(GAddress));
|
||||||
|
|
||||||
|
if (address->m_addr) {
|
||||||
|
addr2->m_addr = (struct sockaddr *)malloc(addr2->m_len);
|
||||||
|
memcpy(addr2->m_addr, address->m_addr, addr2->m_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
return addr2;
|
||||||
|
}
|
||||||
|
|
||||||
|
void GAddress_destroy(GAddress *address)
|
||||||
|
{
|
||||||
|
assert(address != NULL);
|
||||||
|
|
||||||
|
free(address);
|
||||||
|
}
|
||||||
|
|
||||||
|
void GAddress_SetFamily(GAddress *address, GAddressType type)
|
||||||
|
{
|
||||||
|
assert(address != NULL);
|
||||||
|
|
||||||
|
address->m_family = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
GAddressType GAddress_GetFamily(GAddress *address)
|
||||||
|
{
|
||||||
|
assert(address != NULL);
|
||||||
|
|
||||||
|
return address->m_family;
|
||||||
|
}
|
||||||
|
|
||||||
|
void _GAddress_translate_from(GAddress *address, struct sockaddr *addr, int len){
|
||||||
|
address->m_realfamily = addr->sa_family;
|
||||||
|
switch (addr->sa_family) {
|
||||||
|
case AF_INET:
|
||||||
|
address->m_family = GSOCK_INET;
|
||||||
|
break;
|
||||||
|
case AF_UNIX:
|
||||||
|
address->m_family = GSOCK_UNIX;
|
||||||
|
break;
|
||||||
|
case AF_INET6:
|
||||||
|
address->m_family = GSOCK_INET6;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
/* TODO error */
|
||||||
|
}
|
||||||
|
|
||||||
|
if (address->m_addr)
|
||||||
|
free(address->m_addr);
|
||||||
|
|
||||||
|
address->m_len = len;
|
||||||
|
address->m_addr = (struct sockaddr *)malloc(len);
|
||||||
|
memcpy(address->m_addr, addr, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
void _GAddress_translate_to(GAddress *address,
|
||||||
|
struct sockaddr **addr, int *len)
|
||||||
|
{
|
||||||
|
if (!address->m_addr) {
|
||||||
|
/* TODO error */
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
*len = address->m_len;
|
||||||
|
*addr = (struct sockaddr *)malloc(address->m_len);
|
||||||
|
memcpy(*addr, address->m_addr, address->m_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* -------------------------------------------------------------------------
|
||||||
|
* Internet address family
|
||||||
|
* -------------------------------------------------------------------------
|
||||||
|
*/
|
||||||
|
|
||||||
|
void _GAddress_Init_INET(GAddress *address)
|
||||||
|
{
|
||||||
|
address->m_len = sizeof(struct sockaddr_in);
|
||||||
|
address->m_addr = (struct sockaddr *)malloc(address->m_len);
|
||||||
|
address->m_family = GSOCK_INET;
|
||||||
|
address->m_realfamily = PF_INET;
|
||||||
|
((struct sockaddr_in *)address->m_addr)->sin_family = AF_INET;
|
||||||
|
((struct sockaddr_in *)address->m_addr)->sin_addr.s_addr = INADDR_ANY;
|
||||||
|
}
|
||||||
|
|
||||||
|
GSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname)
|
||||||
|
{
|
||||||
|
struct hostent *he;
|
||||||
|
struct in_addr *addr;
|
||||||
|
|
||||||
|
assert(address != NULL);
|
||||||
|
|
||||||
|
CHECK_ADDRESS(address, INET, GSOCK_INVADDR);
|
||||||
|
|
||||||
|
addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr);
|
||||||
|
|
||||||
|
/* only name for the moment */
|
||||||
|
if (inet_aton(hostname, addr) == 0) {
|
||||||
|
struct in_addr *array_addr;
|
||||||
|
|
||||||
|
if ((he = gethostbyname(hostname)) == NULL) {
|
||||||
|
address->m_error = GSOCK_NOHOST;
|
||||||
|
return GSOCK_NOHOST;
|
||||||
|
}
|
||||||
|
array_addr = (struct in_addr *) *(he->h_addr_list);
|
||||||
|
addr->s_addr = array_addr[0].s_addr;
|
||||||
|
}
|
||||||
|
return GSOCK_NOERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
GSocketError GAddress_INET_SetHostAddress(GAddress *address,
|
||||||
|
unsigned long hostaddr)
|
||||||
|
{
|
||||||
|
struct in_addr *addr;
|
||||||
|
|
||||||
|
assert(address != NULL);
|
||||||
|
|
||||||
|
CHECK_ADDRESS(address, INET, GSOCK_INVADDR);
|
||||||
|
|
||||||
|
addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr);
|
||||||
|
addr->s_addr = hostaddr;
|
||||||
|
|
||||||
|
return GSOCK_NOERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
GSocketError GAddress_INET_SetPortName(GAddress *address, const char *port)
|
||||||
|
{
|
||||||
|
struct servent *se;
|
||||||
|
struct sockaddr_in *addr;
|
||||||
|
|
||||||
|
assert(address != NULL);
|
||||||
|
CHECK_ADDRESS(address, INET, GSOCK_INVADDR);
|
||||||
|
|
||||||
|
if (!port) {
|
||||||
|
address->m_error = GSOCK_INVPORT;
|
||||||
|
return GSOCK_INVOP;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* TODO: TCP or UDP */
|
||||||
|
se = getservbyname(port, "tcp");
|
||||||
|
if (!se) {
|
||||||
|
if (isdigit(port[0])) {
|
||||||
|
int port_int;
|
||||||
|
|
||||||
|
port_int = atoi(port);
|
||||||
|
addr = (struct sockaddr_in *)address->m_addr;
|
||||||
|
addr->sin_port = htons(port_int);
|
||||||
|
return GSOCK_NOERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
address->m_error = GSOCK_INVPORT;
|
||||||
|
return GSOCK_INVPORT;
|
||||||
|
}
|
||||||
|
|
||||||
|
addr = (struct sockaddr_in *)address->m_addr;
|
||||||
|
addr->sin_port = se->s_port;
|
||||||
|
|
||||||
|
return GSOCK_NOERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
GSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port)
|
||||||
|
{
|
||||||
|
struct sockaddr_in *addr;
|
||||||
|
|
||||||
|
assert(address != NULL);
|
||||||
|
CHECK_ADDRESS(address, INET, GSOCK_INVADDR);
|
||||||
|
|
||||||
|
addr = (struct sockaddr_in *)address->m_addr;
|
||||||
|
addr->sin_port = htons(port);
|
||||||
|
|
||||||
|
return GSOCK_NOERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
GSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname, size_t sbuf)
|
||||||
|
{
|
||||||
|
struct hostent *he;
|
||||||
|
char *addr_buf;
|
||||||
|
struct sockaddr_in *addr;
|
||||||
|
|
||||||
|
assert(address != NULL);
|
||||||
|
CHECK_ADDRESS(address, INET, GSOCK_INVADDR);
|
||||||
|
|
||||||
|
addr = (struct sockaddr_in *)address->m_addr;
|
||||||
|
addr_buf = (char *)&(addr->sin_addr);
|
||||||
|
|
||||||
|
he = gethostbyaddr(addr_buf, sizeof(addr->sin_addr), AF_INET);
|
||||||
|
if (he == NULL) {
|
||||||
|
address->m_error = GSOCK_NOHOST;
|
||||||
|
return GSOCK_NOHOST;
|
||||||
|
}
|
||||||
|
|
||||||
|
strncpy(hostname, he->h_name, sbuf);
|
||||||
|
|
||||||
|
return GSOCK_NOERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned long GAddress_INET_GetHostAddress(GAddress *address)
|
||||||
|
{
|
||||||
|
struct sockaddr_in *addr;
|
||||||
|
|
||||||
|
assert(address != NULL);
|
||||||
|
CHECK_ADDRESS(address, INET, 0);
|
||||||
|
|
||||||
|
addr = (struct sockaddr_in *)address->m_addr;
|
||||||
|
|
||||||
|
return addr->sin_addr.s_addr;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned short GAddress_INET_GetPort(GAddress *address)
|
||||||
|
{
|
||||||
|
struct sockaddr_in *addr;
|
||||||
|
|
||||||
|
assert(address != NULL);
|
||||||
|
CHECK_ADDRESS(address, INET, 0);
|
||||||
|
|
||||||
|
addr = (struct sockaddr_in *)address->m_addr;
|
||||||
|
return ntohs(addr->sin_port);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* -------------------------------------------------------------------------
|
||||||
|
* Unix address family
|
||||||
|
* -------------------------------------------------------------------------
|
||||||
|
*/
|
||||||
|
|
||||||
|
void _GAddress_Init_UNIX(GAddress *address)
|
||||||
|
{
|
||||||
|
address->m_len = sizeof(struct sockaddr_un);
|
||||||
|
address->m_addr = (struct sockaddr *)malloc(address->m_len);
|
||||||
|
address->m_family = GSOCK_UNIX;
|
||||||
|
address->m_realfamily = PF_UNIX;
|
||||||
|
((struct sockaddr_un *)address->m_addr)->sun_family = AF_UNIX;
|
||||||
|
((struct sockaddr_un *)address->m_addr)->sun_path[0] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
GSocketError GAddress_UNIX_SetPath(GAddress *address, const char *path)
|
||||||
|
{
|
||||||
|
struct sockaddr_un *addr;
|
||||||
|
|
||||||
|
assert(address != NULL);
|
||||||
|
|
||||||
|
CHECK_ADDRESS(address, UNIX, GSOCK_INVADDR);
|
||||||
|
|
||||||
|
addr = ((struct sockaddr_un *)address->m_addr);
|
||||||
|
memcpy(addr->sun_path, path, strlen(path));
|
||||||
|
|
||||||
|
return GSOCK_NOERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
GSocketError GAddress_UNIX_GetPath(GAddress *address, char *path, size_t sbuf)
|
||||||
|
{
|
||||||
|
struct sockaddr_un *addr;
|
||||||
|
|
||||||
|
assert(address != NULL);
|
||||||
|
CHECK_ADDRESS(address, UNIX, GSOCK_INVADDR);
|
||||||
|
|
||||||
|
addr = (struct sockaddr_un *)address->m_addr;
|
||||||
|
|
||||||
|
strncpy(path, addr->sun_path, sbuf);
|
||||||
|
|
||||||
|
return GSOCK_NOERROR;
|
||||||
|
}
|
57
src/unix/gsockunx.h
Normal file
57
src/unix/gsockunx.h
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
#ifndef __GSOCK_UNX_H
|
||||||
|
#define __GSOCK_UNX_H
|
||||||
|
|
||||||
|
#include <wx/gsocket.h>
|
||||||
|
|
||||||
|
/* Definition of GSocket */
|
||||||
|
struct _GSocket {
|
||||||
|
int m_fd;
|
||||||
|
GAddress *m_local, *m_peer;
|
||||||
|
GSocketError m_error;
|
||||||
|
|
||||||
|
bool m_blocking, m_server, m_stream, m_oriented;
|
||||||
|
|
||||||
|
/* Fallbacks */
|
||||||
|
GSocketFallback m_fbacks[GSOCK_MAX_EVENT];
|
||||||
|
char *m_data[GSOCK_MAX_EVENT];
|
||||||
|
|
||||||
|
/* IO calls associated */
|
||||||
|
bool m_iocalls[GSOCK_MAX_EVENT];
|
||||||
|
|
||||||
|
char *m_gui_dependent;
|
||||||
|
};
|
||||||
|
|
||||||
|
/* Definition of GAddress */
|
||||||
|
struct _GAddress {
|
||||||
|
struct sockaddr *m_addr;
|
||||||
|
size_t m_len;
|
||||||
|
|
||||||
|
GAddressType m_family;
|
||||||
|
int m_realfamily;
|
||||||
|
|
||||||
|
GSocketError m_error;
|
||||||
|
};
|
||||||
|
|
||||||
|
void _GSocket_Enable(GSocket *socket, GSocketEvent event);
|
||||||
|
void _GSocket_Disable(GSocket *socket, GSocketEvent event);
|
||||||
|
int _GSocket_Recv_Stream(GSocket *socket, char *buffer, int size);
|
||||||
|
int _GSocket_Recv_Dgram(GSocket *socket, char *buffer, int size);
|
||||||
|
int _GSocket_Send_Stream(GSocket *socket, const char *buffer, int size);
|
||||||
|
int _GSocket_Send_Dgram(GSocket *socket, const char *buffer, int size);
|
||||||
|
void _GSocket_Install_Fallback(GSocket *socket, GSocketEvent count);
|
||||||
|
void _GSocket_Uninstall_Fallback(GSocket *socket, GSocketEvent count);
|
||||||
|
void _GSocket_Detected_Read(GSocket *socket);
|
||||||
|
void _GSocket_Detected_Write(GSocket *socket);
|
||||||
|
void _GSocket_GUI_Init(GSocket *socket);
|
||||||
|
void _GSocket_GUI_Destroy(GSocket *socket);
|
||||||
|
void _GAddress_translate_from(GAddress *address,
|
||||||
|
struct sockaddr *addr, int len);
|
||||||
|
void _GAddress_translate_to(GAddress *address,
|
||||||
|
struct sockaddr **addr, int *len);
|
||||||
|
|
||||||
|
/* Internet address family */
|
||||||
|
void _GAddress_Init_INET(GAddress *address);
|
||||||
|
/* Local address family */
|
||||||
|
void _GAddress_Init_UNIX(GAddress *address);
|
||||||
|
|
||||||
|
#endif
|
Reference in New Issue
Block a user