forked from powerfun/udpservice
一些功能调整
增加tcp协议支持; 增加模拟多用户的功能;
This commit is contained in:
1 parent
e8b6418aa6
commit
0e931a3ac9
21 files changed
+890
-174
No files matched your search
@@ -0,0 +1,17 @@
|
||||
using OnlineUserPool.Model;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
|
||||
namespace OnlineUserPool.Services
|
||||
{
|
||||
public interface IService
|
||||
{
|
||||
public void RunServer(Action<IPEndPoint, MsgModel, IService> action);
|
||||
|
||||
public void Send(byte[] dgram, int bytes, IPEndPoint endPoint);
|
||||
|
||||
public void Close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
using OnlineUserPool.Model;
|
||||
using OnlineUserPool.Unility;
|
||||
using Serilog;
|
||||
using SimpleTcp;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OnlineUserPool.Services
|
||||
{
|
||||
public class TcpService : IService
|
||||
{
|
||||
private bool _stop = false;
|
||||
SimpleTcpServer socket;
|
||||
Action<IPEndPoint, MsgModel, IService> _action;
|
||||
|
||||
public void RunServer(Action<IPEndPoint, MsgModel, IService> action)
|
||||
{
|
||||
//throw new NotImplementedException();
|
||||
|
||||
_action = action;
|
||||
//var ip = IPAddress.Parse("192.168.0.97");
|
||||
var b = IPAddress.Any;
|
||||
socket = new SimpleTcpServer($"{ IPAddress.Any }:11001");
|
||||
socket.Events.ClientConnected += Events_ClientConnected;
|
||||
socket.Events.ClientDisconnected += Events_ClientDisconnected;
|
||||
socket.Events.DataReceived += Events_DataReceived;
|
||||
|
||||
Task.Run(() =>
|
||||
{
|
||||
socket.Start();
|
||||
});
|
||||
}
|
||||
|
||||
private void Events_DataReceived(object sender, SimpleTcp.DataReceivedEventArgs e)
|
||||
{
|
||||
var returnData = Encoding.UTF8.GetString(e.Data);
|
||||
Debug.WriteLine(returnData);
|
||||
|
||||
foreach (var item in returnData.Split(new string[] { "}" }, StringSplitOptions.RemoveEmptyEntries))
|
||||
{
|
||||
var msg = Newtonsoft.Json.JsonConvert.DeserializeObject<MsgModel>(item +"}");
|
||||
var ipEndPoint = IPEndPoint.Parse(e.IpPort);
|
||||
_action(ipEndPoint, msg, this);
|
||||
}
|
||||
}
|
||||
|
||||
private void Events_ClientDisconnected(object sender, ClientDisconnectedEventArgs e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void Events_ClientConnected(object sender, ClientConnectedEventArgs e)
|
||||
{
|
||||
Debug.WriteLine("有新客户端连接" + e.IpPort);
|
||||
}
|
||||
|
||||
public void Send(byte[] dgram, int bytes, IPEndPoint endPoint)
|
||||
{
|
||||
if(dgram == null || !dgram.Any())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var item in socket.GetClients())
|
||||
{
|
||||
//try
|
||||
//{
|
||||
if(socket.IsConnected(item) == false)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
socket.Send(item, dgram);
|
||||
//}
|
||||
//catch(Exception e)
|
||||
//{
|
||||
// Log.Error($"{ item.ToString() }:{ e.Message }\r\n{ e.StackTrace }");
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
public void Close()
|
||||
{
|
||||
_stop = true;
|
||||
socket.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
using NetCoreServer;
|
||||
using OnlineUserPool.Model;
|
||||
using Serilog;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
|
||||
namespace OnlineUserPool.Services
|
||||
{
|
||||
public class TcpService1 : IService
|
||||
{
|
||||
private PfTcpServer _server;
|
||||
|
||||
public void Close()
|
||||
{
|
||||
//throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void RunServer(Action<IPEndPoint, MsgModel, IService> action)
|
||||
{
|
||||
//throw new NotImplementedException();
|
||||
//_action = action;
|
||||
_server = new PfTcpServer(IPAddress.Any, 11001, (ip, model)=> {
|
||||
action(ip, model, this);
|
||||
});
|
||||
//_server.OptionNoDelay = true;
|
||||
_server.Start();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void Send(byte[] dgram, int bytes, IPEndPoint endPoint)
|
||||
{
|
||||
if (dgram == null || !dgram.Any())
|
||||
{
|
||||
return;
|
||||
}
|
||||
_server.Multicast(dgram);
|
||||
}
|
||||
|
||||
class PfTcpServer : TcpServer
|
||||
{
|
||||
Action<IPEndPoint, MsgModel> _action;
|
||||
public PfTcpServer(IPAddress address, int port, Action<IPEndPoint, MsgModel> action) :base(address, port)
|
||||
{
|
||||
_action = action;
|
||||
}
|
||||
|
||||
protected override TcpSession CreateSession()
|
||||
{
|
||||
//return base.CreateSession()
|
||||
return new PfTcpSession(this, _action);
|
||||
}
|
||||
|
||||
protected override void OnError(SocketError error)
|
||||
{
|
||||
//base.OnError(error);
|
||||
Debug.WriteLine(error);
|
||||
}
|
||||
}
|
||||
|
||||
class PfTcpSession : TcpSession
|
||||
{
|
||||
Action<IPEndPoint, MsgModel> _action;
|
||||
public PfTcpSession(TcpServer server, Action<IPEndPoint, MsgModel> action) : base(server) {
|
||||
_action = action;
|
||||
}
|
||||
|
||||
protected override void OnConnected()
|
||||
{
|
||||
//base.OnConnected();
|
||||
Debug.WriteLine("有新的Tcp连接");
|
||||
}
|
||||
|
||||
protected override void OnDisconnected()
|
||||
{
|
||||
//base.OnDisconnected();
|
||||
Debug.WriteLine("Tcp断开连接");
|
||||
}
|
||||
|
||||
protected override void OnReceived(byte[] buffer, long offset, long size)
|
||||
{
|
||||
//base.OnReceived(buffer, offset, size);
|
||||
var returnData = Encoding.UTF8.GetString(buffer, (int)offset, (int)size);
|
||||
//Debug.WriteLine(returnData);
|
||||
//Log.Information(returnData + "\r\n");
|
||||
try
|
||||
{
|
||||
//var msg = Newtonsoft.Json.JsonConvert.DeserializeObject<MsgModel>(returnData);
|
||||
//var ipEndPoint = IPEndPoint.Parse(this.Socket.RemoteEndPoint.ToString());
|
||||
//_action(ipEndPoint, msg);
|
||||
|
||||
foreach (var item in returnData.Split(new string[] { "}" }, StringSplitOptions.RemoveEmptyEntries))
|
||||
{
|
||||
//if (string.IsNullOrWhiteSpace(item)) continue;
|
||||
var msg = Newtonsoft.Json.JsonConvert.DeserializeObject<MsgModel>(item + "}");
|
||||
var ipEndPoint = IPEndPoint.Parse(this.Socket.RemoteEndPoint.ToString());
|
||||
_action(ipEndPoint, msg);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error($"接受到的数据处理时出错:{ returnData }\r\n{ ex.Message }\r\n{ ex.StackTrace }");
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnError(SocketError error)
|
||||
{
|
||||
//base.OnError(error);
|
||||
Debug.WriteLine(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
using OnlineUserPool.Model;
|
||||
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);
|
||||
}
|
||||
|
||||
public void RunServer(Action<IPEndPoint, MsgModel, IService> action)
|
||||
{
|
||||
udpServer = new UdpClient(ConfigHelp.Port);
|
||||
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);
|
||||
var msg = Newtonsoft.Json.JsonConvert.DeserializeObject<MsgModel>(returnData);
|
||||
|
||||
//lock (locker)
|
||||
{
|
||||
#if DEBUG
|
||||
WriteLine($"本次接收:{ remoteIpEndPoint.Address.ToString() }:{ remoteIpEndPoint.Port }收到消息:{ returnData }");
|
||||
#endif
|
||||
action(remoteIpEndPoint, msg, this);
|
||||
|
||||
//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)
|
||||
{
|
||||
udpServer.Send(dgram, bytes, endPoint);
|
||||
}
|
||||
|
||||
public void Close()
|
||||
{
|
||||
_stop = true;
|
||||
udpServer.Close();
|
||||
}
|
||||
|
||||
void WriteLine(string str)
|
||||
{
|
||||
//Debug.WriteLine(str);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user