(IPAddress.Any, 1024);
71 | Console.WriteLine("Starting Server...");
72 | _server.Start();
73 | Console.WriteLine("Server started!");
74 | _server.ClientConnected += ClientConnected;
75 | _server.ReceivedMessage += ServerMessageReceived;
76 | }
77 |
78 | private static async void ServerMessageReceived(IPEndPoint sender, string message)
79 | {
80 | Console.WriteLine($"{sender}: {message}");
81 | await _server.Send($"Hello {sender}!", sender);
82 | }
83 |
84 | private static void ClientMessageReceived(IPEndPoint sender, string message)
85 | {
86 | Console.WriteLine($"{sender}: {message}");
87 | }
88 |
89 | private static async void ClientConnected(IPEndPoint address)
90 | {
91 | await _server.Send($"Hello {address}!", address);
92 | }
93 | }
94 | }
--------------------------------------------------------------------------------
/Images/Icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mrousavy/GenericProtocol/b94290a4b6fa21968d4cfc443f6c7be328f6ea17/Images/Icon.png
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 Marc Rousavy
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 | <T>
4 |
5 | Generic Protocol
6 |
7 |
8 | ⚡️ A fast TCP event based buffered server/client protocol for transferring data over the (inter)net in .NET 🌐
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 | ## Why?
33 | > Send whole objects over the net easier and faster
34 |
35 | 1. Send nearly **any** .NET `object`
36 | (See: [supported](https://github.com/neuecc/ZeroFormatter#built-in-support-types), [custom](https://github.com/neuecc/ZeroFormatter#quick-start)) :package:
37 | 2. Send/Receive **faster** by **buffered send/receive** and **ZeroFormatter**'s **fast (de-)serializing** :dash:
38 | 3. Automatically **correct errors** with **TCP** and **Auto-reconnect** :white_check_mark:
39 | 4. **Async** and **Event** based :zap:
40 | 5. **Efficient Network Discovery** for other **GenericProtocol Hosts** :mag:
41 | 6. **Fast binary links** for file/images/.. transfer :floppy_disk:
42 | 7. Made with love :heart:
43 |
44 | Sending objects:
45 | ```csharp
46 | await client.Send(someObject);
47 | ```
48 |
49 | ...on the other end:
50 | ```csharp
51 | private void MyMessageReceivedCallback(SomeObject someObject) {
52 | Console.WriteLine("I've received an object!");
53 | }
54 | ```
55 |
56 | ## How?
57 | Add **GenericProtocol** to your existing **.NET**/**.NET Core 2.0+**/**.NET Standard 2.0+** Project via **NuGet**:
58 | ```
59 | PM> Install-Package GenericProtocol
60 | ```
61 |
62 | Use the default namespace:
63 | ```csharp
64 | using GenericProtocol;
65 | ```
66 |
67 | Are you [connecting to a server](#client), or **are you** [the server](#server)?
68 |
69 |
70 | ## Client
71 | ### Connect to a [server](#server):
72 | ```csharp
73 | IClient client = await Factory.StartNewClient("82.205.121.132", 1024, true);
74 | ```
75 | The Factory will **construct and connect** a new `IClient` object, where `` is the object
76 | you want to **send over the net** (Here: `MessageObject`). This can be ([supported](https://github.com/neuecc/ZeroFormatter#built-in-support-types))
77 | **built in types** (`string`, `IEnumerable`, ..) or **custom types** marked with `[ZeroFormattable]` (see [here](https://github.com/neuecc/ZeroFormatter#quick-start))
78 |
79 | ### Send/Receive custom objects:
80 | ```csharp
81 | // MessageObject.cs
82 | [ZeroFormattable]
83 | public struct MessageObject {
84 | [Index(0)]
85 | public string Sender { get; set; }
86 | [Index(1)]
87 | public string Recipient { get; set; }
88 | [Index(2)]
89 | public string Message { get; set; }
90 | [Index(3)]
91 | public DateTime Timestamp { get; set; }
92 | }
93 |
94 | // Main.cs
95 | IClient client = await Factory.StartNewClient("82.205.121.132", 1024);
96 | // MyMessageReceivedCallback will be called whenever this client receives a message
97 | client.ReceivedMessage += MyMessageReceivedCallback; // void MyCallback(IPEndPoint, MessageObject)
98 |
99 | var msgObject = new MessageObject() {
100 | Sender = "mrousavy",
101 | Recipient = "cbarosch",
102 | Message = "Hi server!",
103 | Timestamp = DateTime.Now
104 | }
105 | await client.Send(msgObject);
106 | // (Optionally configure your Server so that it should redirect to the Recipient)
107 | client.Dispose();
108 | ```
109 |
110 | ### Send large binary content
111 | ```csharp
112 | IClient client = await Factory.StartNewBinaryDownlink("82.205.121.132", 1024, true);
113 | client.Send(bytes); // bytes can be a large file for example
114 | client.Dispose();
115 | ```
116 | Use `BinaryDownlinks`/`BinaryUplinks` when you just want to **send binary content** (Files, Images, ..). The binary links will skip the serialization and **send buffered right away**.
117 |
118 | ### Other
119 | ```csharp
120 | // Automatically try to reconnect on disconnects
121 | client.AutoReconnect = true;
122 | // Set the reading buffer size for incoming data
123 | client.ReceiveBufferSize = 2048;
124 | // Set the writing buffer size for outgoing data
125 | client.SendBufferSize = 2048;
126 | // Get the current Connection status
127 | var status = client.ConnectionStatus;
128 | // Connection to server lost handler
129 | client.ConnectionLost += ...;
130 | ```
131 |
132 | ## Server
133 | ### Start a new server:
134 | ```csharp
135 | IServer server = await Factory.StartNewServer(IPAddress.Any, 1024, true);
136 | ```
137 |
138 | ### Send/Receive your objects (`MessageObject` in this example):
139 | ```csharp
140 | // Attach to the Message Received event
141 | server.ReceivedMessage += MyMessageReceivedCallback; // void MyCallback(IPEndPoint, MessageObject)
142 |
143 | var msgObject = new MessageObject() {
144 | Sender = "server",
145 | Recipient = "mrousavy",
146 | Message = "Hello client!",
147 | Timestamp = DateTime.Now
148 | }
149 | var clientEndPoint = server.Clients.First(); // Get first client in connected-clients enumerable
150 | await server.Send(msgObject, clientEndPoint); // Send object to given client
151 | ```
152 |
153 | ### Other
154 | ```csharp
155 | // Event once a client connects
156 | server.ClientConnected += ...; // void ClientConnectedCallback(IPEndPoint)
157 | // Event once a client disconnects
158 | server.ClientDisconnected += ...; // void ClientDisconnectedCallback(IPEndPoint)
159 | // Set the reading buffer size for incoming data
160 | server.ReceiveBufferSize = 2048;
161 | // Set the writing buffer size for outgoing data
162 | server.SendBufferSize = 2048;
163 | // Set the count of maximum clients to queue on simultanious connection attempts
164 | server.MaxConnectionsBacklog = 8;
165 | ```
166 |
167 | > License: [MIT](https://github.com/mrousavy/GenericProtocol/blob/master/LICENSE) | [Contributing](https://github.com/mrousavy/GenericProtocol/blob/master/CONTRIBUTING.md) | Thanks!
168 |
--------------------------------------------------------------------------------
/appveyor.yml:
--------------------------------------------------------------------------------
1 | version: 1.0.{build}
2 | skip_non_tags: true
3 | image: Visual Studio 2017
4 | configuration: Release
5 | dotnet_csproj:
6 | patch: true
7 | file: 'GenericProtocol\*.csproj'
8 | version: '{version}'
9 | package_version: '{version}'
10 | cache:
11 | - packages -> **\packages.config
12 |
13 | before_build:
14 | - cmd: >-
15 | dotnet restore
16 |
17 | nuget restore
18 | build:
19 | verbosity: minimal
20 | after_build:
21 | - cmd: >-
22 | dotnet pack GenericProtocol\GenericProtocol.csproj
23 | artifacts:
24 | - path: GenericProtocol\bin\$(configuration)\*.nupkg
25 | name: NupkgArtifact
26 | deploy:
27 | provider: NuGet
28 | api_key:
29 | secure: GbnKcTtDdY0RbYxlclr1ad4oL0qmge9VN9bkiA8r8PWx5ElCPfKo5m/6cR79cTgG
30 | skip_symbols: true
31 | artifact: /.*\.nupkg/
32 |
--------------------------------------------------------------------------------