diff -r 7f18229bae34 -r 577875bb5a65 src/node/socket.h --- a/src/node/socket.h Wed Aug 01 17:24:55 2007 +0200 +++ b/src/node/socket.h Wed Aug 01 18:48:24 2007 +0200 @@ -1,22 +1,24 @@ -/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ -// -// Copyright (c) 2006 Georgia Tech Research Corporation -// -// This program is free software; you can redistribute it and/or modify -// it under the terms of the GNU General Public License version 2 as -// published by the Free Software Foundation; -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -// -// Author: George F. Riley -// +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2006 Georgia Tech Research Corporation + * 2007 INRIA + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Authors: George F. Riley + * Mathieu Lacage + */ #ifndef __SOCKET_H__ #define __SOCKET_H__ @@ -30,6 +32,7 @@ namespace ns3 { class Node; +class Packet; /** * \brief Define a Socket API based on the BSD Socket API. @@ -70,6 +73,60 @@ */ virtual Ptr GetNode (void) const = 0; + /** + * \param closeCompleted Callback invoked when the close operation is + * completed. + */ + void SetCloseCallback (Callback > closeCompleted); + + /** + * \param connectionSucceeded this callback is invoked when the connection request + * initiated by the user is successfully completed. The callback is passed + * back a pointer to the same socket object. + * \param connectionFailed this callback is invoked when the connection request + * initiated by the user is unsuccessfully completed. The callback is passed + * back a pointer to the same socket object. + * \param halfClose XXX When exactly is this callback invoked ? If it invoked when the + * other side closes the connection ? Or when I call Close ? + */ + void SetConnectCallback (Callback > connectionSucceeded, + Callback > connectionFailed, + Callback > halfClose); + /** + * \brief Accept connection requests from remote hosts + * \param connectionRequest Callback for connection request from peer. + * This user callback is passed a pointer to this socket, the + * ip address and the port number of the connection originator. + * This callback must return true to accept the incoming connection, + * false otherwise. If the connection is accepted, the + * "newConnectionCreated" callback will be invoked later to give access + * to the user to the socket created to match this new connection. If the + * user does not explicitely specify this callback, all incoming + * connections will be refused. + * \param newConnectionCreated Callback for new connection: when a new + * is accepted, it is created and the corresponding socket is passed + * back to the user through this callback. This user callback is passed + * a pointer to the new socket, and the ip address and port number + * of the connection originator. + * \param closeRequested Callback for connection close request from peer. + * XXX: when is this callback invoked ? + */ + void SetAcceptCallback (Callback, const Address &> connectionRequest, + Callback, const Address&> newConnectionCreated, + Callback > closeRequested); + void SetSendCallback (Callback, uint32_t> dataSent); + /** + * \brief Receive data + * \param receivedData Invoked whenever new data is received. + * + * If you wish to transport only dummy packets, this method is not a very + * efficient way to receive these dummy packets: it will trigger a memory + * allocation to hold the dummy memory into a buffer which can be passed + * to the user. Instead, consider using the RecvDummy method. + */ + void SetRecvCallback (Callback, const uint8_t*, uint32_t,const Address&> receivedData, + Callback, uint32_t,const Address&> receivedDummyData); + /** * \param address the address to try to allocate * \returns 0 on success, -1 on failure. @@ -85,16 +142,13 @@ */ virtual int Bind () = 0; - /** * \brief Close a socket. - * \param closeCompleted Callback invoked when the close operation is - * completed. * * After the Close call, the socket is no longer valid, and cannot * safely be used for subsequent operations. */ - int Close(Callback > closeCompleted = MakeCallback (&Socket::DummyCallbackVoidSocket)); + virtual int Close(void) = 0; /** * \returns zero on success, -1 on failure. @@ -115,46 +169,10 @@ /** * \brief Initiate a connection to a remote host * \param address Address of remote. - * \param connectionSucceeded this callback is invoked when the connection request - * initiated by the user is successfully completed. The callback is passed - * back a pointer to the same socket object. - * \param connectionFailed this callback is invoked when the connection request - * initiated by the user is unsuccessfully completed. The callback is passed - * back a pointer to the same socket object. - * \param halfClose XXX When exactly is this callback invoked ? If it invoked when the - * other side closes the connection ? Or when I call Close ? */ - int Connect(const Address &address, - Callback > connectionSucceeded = MakeCallback(&Socket::DummyCallbackVoidSocket), - Callback > connectionFailed = MakeCallback(&Socket::DummyCallbackVoidSocket), - Callback > halfClose = MakeCallback(&Socket::DummyCallbackVoidSocket)); + virtual int Connect(const Address &address) = 0; /** - * \brief Accept connection requests from remote hosts - * \param connectionRequest Callback for connection request from peer. - * This user callback is passed a pointer to this socket, the - * ip address and the port number of the connection originator. - * This callback must return true to accept the incoming connection, - * false otherwise. If the connection is accepted, the - * "newConnectionCreated" callback will be invoked later to give access - * to the user to the socket created to match this new connection. If the - * user does not explicitely specify this callback, all incoming - * connections will be refused. - * \param newConnectionCreated Callback for new connection: when a new - * is accepted, it is created and the corresponding socket is passed - * back to the user through this callback. This user callback is passed - * a pointer to the new socket, and the ip address and port number - * of the connection originator. - * \param closeRequested Callback for connection close request from peer. - * XXX: when is this callback invoked ? - */ - int Accept(Callback, const Address &> connectionRequest = - MakeCallback(&Socket::RefuseAllConnections), - Callback, const Address&> newConnectionCreated = - MakeCallback (&Socket::DummyCallbackVoidSocketAddress), - Callback > closeRequested = MakeCallback (&Socket::DummyCallbackVoidSocket)); - - /** * \brief Send data (or dummy data) to the remote host * \param buffer Data to send (nil if dummy data). * \param size Number of bytes to send. @@ -162,9 +180,7 @@ * \returns -1 in case of error or the number of bytes copied in the * internal buffer and accepted for transmission. */ - int Send (const uint8_t* buffer, - uint32_t size, - Callback, uint32_t> dataSent = MakeCallback (&Socket::DummyCallbackVoidSocketUi32)); + virtual int Send (const uint8_t* buffer, uint32_t size) = 0; /** * \brief Send data to a specified peer. @@ -175,60 +191,29 @@ * \returns -1 in case of error or the number of bytes copied in the * internal buffer and accepted for transmission. */ - int SendTo(const Address &address, - const uint8_t *buffer, - uint32_t size, - Callback, uint32_t> dataSent = MakeCallback (&Socket::DummyCallbackVoidSocketUi32)); - - /** - * \brief Receive data - * \param receivedData Invoked whenever new data is received. - * - * If you wish to transport only dummy packets, this method is not a very - * efficient way to receive these dummy packets: it will trigger a memory - * allocation to hold the dummy memory into a buffer which can be passed - * to the user. Instead, consider using the RecvDummy method. - */ - void Recv(Callback, const uint8_t*, uint32_t,const Address&> receivedData = - MakeCallback (&Socket::DummyCallbackVoidSocketBufferUi32Address)); - - /** - * \brief Receive data - * \param receivedData Invoked whenever new data is received. - * - * This method is included because it is vastly more efficient than the - * Recv method when you use dummy payload. - */ - void RecvDummy(Callback, uint32_t,const Address&> receivedData = - MakeCallback (&Socket::DummyCallbackVoidSocketUi32Address)); + virtual int SendTo(const Address &address,const uint8_t *buffer, uint32_t size) = 0; -private: - virtual int DoClose(Callback > closeCompleted) = 0; - virtual int DoConnect(const Address & address, - Callback > connectionSucceeded, - Callback > connectionFailed, - Callback > halfClose) = 0; - virtual int DoAccept(Callback, const Address&> connectionRequest, - Callback, const Address&> newConnectionCreated, - Callback > closeRequested) = 0; - virtual int DoSend (const uint8_t* buffer, - uint32_t size, - Callback, uint32_t> dataSent) = 0; - virtual int DoSendTo(const Address &address, - const uint8_t *buffer, - uint32_t size, - Callback, uint32_t> dataSent) = 0; - virtual void DoRecv(Callback, const uint8_t*, uint32_t,const Address&> receive) = 0; - virtual void DoRecvDummy(Callback, uint32_t,const Address&>) = 0; +protected: + void NotifyCloseCompleted (void); + void NotifyConnectionSucceeded (void); + void NotifyConnectionFailed (void); + void NotifyHalfClose (void); + bool NotifyConnectionRequest (const Address &from); + void NotifyNewConnectionCreated (Ptr socket, const Address &from); + void NotifyCloseRequested (void); + void NotifyDataSent (uint32_t size); + void NotifyDataReceived (const Packet &p, const Address &from); - - static bool RefuseAllConnections (Ptr socket, const Address& address); - static void DummyCallbackVoidSocket (Ptr socket); - static void DummyCallbackVoidSocketUi32 (Ptr socket, uint32_t); - static void DummyCallbackVoidSocketUi32Address (Ptr socket, uint32_t, const Address &); - static void DummyCallbackVoidSocketBufferUi32Address (Ptr socket, const uint8_t *, uint32_t, - const Address &); - static void DummyCallbackVoidSocketAddress (Ptr socket, const Address &); + Callback > m_closeCompleted; + Callback > m_connectionSucceeded; + Callback > m_connectionFailed; + Callback > m_halfClose; + Callback > m_closeRequested; + Callback, const Address &> m_connectionRequest; + Callback, const Address&> m_newConnectionCreated; + Callback, uint32_t> m_dataSent; + Callback, const uint8_t*, uint32_t,const Address&> m_receivedData; + Callback, uint32_t,const Address&> m_receivedDummyData; }; } //namespace ns3