2021-07-07 09:49:36 +08:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
using OnlineUserPool.Model;
|
2021-01-18 20:24:19 +08:00
|
|
|
|
using OnlineUserPool.Unility;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
using System.Net;
|
|
|
|
|
|
using System.Net.Sockets;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
|
|
namespace OnlineUserPool.Services
|
|
|
|
|
|
{
|
|
|
|
|
|
public class UdpService : IService
|
|
|
|
|
|
{
|
|
|
|
|
|
private bool _stop = false;
|
|
|
|
|
|
//private static IPEndPoint serverIpEndPoint;
|
|
|
|
|
|
private static UdpClient udpServer;
|
|
|
|
|
|
|
|
|
|
|
|
public UdpService()
|
|
|
|
|
|
{
|
|
|
|
|
|
//serverIpEndPoint = new IPEndPoint(IPAddress.Parse(ConfigHelp.Ip), ConfigHelp.Port);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-07 09:49:36 +08:00
|
|
|
|
public void RunServer(Action<IPEndPoint, ReceiveModel, IService> action, Action<EndPoint> disconnected = null)
|
2021-01-18 20:24:19 +08:00
|
|
|
|
{
|
2021-06-24 09:20:01 +08:00
|
|
|
|
udpServer = new UdpClient(ConfigHelp.UdpPort);
|
2021-01-18 20:24:19 +08:00
|
|
|
|
uint IOC_IN = 0x80000000;
|
|
|
|
|
|
uint IOC_VENDOR = 0x18000000;
|
|
|
|
|
|
uint SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12;
|
|
|
|
|
|
udpServer.Client.IOControl((int)SIO_UDP_CONNRESET, new byte[] { Convert.ToByte(false) }, null);
|
|
|
|
|
|
var remoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
|
|
|
|
|
|
Task.Run(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
while (_stop == false)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
byte[] receiveBytes = udpServer.Receive(ref remoteIpEndPoint);
|
|
|
|
|
|
var returnData = Encoding.ASCII.GetString(receiveBytes);
|
2021-07-07 09:49:36 +08:00
|
|
|
|
var msg = JsonConvert.DeserializeObject<ReceiveModel>(returnData);
|
2021-01-18 20:24:19 +08:00
|
|
|
|
|
|
|
|
|
|
//lock (locker)
|
|
|
|
|
|
{
|
|
|
|
|
|
#if DEBUG
|
|
|
|
|
|
WriteLine($"本次接收:{ remoteIpEndPoint.Address.ToString() }:{ remoteIpEndPoint.Port }收到消息:{ returnData }");
|
|
|
|
|
|
#endif
|
2021-07-07 09:49:36 +08:00
|
|
|
|
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);
|
|
|
|
|
|
}
|
2021-01-18 20:24:19 +08:00
|
|
|
|
|
|
|
|
|
|
//if (timer.AutoReset == false)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// timer.AutoReset = true;
|
|
|
|
|
|
//}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
|
|
|
|
|
Serilog.Log.Error("RunServer:" + e.Message);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Send(byte[] dgram, int bytes, IPEndPoint endPoint)
|
|
|
|
|
|
{
|
2021-09-18 14:04:52 +08:00
|
|
|
|
udpServer.SendAsync(dgram, bytes, endPoint);
|
2021-01-18 20:24:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Close()
|
|
|
|
|
|
{
|
|
|
|
|
|
_stop = true;
|
|
|
|
|
|
udpServer.Close();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void WriteLine(string str)
|
|
|
|
|
|
{
|
|
|
|
|
|
//Debug.WriteLine(str);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|