forked from powerfun/udpservice
针对unity版本比赛功能的调整
This commit is contained in:
1 parent
b9bb485a2d
commit
c380654041
10 files changed
+393
-74
No files matched your search
@@ -8,7 +8,7 @@ namespace OnlineUserPool.Services
|
||||
{
|
||||
public interface IService
|
||||
{
|
||||
public void RunServer(Action<IPEndPoint, MsgModel, IService> action);
|
||||
public void RunServer(Action<IPEndPoint, ReceiveModel, IService> received, Action<EndPoint> disconnected = null);
|
||||
|
||||
public void Send(byte[] dgram, int bytes, IPEndPoint endPoint);
|
||||
|
||||
|
||||
+58
-15
@@ -1,4 +1,5 @@
|
||||
using NetCoreServer;
|
||||
using Newtonsoft.Json;
|
||||
using OnlineUserPool.Model;
|
||||
using OnlineUserPool.Unility;
|
||||
using Serilog;
|
||||
@@ -21,35 +22,40 @@ namespace OnlineUserPool.Services
|
||||
//throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void RunServer(Action<IPEndPoint, MsgModel, IService> action)
|
||||
public void RunServer(Action<IPEndPoint, ReceiveModel, IService> action, Action<EndPoint> disconnected = null)
|
||||
{
|
||||
//throw new NotImplementedException();
|
||||
//_action = action;
|
||||
_server = new PfTcpServer(IPAddress.Any, ConfigHelp.TcpPort, (ip, model)=> {
|
||||
action(ip, model, this);
|
||||
});
|
||||
}, disconnected);
|
||||
//_server.OptionNoDelay = true;
|
||||
_server.Start();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void Send(byte[] dgram, int bytes, IPEndPoint endPoint)
|
||||
{
|
||||
if (dgram == null || !dgram.Any())
|
||||
{
|
||||
return;
|
||||
}
|
||||
_server.Multicast(dgram);
|
||||
|
||||
//_server.Multicast(dgram);
|
||||
var session = _server.FindSession(endPoint);
|
||||
if(session != null && session.IsConnected)
|
||||
{
|
||||
session.Send(dgram);
|
||||
}
|
||||
}
|
||||
|
||||
class PfTcpServer : TcpServer
|
||||
{
|
||||
Action<IPEndPoint, MsgModel> _action;
|
||||
public PfTcpServer(IPAddress address, int port, Action<IPEndPoint, MsgModel> action) :base(address, port)
|
||||
private Action<IPEndPoint, ReceiveModel> _action;
|
||||
private Action<EndPoint> _disconnected;
|
||||
//private System.Collections.Generic.Dictionary<Guid, PfTcpSession> clients = new Dictionary<Guid, PfTcpSession>();
|
||||
public PfTcpServer(IPAddress address, int port, Action<IPEndPoint, ReceiveModel> action, Action<EndPoint> disconnected = null) :base(address, port)
|
||||
{
|
||||
_action = action;
|
||||
_disconnected = disconnected;
|
||||
}
|
||||
|
||||
protected override TcpSession CreateSession()
|
||||
@@ -63,24 +69,46 @@ namespace OnlineUserPool.Services
|
||||
//base.OnError(error);
|
||||
Debug.WriteLine(error);
|
||||
}
|
||||
|
||||
public TcpSession FindSession(IPEndPoint ip)
|
||||
{
|
||||
var item = this.Sessions.Values.FirstOrDefault(t => t.Socket.RemoteEndPoint.ToString() == ip.ToString());
|
||||
return item;
|
||||
}
|
||||
|
||||
protected override void OnDisconnected(TcpSession session)
|
||||
{
|
||||
//var bbb = this.Sessions.GetValueOrDefault(session.Id);
|
||||
|
||||
var bbb = (session as PfTcpSession);
|
||||
if(bbb.IPEndPoint != null)
|
||||
{
|
||||
_disconnected?.Invoke(bbb.IPEndPoint);
|
||||
}
|
||||
base.OnDisconnected(session);
|
||||
}
|
||||
}
|
||||
|
||||
class PfTcpSession : TcpSession
|
||||
{
|
||||
Action<IPEndPoint, MsgModel> _action;
|
||||
public PfTcpSession(TcpServer server, Action<IPEndPoint, MsgModel> action) : base(server) {
|
||||
Action<IPEndPoint, ReceiveModel> _action;
|
||||
public IPEndPoint IPEndPoint { get; private set; }
|
||||
public PfTcpSession(TcpServer server, Action<IPEndPoint, ReceiveModel> action) : base(server) {
|
||||
_action = action;
|
||||
|
||||
}
|
||||
|
||||
protected override void OnConnected()
|
||||
{
|
||||
//base.OnConnected();
|
||||
base.OnConnected();
|
||||
Debug.WriteLine("有新的Tcp连接");
|
||||
|
||||
IPEndPoint = IPEndPoint.Parse(this.Socket.RemoteEndPoint.ToString());
|
||||
}
|
||||
|
||||
protected override void OnDisconnected()
|
||||
{
|
||||
//base.OnDisconnected();
|
||||
base.OnDisconnected();
|
||||
Debug.WriteLine("Tcp断开连接");
|
||||
}
|
||||
private string temp = "";
|
||||
@@ -102,10 +130,25 @@ namespace OnlineUserPool.Services
|
||||
if(item == '}')
|
||||
{
|
||||
temp += item;
|
||||
var msg = Newtonsoft.Json.JsonConvert.DeserializeObject<MsgModel>(temp);
|
||||
Debug.WriteLine("收到消息:"+temp);
|
||||
var msg = JsonConvert.DeserializeObject<ReceiveModel>(temp);
|
||||
var ipEndPoint = IPEndPoint.Parse(this.Socket.RemoteEndPoint.ToString());
|
||||
_action(ipEndPoint, msg);
|
||||
|
||||
if (msg.CommandType == 1)
|
||||
{
|
||||
_action(ipEndPoint, JsonConvert.DeserializeObject<MsgModel>(temp));
|
||||
}
|
||||
else if (msg.CommandType == 2)
|
||||
{
|
||||
_action(ipEndPoint, JsonConvert.DeserializeObject<SetClientCommand>(temp));
|
||||
}
|
||||
//else if(msg.CommandType == 3)
|
||||
//{
|
||||
// _action(ipEndPoint, JsonConvert.DeserializeObject<SetWatchCommand>(temp));
|
||||
//}
|
||||
else
|
||||
{
|
||||
_action(ipEndPoint, msg);
|
||||
}
|
||||
temp = "";
|
||||
}
|
||||
else
|
||||
|
||||
+16
-4
@@ -1,4 +1,5 @@
|
||||
using OnlineUserPool.Model;
|
||||
using Newtonsoft.Json;
|
||||
using OnlineUserPool.Model;
|
||||
using OnlineUserPool.Unility;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -21,7 +22,7 @@ namespace OnlineUserPool.Services
|
||||
//serverIpEndPoint = new IPEndPoint(IPAddress.Parse(ConfigHelp.Ip), ConfigHelp.Port);
|
||||
}
|
||||
|
||||
public void RunServer(Action<IPEndPoint, MsgModel, IService> action)
|
||||
public void RunServer(Action<IPEndPoint, ReceiveModel, IService> action, Action<EndPoint> disconnected = null)
|
||||
{
|
||||
udpServer = new UdpClient(ConfigHelp.UdpPort);
|
||||
uint IOC_IN = 0x80000000;
|
||||
@@ -37,14 +38,25 @@ namespace OnlineUserPool.Services
|
||||
{
|
||||
byte[] receiveBytes = udpServer.Receive(ref remoteIpEndPoint);
|
||||
var returnData = Encoding.ASCII.GetString(receiveBytes);
|
||||
var msg = Newtonsoft.Json.JsonConvert.DeserializeObject<MsgModel>(returnData);
|
||||
var msg = JsonConvert.DeserializeObject<ReceiveModel>(returnData);
|
||||
|
||||
//lock (locker)
|
||||
{
|
||||
#if DEBUG
|
||||
WriteLine($"本次接收:{ remoteIpEndPoint.Address.ToString() }:{ remoteIpEndPoint.Port }收到消息:{ returnData }");
|
||||
#endif
|
||||
action(remoteIpEndPoint, msg, this);
|
||||
if (msg.CommandType == 1)
|
||||
{
|
||||
action(remoteIpEndPoint, JsonConvert.DeserializeObject<MsgModel>(returnData), this);
|
||||
}
|
||||
else if (msg.CommandType == 2)
|
||||
{
|
||||
action(remoteIpEndPoint, JsonConvert.DeserializeObject<SetClientCommand>(returnData), this);
|
||||
}
|
||||
else
|
||||
{
|
||||
action(remoteIpEndPoint, msg, this);
|
||||
}
|
||||
|
||||
//if (timer.AutoReset == false)
|
||||
//{
|
||||
|
||||
Reference in new issue
Block a user