📄 socketbase.cs
字号:
using System;
namespace SocketLibrary
{
/// <summary>
/// SocketBase 的摘要说明。
/// </summary>
public class SocketBase
{
public class MessageEventArgs:EventArgs {
public Message Message;
public Connection Connecction;
public MessageEventArgs(Message message,Connection connection) {
this.Message = message;
this.Connecction = connection;
}
}
public delegate void MessageEventHandler(object sender,MessageEventArgs e);
public class ConnectionEventArgs:EventArgs {
public Connection Connection;
public Exception Exception;
public ConnectionEventArgs(Connection connection,Exception exception) {
this.Connection = connection;
this.Exception = exception;
}
}
public delegate void ConnectionHandler(object sender,ConnectionEventArgs e);
public ConnectionCollection Connections {
get{return _connections;}
set{_connections = value;}
}
protected ConnectionCollection _connections;
public MessageCollection messageQueue {
get{return _messageQueue;}
set{_messageQueue = value;}
}
protected MessageCollection _messageQueue;
public SocketBase()
{
this._connections = new ConnectionCollection();
this._messageQueue = new MessageCollection();
}
protected void Send(Message message) {
this.Send(message,message.SendToOrReceivedFrom);
}
protected void Send(Message message,Connection connection) {
byte[] buffer = message.ToBytes();
lock(this) {
connection.NetworkStream.Write(buffer,0,buffer.Length);
}
}
protected System.Threading.Thread _listenningthread;
protected System.Threading.Thread _sendthread;
protected virtual void Sendding() {
try {
while(true) {
System.Threading.Thread.Sleep(200);
for(int i = 0 ; i < this.messageQueue.Count ; i++) {
if(this.messageQueue[i].SendToOrReceivedFrom != null) {
this.Send(this.messageQueue[i]);
this.OnMessageSent(this,new MessageEventArgs(this.messageQueue[i],this.messageQueue[i].SendToOrReceivedFrom));
}
else {//对每一个连接都发送此消息
for(int j = 0 ; j < this.Connections.Count ; j++) {
this.Send(this.messageQueue[i],this.Connections[j]);
this.OnMessageSent(this,new MessageEventArgs(this.messageQueue[i],this.Connections[j]));
}
}
this.messageQueue[i].Sent = true;
}
//清除所有已发送消息
for(int i = this.messageQueue.Count - 1 ; i > -1 ; i--) {
if(this.messageQueue[i].Sent)
this.messageQueue.RemoveAt(i);
}
}
}catch{
}
}
protected virtual void Listenning() {
try {
while(true) {
System.Threading.Thread.Sleep(200);
foreach(Connection connection in this._connections) {
if(connection.NetworkStream.CanRead && connection.NetworkStream.DataAvailable) {
try {
Message message = Message.Parse(connection);
this.OnMessageReceived(this,new MessageEventArgs(message,connection));
}
catch(Exception ex) {
connection.NetworkStream.Close();
this.OnConnectionClose(this,new ConnectionEventArgs(connection,ex));
}
}
}
}
}
catch {
}
}
protected void StartListenAndSend() {
_listenningthread = new System.Threading.Thread(new System.Threading.ThreadStart(Listenning));
_listenningthread.Start();
_sendthread = new System.Threading.Thread(new System.Threading.ThreadStart(Sendding));
_sendthread.Start();
}
public void EndListenAndSend() {
_listenningthread.Abort();
_sendthread.Abort();
}
public event MessageEventHandler MessageReceived;
public event MessageEventHandler MessageSent;
public event ConnectionHandler ConnectionClose;
public event ConnectionHandler Connected;
public void OnMessageReceived(object sender,MessageEventArgs e) {
if(MessageReceived != null)
this.MessageReceived(sender,e);
}
public void OnMessageSent(object sender,MessageEventArgs e) {
if(MessageSent != null)
this.MessageSent(sender,e);
}
public void OnConnectionClose(object sender,ConnectionEventArgs e) {
if(ConnectionClose != null)
this.ConnectionClose(sender,e);
}
public void OnConnected(object sender,ConnectionEventArgs e) {
if(Connected != null)
this.Connected(sender,e);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -