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.
 
 
 
 
 
 

135 lines
3.9 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.Net.Sockets;
namespace SMConsole
{
public class SMConsoleNet : Singleton<SMConsoleNet>
{
private ClientSession m_client;
private MsgHandler m_handler = new MsgHandler();
private long m_lostTime;
public void Start()
{
SMProtoRegister.Instance.RegisterPacket();
string[] ipport = SMConsoleUtils.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 tMs)
{
m_client.Update(tMs);
if(m_lostTime > 0 && SMConsoleUtils.GetTimeSecond() - m_lostTime >= 5)
{
m_lostTime = 0;
m_client.Reconnect();
}
}
void OnConnected(IMessageTrans messageTrans, SessionEventArgs e)
{
TraceLog.Trace("OnConnected");
SMClientRegReq req = new SMClientRegReq();
req.ClientType = 2;
req.MachineName = "console_client";
SendMsgToCenter(req, SMMsgID.ClientRegReq);
}
void OnDisConnected(IMessageTrans messageTrans, SessionEventArgs e)
{
TraceLog.Trace("OnDisConnected");
m_lostTime = SMConsoleUtils.GetTimeSecond();
}
void OnConnectFail(IMessageTrans messageTrans, SessionEventArgs e)
{
TraceLog.Trace("OnConnectFail");
m_lostTime = SMConsoleUtils.GetTimeSecond();
}
void OnDataReceived(IMessageTrans messageTrans, SessionEventArgs e)
{
//return;
MessageData message = e.Message;
RequestPacket packet;
bool result = ProtoPackerFactory.Instance.GetProtoBufPacker().UnpackMessage(message, out packet);
if (result)
{
// log message
Google.Protobuf.IMessage pbMessage = (Google.Protobuf.IMessage)(packet.Message);
TraceLog.Trace("recv message from center, message type {0} length {1} : {2}"
, packet.Message.GetType().Name
, message.Header.Length
, pbMessage.ToString());
}
m_handler.HandlerMsg(packet);
}
public void SendMsgToCenter(IMessage pbMessage, SMMsgID iMsgID)
{
RequestPacket packet = new RequestPacket((int)iMsgID, 0,0);
packet.Message = pbMessage;
MessageData message = new MessageData();
message.Header.Type = (int)iMsgID;
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;
}
TraceLog.Trace("send to center msgId {0} length {1}, {2} -> {3}"
, iMsgID, message.Header.Length, packet.Message.GetType().Name
, pbMessage.ToString());
m_client.Send(message);
}
}
}