//
// Copyright (C) 2004 Enterprise Distributed Technologies Ltd
//
// www.enterprisedt.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// Bug fixes, suggestions and comments should posted on
// http://www.enterprisedt.com/forums/index.php
//
// Change Log:
//
// $Log: BaseSocket.cs,v $
// Revision 1.2 2004/11/13 19:03:49 bruceb
// GetStream() changed, added comments
//
// Revision 1.1 2004/10/29 14:30:10 bruceb
// first cut
//
//
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace EnterpriseDT.Net
{
///
/// Socket abstraction that simplifies socket code
///
///
/// Hans Andersen
///
///
/// $LastChangedRevision$
///
public abstract class BaseSocket
{
///
/// Creates a new Socket for a newly created connection
///
public abstract BaseSocket Accept();
///
/// Associates a Socket with a local endpoint.
///
public abstract void Bind(EndPoint localEP);
///
/// Closes the Socket connection and releases all associated resources.
///
public abstract void Close();
///
/// Establishes a connection to a remote endpoint
///
public abstract void Connect(EndPoint remoteEP);
///
/// Places socket in a listening state.
///
public abstract void Listen(int backlog);
///
/// Get the stream associated with the socket.
///
///
/// The stream returned owns the socket, so closing the
/// stream will close the socket
///
public abstract Stream GetStream();
///
/// Receives data from a bound Socket.
///
public abstract int Receive(byte[] buffer);
///
/// Sends data to a connected Socket.
///
public abstract int Send(byte[] buffer);
///
/// Sets a Socket option.
///
public abstract void SetSocketOption(
SocketOptionLevel optionLevel,
SocketOptionName optionName,
int optionValue);
///
/// Gets the local endpoint.
///
public abstract EndPoint LocalEndPoint {get;}
}
///
/// Standard implementation of BaseSocket
///
///
/// Hans Andersen
///
///
/// $LastChangedRevision$
///
public class StandardSocket : BaseSocket
{
///
/// The real socket this class is wrapping
///
private Socket socket;
///
/// Initializes a new instance of the StandardSocket class
///
public StandardSocket(
AddressFamily addressFamily,
SocketType socketType,
ProtocolType protocolType
)
{
socket = new Socket(addressFamily, socketType, protocolType);
}
///
/// Initializes a new instance of the StandardSocket class
///
protected StandardSocket(Socket socket)
{
this.socket = socket;
}
///
/// Creates a new Socket for a newly created connection
///
public override BaseSocket Accept()
{
return new StandardSocket(socket.Accept());
}
///
/// Associates a Socket with a local endpoint.
///
public override void Bind(EndPoint localEP)
{
socket.Bind(localEP);
}
///
/// Closes the Socket connection and releases all associated resources.
///
public override void Close()
{
socket.Close();
}
///
/// Establishes a connection to a remote endpoint
///
public override void Connect(EndPoint remoteEP)
{
socket.Connect(remoteEP);
}
///
/// Places socket in a listening state.
///
public override void Listen(int backlog)
{
socket.Listen(backlog);
}
///
/// Get the stream associated with the socket.
///
public override Stream GetStream()
{
return new NetworkStream(socket, true);
}
///
/// Receives data from a bound Socket.
///
public override int Receive(byte[] buffer)
{
return socket.Receive(buffer);
}
///
/// Sends data to a connected Socket.
///
public override int Send(byte[] buffer)
{
return socket.Send(buffer);
}
///
/// Sets a Socket option.
///
public override void SetSocketOption(
SocketOptionLevel optionLevel,
SocketOptionName optionName,
int optionValue)
{
socket.SetSocketOption(optionLevel, optionName, optionValue);
}
///
/// Gets the local endpoint.
///
public override EndPoint LocalEndPoint
{
get
{
return socket.LocalEndPoint;
}
}
}
}