markiodev / networker Goto Github PK
View Code? Open in Web Editor NEWA simple to use TCP and UDP networking library for .NET. Compatible with Unity.
License: MIT License
A simple to use TCP and UDP networking library for .NET. Compatible with Unity.
License: MIT License
Hey. I scanned the code, and I liked it. But I can not find the opportunity to work without a serializer with byte array. Please add this low-level function.
在UdpClientListener中
public UdpClientListener(ServerBuilderOptions options,
ILogger<UdpClientListener> logger,
IServerPacketProcessor serverPacketProcessor,
IServerInformation serverInformation)
{
this.options = options;
this.logger = logger;
this.serverPacketProcessor = serverPacketProcessor;
this.serverInformation = serverInformation;
endPointPool = new ObjectPool<EndPoint>(this.options.UdpSocketObjectPoolSize);
for (var i = 0; i < endPointPool.Capacity; i++)
//endPointPool.Push(new IPEndPoint(IPAddress.Loopback, this.options.UdpPort));
endPointPool.Push(new IPEndPoint(BindIPAddress, this.options.UdpPort));
}
public void Listen()
{
client = new UdpClient();
client.ExclusiveAddressUse = false;
client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
//临时处理,后续使用option传入网卡地址
//endPoint = new IPEndPoint(IPAddress.Loopback, options.UdpPort);
endPoint = new IPEndPoint(BindIPAddress, options.UdpPort);
client.Client.Bind(endPoint);
...
}
RegisterPacketHandler<ChatPacket, ClientChatPacketHandler>()
if I have 1000 Handlers, I must write it 1000 times?
Hey, love the library! One issue however is the library is licensed under GPL v3 which would require all applications/games using it to also be GPL and open source. Is switching to a separate license such as MIT possible?
Hello,
I would like to use your libs but a this moment missing possibility to exchange serializer and deserializer block me. Would you like to add this in future?
Networker uses its own ILogger abstraction and doesn't benefit from any of the logger ecosystem provided by Microsoft.Extensions.Logging.
Please.
What do you think about adding built-in support for Packet Handler Decorators?
We could use an Attribute-based means of assigning Decorators to Packet Handlers and wire them up at startup.
This blog talks about the Command Handler Pattern and has a section about enhancing the pattern with Decorators:
https://blogs.cuttingedge.it/steven/posts/2011/meanwhile-on-the-command-side-of-my-architecture/
The article provides these use case examples:
Here's an example of how you might define custom Decorators for a Handler:
[Decorate(
typeof(LoggingHandlerDecorator),
typeof(AuthorizationHandlerDecorator),
typeof(BackgroundJobHandlerDecorator)]
public class SomePacketHandler : PacketHandlerBase<SomePacket>
{
....
}
Having a way to define global or module Decorators could prevent having to add the Decorate attribute to so many Handler classes.
Here's an example of the sort of benefits that Decorators can provide, especially when used with helper Attributes:
[Authorization(30)] // User must have authorization level of 30 for this to succeed
public override Task Process(SomePacket packet, IPacketContext context)
{
...
}
For this example, the AuthorizationHandlerDecorator.Process method might look something like this:
public override async Task Process(SomePacket packet, IPacketContext context)
{
int userAccessLevel = 0; // Get user access level from service
// Gets from the method's Authorization Attribute
int requiredAccessLevel = GetRequiredAccessLevel(packet);
if (userAccessLevel >= requiredAccessLevel)
{
await DecoratedHandler.Process(packet, context);
}
}
Thoughts?
Absent Semaphore.Realese() call. So after 100 (default) separated connections even with the same client app get stuck and require restart.
V3.0.3
Hello there,
I was wondering if a client is able to connect via both TCP and UDP at the same time allowing you to determine which message types you would like to send over each connection? Also, is it possible to have multiple listeners setup on the server side for more than one port? I wanted the interconnecting servers to connect to one port and clients to connect on another and the messages get handled differently based on what port the connection came in on.
Thanks!
void ProcessUdpFromBuffer(EndPoint sender, byte[] buffer, int offset = 0, int length = 0);
private async Task Process(EndPoint endPoint, byte[] buffer)
{
// serverPacketProcessor.ProcessUdpFromBuffer(endPoint, buffer);
serverPacketProcessor.ProcessUdpFromBuffer(endPoint, buffer,0,buffer.Length);
endPointPool.Push(endPoint);
}
HI, does the udp version reliable? or default udp.
Need to add some guidelines to the Wiki about how to correctly configure the object pooling base on max connections.
Hi just tested your library and found a issue about this event, ClientDisconnected on server never trigger.
There is a second problem, there is no ClientConnected and Close/Disconnect on client side
example:
``
public ApisServer(string host, int port)
{
Host = host;
Port = port;
server = new NetworkerServerBuilder()
.UseIpAddresses(new[] {host}).UseConsoleLogger().UseTcp(port)
.RegisterPacketHandler<CL_Login, LoginHandler>()
.Build<DefaultServer>();
server.ClientConnected += ClientConnected;
server.ClientDisconnected += ClientDisconnected;
}
private void ClientDisconnected(object sender, TcpConnectionDisconnectedEventArgs e)
{
Logger.Debug("Client disconnected {0}", e.Connection.Identifier); // Never trigger
}
private void ClientConnected(object sender, TcpConnectionConnectedEventArgs e)
{
Logger.Debug("Client connected {0}", e.Connection.Identifier);
}
``
This task is to restructure the projects to separate the client from server. Refactor code for performance and extensibility.
I see that encryption is on your road map -- and it's a feature I desperately want in a library like this.
I'm tempted to try and implement it myself in a fork, but I'd likely use something like Inferno. How are you planning to add encryption?
I was looking at the code and I figured it would be fairly easy to register some packet handlers for passing public keys between client/server and then encrypting/decrypting just before serialization/deserialization.
I feel dumb for asking this, but building a .dll file for Unity doesn't work, because Unity says that it has errors and unloads it.
Unloading broken assembly Assets/Plugins/Networker/Networker.dll, this assembly can cause crashes in the runtime
Something strange happens here ... need help, I can reproduce the failure
fail: Networker.Client.ClientPacketProcessor[0]
System.ArgumentOutOfRangeException: Index and count must refer to a location within the buffer.
Parameter name: bytes
at Networker.Client.ClientPacketProcessor.Process(Byte[] buffer, ISender sender) in C:\Users\admin\Source\Repos\Networker\Networker\Client\ClientPacketProcessor.cs:line 134
at Networker.Client.ClientPacketProcessor.Process(Socket socket) in C:\Users\admin\Source\Repos\Networker\Networker\Client\ClientPacketProcessor.cs:line 70
Hi,
I spent the whole day trying to make this project work with Unity but unfortunately I still cannot import the dll without errors.
I tried many different things and it seems like the necessary dependencies are not included in the build.
Here is the kind of errors I am getting when importing the dll of Networker:
Unable to resolve reference 'Microsoft.Extensions.Logging.Abstractions'. Is the assembly missing or incompatible with the current platform?
Reference validation can be disabled in the Plugin Inspector.
Unable to resolve reference 'Microsoft.Extensions.Configuration.Abstractions'. Is the assembly missing or incompatible with the current platform?
Reference validation can be disabled in the Plugin Inspector.
Unable to resolve reference 'Microsoft.Extensions.Logging'. Is the assembly missing or incompatible with the current platform?
Reference validation can be disabled in the Plugin Inspector.
Unable to resolve reference 'Microsoft.Extensions.DependencyInjection.Abstractions'. Is the assembly missing or incompatible with the current platform?
Reference validation can be disabled in the Plugin Inspector.
Unable to resolve reference 'Microsoft.Extensions.DependencyInjection'. Is the assembly missing or incompatible with the current platform?
Reference validation can be disabled in the Plugin Inspector.
Unable to resolve reference 'Microsoft.Extensions.Options.ConfigurationExtensions'. Is the assembly missing or incompatible with the current platform?
Reference validation can be disabled in the Plugin Inspector.
Any idea what I am doing wrong or how to fix it?
Thank you,
Etienne
Need to look at this before v3 release.
Hi,
I have got stuck in a weird situation. I want to initiate a send from the server side to all of the clients. So logically this should work:
private IServer _server
...
var connections = _server.GetConnections().GetConnections();
foreach (ITcpConnection connection in connections)
{
connection.Socket.Send(Encoding.ASCII.GetBytes("COMMAND1"));
}
But client doesn't receive this command.
Now if I save the server context somewhere and reuse it later it works:
private ISender _sender;
ServerPacketHandler_OnProcess(object sender, string e)
{
_sender = (sender as IPacketContext).Sender;
}
Now this one will work :
_sender.Send<string>("COMMAND1");`
What is the correct way of initiating a send from the server. Its not in the example and tests either.
Currently the second RegisterTypes call overrides the previous call as it's saved as an action in the builder.
new ServerBuilder()
.RegisterTypes(serviceCollection =>
{
// Never called
})
.RegisterTypes(serviceCollection =>
{
// Called
})
Would be nice if it was possible to call it multiple times or that it alerted that it is already set.
The use of Udp is possible only at the reception, since at this moment _socketSender is null and cannot be specified yet. Since SetUdpSender is never called, sending Udp packets is not possible.
If an application has a lot of packets and the developer must create a handler for each packet, it might get tedious, especially if a group of packet handlers have the same dependencies.
Ideally, related packets that require similar dependencies would get processed by the same handler. For example, in a game, all combat packets could get handled by CombatHandler
. Of course if this class grows unwieldy, a developer could always break it all the way down to the traditional 1:1 relationship, if desired.
If the packet processor handled deserialization and the handler just processed the packet, then PacketHandlerBase<T>
could get replaced by IPacketHandler<T>
which would just require the Process
method. This way, a concrete implementation could implement any number of IPacketHandler<T>
interfaces.
What do you think?
Hello, i try to use Networker with unity but there are errors:
I followed the #43 instructions (Compile Networker.dll to 4.7.2, set unity in .net 4.7.* and put it in a Plugins folder)
In my plugin folder there is Networker.dll in 4.7.2 version and Microsoft.Extensions.Logging.
I have tried to put the assemblies files which causes the problem in it (With the 4.7.2 compilation and without) but it didn't change anything.
Is it possible to make a simple example of a unity project ? Or to upload all the dlls in the good version ?
Thank you
var packetSize = packetSerialiser.CanReadLength
? BitConverter.ToInt32(buffer, currentPosition)
: 0;
//包长度后续没赋值,在不能读取长度的业务中陷入循环,如果数据包没有长度说明,则使用整包的长度
var packetSize = packetSerialiser.CanReadLength
? BitConverter.ToInt32(buffer, currentPosition)
: length;
Hello Mark.
I look for a tcp&udp library for my unity project for a while and i finally find your project.
i have try add the source code to the a new project but it tell the error like the issue(#43)
i also see your blob in your site .but it seems only not to tell how to do it with unity now yet.
Could you please provide a simple unity client project using with the networker source code example in your github repo.
thank you very much. waiting for your reply.
Is almost ready! Subscribe here for notifications
Hi,
I'm trying to play with Networker, so I just installed this package, then I set a simple server like this
public sealed class UdpServer
{
private readonly IServer _server;
public UdpServer (int port)
{
_server = new ServerBuilder().UseUdp(port).RegisterPacketHandlerModule<PacketHandler>().Build();
}
public void Start ()
{
_server.Start();
Console.WriteLine($"UdpServer running on ${_server}");
}
}
And as soon as I instanciate UdpServer
(not even calling Start) i got this error:
Unhandled Exception: System.InvalidOperationException: Unable to resolve service for type 'Networker.Common.Abstractions.IPacketSerialiser' while attempting to activate 'Networker.Server.ServerPacketProcessor'.
at Microsoft.Extensions.DependencyInjection.ServiceLookup.Service.PopulateCallSites(ServiceProvider provider, ISet1 callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound) at Microsoft.Extensions.DependencyInjection.ServiceLookup.Service.CreateCallSite(ServiceProvider provider, ISet
1 callSiteChain)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetResolveCallSite(IService service, ISet1 callSiteChain) at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetServiceCallSite(Type serviceType, ISet
1 callSiteChain)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.Service.PopulateCallSites(ServiceProvider provider, ISet1 callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound) at Microsoft.Extensions.DependencyInjection.ServiceLookup.Service.CreateCallSite(ServiceProvider provider, ISet
1 callSiteChain)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetResolveCallSite(IService service, ISet1 callSiteChain) at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetServiceCallSite(Type serviceType, ISet
1 callSiteChain)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(Type serviceType, ServiceProvider serviceProvider)
at System.Collections.Concurrent.ConcurrentDictionaryExtensions.GetOrAdd[TKey,TValue,TArg](ConcurrentDictionary2 dictionary, TKey key, Func
3 valueFactory, TArg arg)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider)
at Networker.Server.ServerBuilder.Build()
at Survixel.Server.Networking.UDP.UdpServer..ctor(Int32 port) in C:\Users\alexr\Prog\C#\Survixel.Server\Survixel.Server.Networking\UDP\UdpServer.cs:line 13
at Survixel.Server.Core.Program.Main() in C:\Users\alexr\Prog\C#\Survixel.Server\Survixel.Server.Core\Program.cs:line 10
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.