You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
150 lines
4.5 KiB
150 lines
4.5 KiB
using System;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Net;
|
|
|
|
using Sog;
|
|
using Sog.Log;
|
|
using Sog.IO;
|
|
|
|
using Google.Protobuf.WellKnownTypes;
|
|
using Google.Protobuf;
|
|
using System.Diagnostics;
|
|
using System.Net.Sockets;
|
|
|
|
namespace SMAgent
|
|
{
|
|
/*
|
|
|
|
*/
|
|
public class SMAgentNet : Singleton<SMAgentNet>
|
|
{
|
|
private ClientSession m_client;
|
|
|
|
private MsgHandler m_handler = new MsgHandler();
|
|
|
|
private long m_lostTime;
|
|
|
|
public void Start()
|
|
{
|
|
SMProtoRegister.Instance.RegisterPacket();
|
|
|
|
string[] ipport = SMAgentUtils.Config.centerip.Split(':');
|
|
|
|
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(ipport[0]), int.Parse(ipport[1]));
|
|
|
|
ClientSessionSettings clientSettings = new ClientSessionSettings(1024000, endPoint,ProtocolType.Tcp);
|
|
|
|
m_client = new TcpClientSession(clientSettings, SessionNetSelectMode.Synchronous);
|
|
m_client.DataReceived += this.OnDataReceived;
|
|
m_client.Connected += this.OnConnected;
|
|
m_client.Disconnected += this.OnDisConnected;
|
|
m_client.ConnectFail += this.OnConnectFail;
|
|
|
|
m_client.Connect();
|
|
}
|
|
|
|
public bool IsConnected()
|
|
{
|
|
return m_client.IsConnect();
|
|
}
|
|
|
|
public void Update(long nowMs)
|
|
{
|
|
m_client.Update(nowMs);
|
|
|
|
if(m_lostTime > 0 && nowMs - m_lostTime >= 2000)
|
|
{
|
|
m_lostTime = 0;
|
|
m_client.Reconnect();
|
|
}
|
|
}
|
|
|
|
void OnConnected(IMessageTrans messageTrans, SessionEventArgs e)
|
|
{
|
|
TraceLog.Trace("SMAgentNet.OnConnected");
|
|
|
|
SMClientRegReq reg = new SMClientRegReq();
|
|
reg.ClientType = 1;
|
|
reg.MachineName = SMAgentUtils.HostName;
|
|
SendMsgToCenter(reg, SMMsgID.ClientRegReq);
|
|
}
|
|
|
|
void OnDisConnected(IMessageTrans messageTrans, SessionEventArgs e)
|
|
{
|
|
TraceLog.Trace("SMAgentNet.OnDisConnected");
|
|
|
|
m_lostTime = SMAgentUtils.GetTimeMs();
|
|
}
|
|
|
|
void OnConnectFail(IMessageTrans messageTrans, SessionEventArgs e)
|
|
{
|
|
TraceLog.Trace("SMAgentNet.OnConnectFail");
|
|
|
|
m_lostTime = SMAgentUtils.GetTimeMs();
|
|
}
|
|
|
|
void OnDataReceived(IMessageTrans messageTrans, SessionEventArgs e)
|
|
{
|
|
MessageData message = e.Message;
|
|
|
|
RequestPacket packet;
|
|
bool result = ProtoPackerFactory.Instance.GetProtoBufPacker().UnpackMessage(message, out packet);
|
|
if (result)
|
|
{
|
|
Google.Protobuf.IMessage pbMessage = (Google.Protobuf.IMessage)(packet.Message);
|
|
|
|
//这个包太大,不能打印
|
|
if (message.Header.Type == (int)SMMsgID.FileContentRes)
|
|
{
|
|
TraceLog.Trace("recv from center msgId {0} length {1} message: {2}"
|
|
, packet.MsgID, message.Header.Length, packet.Message.GetType().Name);
|
|
}
|
|
else
|
|
{
|
|
TraceLog.Trace("recv from center msgId {0} length {1} message: {2} -> {3}"
|
|
, packet.MsgID, message.Header.Length, packet.Message.GetType().Name, pbMessage.ToString());
|
|
}
|
|
}
|
|
|
|
m_handler.HandlerMsg(packet);
|
|
}
|
|
|
|
public void SendMsgToCenter(IMessage pbMessage, SMMsgID msgId)
|
|
{
|
|
RequestPacket packet = new RequestPacket((int)msgId, 0, 0);
|
|
packet.Message = pbMessage;
|
|
|
|
MessageData message = new MessageData();
|
|
message.Header.Type = (int)msgId;
|
|
message.Header.Length = 0;
|
|
message.Header.ObjectID = 0;
|
|
message.Header.ServerID = 0;
|
|
|
|
|
|
bool bRet = ProtoPackerFactory.Instance.GetProtoBufPacker().PackMessage(packet, ref message);
|
|
if (bRet == false)
|
|
{
|
|
TraceLog.Error("PackMessage error");
|
|
return;
|
|
}
|
|
|
|
//这个包太大,不能打
|
|
if (msgId == SMMsgID.FileContentRes)
|
|
{
|
|
TraceLog.Trace("send to center msgId {0} length {1} message: {2}"
|
|
, (int)msgId, message.Header.Length, packet.Message.GetType().Name);
|
|
}
|
|
else
|
|
{
|
|
TraceLog.Trace("send to center msgId {0} length {1} message: {2} -> {3}"
|
|
, (int)msgId, message.Header.Length, packet.Message.GetType().Name, pbMessage.ToString());
|
|
}
|
|
|
|
|
|
m_client.Send(message);
|
|
|
|
}
|
|
}
|
|
}
|
|
|