提交 fe94aadc 编写于 作者: T tanghai

整理了网络层代码,删除一些废代码

上级 a34d2825
......@@ -17,6 +17,7 @@ namespace Base
public abstract class AChannel: IDisposable
{
public long Id { get; private set; }
protected AService service;
public string RemoteAddress { get; protected set; }
......@@ -35,7 +36,7 @@ namespace Base
}
}
public void OnError(AChannel channel, SocketError e)
protected void OnError(AChannel channel, SocketError e)
{
this.errorCallback(channel, e);
}
......
......@@ -30,14 +30,6 @@ namespace Base
public abstract void Update();
public Action<AChannel, SocketError> OnError;
protected void OnChannelError(AChannel channel, SocketError error)
{
this.OnError?.Invoke(channel, error);
this.Remove(channel.Id);
}
public abstract void Dispose();
}
}
\ No newline at end of file
......@@ -63,7 +63,6 @@ namespace Base
TSocket socket = new TSocket(this.poller);
await this.acceptor.AcceptAsync(socket);
TChannel channel = new TChannel(socket, this);
channel.ErrorCallback += this.OnChannelError;
this.idChannels[channel.Id] = channel;
return channel;
}
......@@ -72,7 +71,6 @@ namespace Base
{
TSocket newSocket = new TSocket(this.poller);
TChannel channel = new TChannel(newSocket, host, port, this);
channel.ErrorCallback += this.OnChannelError;
this.idChannels[channel.Id] = channel;
return channel;
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Threading.Tasks;
namespace Base
......@@ -21,6 +22,7 @@ namespace Base
this.RemoteAddress = host + ":" + port;
this.socket.ConnectAsync(host, (ushort)port);
this.socket.Received += this.OnRecv;
this.socket.Disconnect += () => { this.OnError(this, SocketError.SocketError); };
}
/// <summary>
......@@ -32,6 +34,7 @@ namespace Base
this.service = service;
this.RemoteAddress = socket.RemoteAddress;
this.socket.Received += this.OnRecv;
this.socket.Disconnect += () => { this.OnError(this, SocketError.SocketError); };
}
public override void Dispose()
......@@ -82,8 +85,9 @@ namespace Base
private void OnRecv()
{
this.recvTcs?.SetResult(this.socket.RecvQueue.Dequeue());
var tcs = this.recvTcs;
this.recvTcs = null;
tcs?.SetResult(this.socket.RecvQueue.Dequeue());
}
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Threading.Tasks;
namespace Base
......@@ -53,7 +52,6 @@ namespace Base
{
USocket socket = await this.poller.AcceptAsync();
UChannel channel = new UChannel(socket, this);
socket.Disconnect += () => this.OnChannelError(channel, SocketError.SocketError);
this.idChannels[channel.Id] = channel;
return channel;
}
......@@ -62,7 +60,6 @@ namespace Base
{
USocket newSocket = new USocket(this.poller);
UChannel channel = new UChannel(newSocket, host, port, this);
newSocket.Disconnect += () => this.OnChannelError(channel, SocketError.SocketError);
this.idChannels[channel.Id] = channel;
return channel;
}
......
......@@ -18,8 +18,8 @@ namespace Base
private readonly Queue<byte[]> recvQueue = new Queue<byte[]>();
private readonly Queue<BufferInfo> sendQueue = new Queue<BufferInfo>();
private bool isConnected;
public Action disconnect;
public Action received;
private Action disconnect;
private Action received;
public event Action Received
{
......
......@@ -26,10 +26,10 @@ namespace Model
public class NetworkComponent: Component
{
public AService Service;
private AService Service;
public Dictionary<long, Entity> sessions = new Dictionary<long, Entity>();
public Dictionary<string, Entity> adressSessions = new Dictionary<string, Entity>();
private readonly Dictionary<long, Entity> sessions = new Dictionary<long, Entity>();
private readonly Dictionary<string, Entity> adressSessions = new Dictionary<string, Entity>();
public void Awake(NetworkProtocol protocol)
{
......@@ -81,13 +81,13 @@ namespace Model
}
}
public void Add(Entity session)
private void Add(Entity session)
{
this.sessions.Add(session.Id, session);
this.adressSessions.Add(session.GetComponent<MessageComponent>().RemoteAddress, session);
}
public void Remove(long id)
private void Remove(long id)
{
Entity session;
if (!this.sessions.TryGetValue(id, out session))
......@@ -122,7 +122,6 @@ namespace Model
session.AddComponent<MessageComponent, AChannel>(channel);
this.Add(session);
return session;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册