📄 socketbase.cs
字号:
using System;
using System.Text;
namespace SmartDeviceApplication5
{
/// <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 class ConnectionEventArgs:EventArgs
{
public Connection Connection;
public Exception Exception;
public ConnectionEventArgs(Connection connection,Exception exception)
{
this.Connection = connection;
this.Exception = exception;
}
}
public delegate void MessageEventHandler(object sender,MessageEventArgs e);
public delegate void ConnectionHandler(object sender,ConnectionEventArgs e);
public int linewnum;
public int exitnum;
public ConnectionCollection Connections
{
get{return _connections;}
set{_connections = value;}
}
protected ConnectionCollection _connections;
public Connection Connection
{
get{return _connection;}
set{_connection = value;}
}
protected Connection _connection;
public MessageCollection messageQueue {
get{return _messageQueue;}
set{_messageQueue = value;}
}
protected MessageCollection _messageQueue;
public SocketBase()
{
this._connections = new ConnectionCollection();
this._messageQueue = new MessageCollection();
this._connection = new Connection();
this.linewnum =0;
this.exitnum =0;
}
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 )
{
if (this.exitnum == 1)
return;
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.Send(this.messageQueue[i],this.Connection);
this.OnMessageSent(this,new MessageEventArgs(this.messageQueue[i],this.Connection));
//}
}
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 )
{
if (this.exitnum == 1)
return;
System.Threading.Thread.Sleep(200);
//foreach(Connection connection in this._connections)
//{
if(this._connection.NetworkStream.CanRead && this._connection.NetworkStream.DataAvailable)
{
try
{
Message message = Message.Parse(this._connection);
this.OnMessageReceived(this,new MessageEventArgs(message,this._connection));
if ( message.MessageBody =="Exit")
this._connection.NetworkStream.Close();
}
catch(Exception ex)
{
this._connection.NetworkStream.Close();
this.OnConnectionClose(this,new ConnectionEventArgs(this._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()
{
this.exitnum = 1;
}
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 + -