105 lines
3.4 KiB
Markdown
105 lines
3.4 KiB
Markdown
# Custom Protocol Support
|
|
|
|
The EonaCat.Connections library now provides **extensible protocol support** that allows you to easily integrate custom network protocols without modifying the core library.
|
|
|
|
## Key Features
|
|
|
|
### 1. **INetworkProtocol Interface**
|
|
A clean, well-defined contract that any protocol implementation must follow:
|
|
- `ConnectAsync`, `SendAsync`, `ReceiveAsync`, `DisconnectAsync`
|
|
- Configuration methods: `ConfigureKeepAlive`, `ConfigureSsl`, `ConfigureBufferSize`, etc.
|
|
- Utility methods: `GetRemoteEndPoint`, `GetLocalEndPoint`, `GetUnderlyingSocket`, `GetUnderlyingStream`
|
|
|
|
### 2. **ProtocolFactory**
|
|
Central registry for managing protocol implementations:
|
|
- Register custom protocols: `ProtocolFactory.RegisterProtocol("CUSTOM", () => new CustomProtocol())`
|
|
- Create instances: `ProtocolFactory.CreateProtocol("CUSTOM")`
|
|
- Query registered protocols: `ProtocolFactory.IsProtocolRegistered("CUSTOM")`
|
|
- Get all protocols: `ProtocolFactory.GetRegisteredProtocols()`
|
|
- Unregister protocols (except built-in TCP/UDP)
|
|
|
|
### 3. **Easy Integration with Configuration**
|
|
Use protocols with a single line:
|
|
```csharp
|
|
var config = new Configuration
|
|
{
|
|
ProtocolFactory = () => new MyCustomProtocol()
|
|
};
|
|
```
|
|
|
|
### 4. **Built-in Implementations**
|
|
TCP and UDP protocols are provided out-of-the-box:
|
|
- `TcpProtocol` - Full TCP support with SSL/TLS, keep-alive, socket options
|
|
- `UdpProtocol` - UDP connectionless protocols
|
|
|
|
### 5. **Backward Compatibility**
|
|
Existing code continues to work without changes:
|
|
```csharp
|
|
// Old way still works
|
|
var config = new Configuration { Protocol = ProtocolType.TCP };
|
|
|
|
// New way for custom protocols
|
|
var config = new Configuration { ProtocolFactory = () => new CustomProtocol() };
|
|
```
|
|
|
|
## Use Cases
|
|
|
|
- **QUIC Protocol** - Modern, low-latency transport
|
|
- **HTTP/2 or HTTP/3** - Web-based communication
|
|
- **WebSocket** - Browser-compatible real-time communication
|
|
- **Custom Binary Protocols** - Application-specific formats
|
|
- **Compression Wrappers** - Transparent data compression
|
|
- **Encryption Wrappers** - Protocol-level encryption
|
|
- **Protocol Composition** - Layered protocol stacks
|
|
|
|
## Files Added
|
|
|
|
- `EonaCat.Connections/Protocols/INetworkProtocol.cs` - Protocol interface
|
|
- `EonaCat.Connections/Protocols/ProtocolFactory.cs` - Protocol registry
|
|
- `EonaCat.Connections/Protocols/TcpProtocol.cs` - TCP implementation
|
|
- `EonaCat.Connections/Protocols/UdpProtocol.cs` - UDP implementation
|
|
- `EonaCat.Connections/Protocols/CUSTOM_PROTOCOLS.md` - Complete documentation
|
|
|
|
## Files Modified
|
|
|
|
- `EonaCat.Connections/Models/Configuration.cs`
|
|
- Added `ProtocolFactory` property (optional custom protocol factory)
|
|
- Added `GetProtocol()` method (creates and configures protocol instance)
|
|
- Maintains backward compatibility with `Protocol` enum
|
|
|
|
## Getting Started
|
|
|
|
### 1. Create Your Protocol
|
|
Implement `INetworkProtocol`:
|
|
```csharp
|
|
public class MyProtocol : INetworkProtocol
|
|
{
|
|
public string ProtocolName => "MyProtocol";
|
|
public bool IsConnected => /* ... */;
|
|
// Implement required methods...
|
|
}
|
|
```
|
|
|
|
### 2. Register (Optional)
|
|
```csharp
|
|
ProtocolFactory.RegisterProtocol("MY_PROTO", () => new MyProtocol());
|
|
```
|
|
|
|
### 3. Use in Configuration
|
|
```csharp
|
|
var config = new Configuration
|
|
{
|
|
ProtocolFactory = () => new MyProtocol(),
|
|
Host = "example.com",
|
|
Port = 8080
|
|
};
|
|
|
|
var client = new NetworkClient(config);
|
|
await client.ConnectAsync();
|
|
```
|
|
|
|
That's it! Your custom protocol is now integrated.
|
|
|
|
## Questions?
|
|
|
|
Please contact the Brasser |