|
| 1 | +using System; |
| 2 | +using System.Net; |
| 3 | + |
| 4 | +namespace SimplSockets |
| 5 | +{ |
| 6 | + /// <summary> |
| 7 | + /// Represents a socket. |
| 8 | + /// </summary> |
| 9 | + public interface ISimplSocket : IDisposable |
| 10 | + { |
| 11 | + /// <summary> |
| 12 | + /// Connects to an endpoint. Once this is called, you must call Close before calling Connect or Listen again. This method will not raise an error. |
| 13 | + /// </summary> |
| 14 | + /// <param name="endPoint">The endpoint.</param> |
| 15 | + /// <returns>true if connection is successful, false otherwise.</returns> |
| 16 | + bool Connect(EndPoint endPoint); |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// Begin listening for incoming connections. Once this is called, you must call Close before calling Connect or Listen again. |
| 20 | + /// </summary> |
| 21 | + /// <param name="localEndpoint">The local endpoint to listen on.</param> |
| 22 | + void Listen(EndPoint localEndpoint); |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Sends a message to the server without waiting for a response (one-way communication). |
| 26 | + /// </summary> |
| 27 | + /// <param name="message">The message to send.</param> |
| 28 | + void Send(byte[] message); |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Sends a message to the server and receives the response to that message. |
| 32 | + /// </summary> |
| 33 | + /// <param name="message">The message to send.</param> |
| 34 | + /// <returns>The response message.</returns> |
| 35 | + byte[] SendReceive(byte[] message); |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Sends a message back to the client. |
| 39 | + /// </summary> |
| 40 | + /// <param name="message">The reply message to send.</param> |
| 41 | + /// <param name="receivedMessage">The received message which is being replied to.</param> |
| 42 | + void Reply(byte[] message, SimplSocket.ReceivedMessage receivedMessage); |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Closes the connection. Once this is called, you can call Connect or Listen again to start a new connection. |
| 46 | + /// </summary> |
| 47 | + void Close(); |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Gets the currently connected client count (when acting as a client, this will return 0 or 1). |
| 51 | + /// </summary> |
| 52 | + int CurrentlyConnectedClientCount { get; } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// An event that is fired whenever a message is received. Hook into this to process messages and potentially call Reply to send a response. |
| 56 | + /// </summary> |
| 57 | + event EventHandler<MessageReceivedArgs> MessageReceived; |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// An event that is fired whenever a socket communication error occurs. Hook into this to do something when communication errors happen. |
| 61 | + /// </summary> |
| 62 | + event EventHandler<SocketErrorArgs> Error; |
| 63 | + } |
| 64 | +} |
0 commit comments