📄 socket.h
字号:
/*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************//** * Copyright (c) 2000 * * A C++ implementation of socket I/O for the THREADS library * */#ifndef THREAD_SOCKET#define THREAD_SOCKET#include <thread.h>#include <string>extern "C" {# include <stdlib.h># include <stdio.h># include <stdarg.h># include <sys/socket.h># include <netinet/in.h>};namespace cpp_threads { class SocketAddress; class Socket; /** * Message header in C++ threads. * * This class provides means for messages delivery, reception * through sockets. It provides a C++ interface to the C structure * msghdr, and associates with it methods, to handle the structure. * * @author Orn E. Hansen <oe.hansen@gamma.telenordia.se> * @short Message headers under threads. */ class MessageHeader { public: enum Flags { /** This flag requests receipt of out-of-bound data, that would normally not be received in the data stream. */ oob = MSG_OOB, /** This flag causes the receive operation to return data from the beginning of the queue without removing the data from the queue. */ peek = MSG_PEEK, /** Bypass routing, and use a direct interface described by the destination address. */ dontroute = MSG_DONTROUTE, /** This name is used on solaris, isntead of the above. */ tryhard = MSG_DONTROUTE, /** Control data lost before delivery. */ ctrunc = MSG_CTRUNC, /** Supply or ask second address. */ proxy = MSG_PROXY, trunc = MSG_TRUNC, /** Enables non-blocking operation; if the operation would block, EAGAIN is returned. */ dontwait = MSG_DONTWAIT, /** End of record. */ eor = MSG_EOR, /** This flag requests that the operation blocks, until the full request is satified. However, the call may still return less data than requested if a signal is caught, an error or disconnect occurs, or the next data to be received is of a different type than that returned. */ waitall = MSG_WAITALL, fin = MSG_FIN, syn = MSG_SYN,#ifdef MSG_CONFIRM /** Confirm path validity. */ confirm = MSG_CONFIRM,#endif rst = MSG_RST, /** Receive packet from the error queue */ errqueue = MSG_ERRQUEUE, /** This flag turns off raising of SIGPIPE on stream sockets when the other end disappears. */ nosignal = MSG_NOSIGNAL, /** Sender will send more. */#ifdef MSG_MORE more = MSG_MORE#endif }; protected: struct msghdr _Header; public: MessageHeader(); ~MessageHeader(); void setName(SocketAddress*); void setControl(void *,int); void setFlags(int); int getFlags(); MessageHeader& operator |= (Flags); MessageHeader& operator &= (Flags); operator struct msghdr& (); operator struct msghdr* (); }; /** * This class functions as a common denominator for socket addresses, * both for input and output purposes. Through use of this class, * the burden is taken away to have to associate each kind of socket * with different socket address structures, and instead hold one * such class, which keeps track of the associations that are required * for each type of socket connection. * * @author Orn E. Hansen <oe.hansen@gamma.telenordia.se> * @short Socket addresses under threads. */ class SocketAddress { public: /** * These are the address families, defined in linux. Most of these * aren't implemented, but are available here for reference. The * address families that are implemented are commented. */ enum t_afamily { unspec = AF_UNSPEC, /** Local space, usually on the file system. */ local = AF_LOCAL, /** Same as local, but more usual in unix systems. */ af_unix = AF_UNIX, file = AF_FILE, /** The internet, 4 byte IP addresses. */ inet = AF_INET, ax25 = AF_AX25, ipx = AF_IPX, appletalk = AF_APPLETALK, netrom = AF_NETROM, bridge = AF_BRIDGE, atmpvc = AF_ATMPVC, x25 = AF_X25, /** The internet, 6 byte IP addresses. */ inet6 = AF_INET6, rose = AF_ROSE, DECnet = AF_DECnet, netbeui = AF_NETBEUI, security = AF_SECURITY, key = AF_KEY, netlink = AF_NETLINK, route = AF_ROUTE, packet = AF_PACKET, ash = AF_ASH, econet = AF_ECONET, atmsvc = AF_ATMSVC, sna = AF_SNA, irda = AF_IRDA, max = AF_MAX }; private: struct sockaddr* _Sin; void *host_address(const char *,t_afamily); void initFamily(t_afamily); void assign_Sin(struct sockaddr *); public: /** * Obtain the socket information, from an already opened * socket descriptor. * * @param sd Socket descriptor. */ SocketAddress(int); /** * Create a new socket address, based on a specific family * of sockets. @see t_afamily. * * @param family The socket family, to associate with. */ SocketAddress(t_afamily); /** * Create a copy of another socket address, but make this one * a new one. * * @param sa Reference to socket address. */ SocketAddress(SocketAddress&); ~SocketAddress(); /** * Obtain information about host and port, to associate with * this socket address. * * @param host The hostname to resolv. * @param port The port to associate with. */ void setPort(const char *,int); /** * Obtain information about a specific port, on the local * machine, that the socket address should use. * * @param port The port to associate with. */ void setPort(int); /** * When using this in C routines, it is necessary to know the * length of the C structure that must be passed. * * @return Length of the structure behind the class. */ uint length(); /** * Each socket address is associated with an address family, * @see t_afamily. * * @return The address family of this socket address. */ t_afamily family(); /** * This operator is used to facilitate the use of this class * directly as a parameter to C routines that use socket addresses * in their calling conventions. The class can be used in exchange * with the C sockaddr structure, through this operator. * * @return The equivalent sockaddr structure for this class. */ operator struct sockaddr*(); SocketAddress& operator =(SocketAddress&); }; /** * Sockets * * This class is a unified means of accessing sockets within the * threads library. * * The class maintains a lock mechanism, to ensure that only * process writes through it at any given time, and the same * for read operations. Making read/write operations on a socket * atomic within a multi threaded application. * * Use of this socket class, is directed at simplifying the * creation and maintenance of sockets inside an application, as * all data, such as port, peer and socket addresses are held * within and maintained within this class. * * @author Orn E. Hansen <oe.hansen@gamma.telenordia.se> * @short Sockets for the C++ Threads. */ class Socket { public: /** * These are the socket options, as defined in the socket * header. Here put into an enum type for simpler * access within c++. */ enum t_options { /** Toggle recording of debugging information in the underlying * protocol modules. */ s_debug = SO_DEBUG, /** Control wether the address for this socket, can be reused by * another. */ s_reuseaddr = SO_REUSEADDR, /** Wether keep alive messages should periodically be sent over * the connection */ s_keepalive = SO_KEEPALIVE, /** Wether outgoing messages bypass the normal message routing * facilities */ s_dontroute = SO_DONTROUTE, /** What happens when a socket with untransmitted messages is * closed. This is kept here for compatibility with C only, * and cannot be used as a parameter, as this should be set * with a specific method of the socket class. */ s_linger = SO_LINGER, /** Wether datagrams may be broadcasted with the socket */ s_broadcast = SO_BROADCAST, /** Specifies if out of bound data is received in the normal * message queue, and can be read with read */ s_oobinline = SO_OOBINLINE, /** Get or set the size of the output buffer. */ s_sndbuf = SO_SNDBUF,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -