private void _ProcEstablishEvent(LPNetEvent netEvent) { LPSocker socker = null; LPEstablishNetEvent establishEvent = null; if (LOG_ERROR(netEvent != null)) { goto Exit0; } establishEvent = (LPEstablishNetEvent)netEvent; socker = establishEvent.Socker; if (socker.IsAcceptCreate) { m_NetMessageHandler.OnAccepted(socker); } else { m_NetMessageHandler.OnConnected(socker); } socker.PostRecv(); Exit0: return; }
void OnGTLoginAck(LPSocker socker, byte[] buf, int length) { bool result = false; T2C_LOGIN_ACK msg = null; msg = new T2C_LOGIN_ACK(); if (LOG_ERROR(msg != null)) { goto Exit0; } result = m_RecvMessageSerializer.Init(null, 0, buf, length); if (LOG_ERROR(result)) { goto Exit0; } result = msg.UnSerialize(m_RecvMessageSerializer); if (LOG_ERROR(result)) { goto Exit0; } LP.Logger.IMP("recv gateserver login ack, login successfully !"); LP.Logger.IMP("T2C_LOGIN_ACK(byValue={0},wValue={1},dwValue={2},qwValue={3},szValue={4})", msg.byValue, msg.wValue, msg.dwValue, msg.qwValue, msg.sValue); Exit0: result = m_RecvMessageSerializer.UnInit(); LOG_ERROR(result); return; }
public void OnConnect(IAsyncResult ar) { bool result = false; LPSocker socker = null; LPConnector connector = null; HandlerParam handlerParam = null; try { if (LOG_ERROR(ar != null)) { goto Exit0; } handlerParam = (HandlerParam)ar.AsyncState; if (LOG_ERROR(handlerParam != null)) { goto Exit0; } if (LOG_ERROR(handlerParam.HandlerType == EHandlerType.Connector)) { goto Exit0; } connector = (LPConnector)handlerParam.Handler; LP.Logger.IMP("connect success."); connector.Client.EndConnect(ar); socker = LP.NetModule.CreateSocker(connector.Client.Client, connector.PacketParser, connector, false); if (LOG_ERROR(socker != null)) { goto Exit0; } result = LP.NetModule.EventMgr.PushEstablishEvent(socker); if (LOG_ERROR(result)) { goto Exit0; } } catch (SocketException e) { //if(!connector.IsStoped) { LP.Logger.P_ERR("ErrorCode={0},SocketErrorCode={1}, Message:{2}, StackTrace:{3}", e.ErrorCode, e.SocketErrorCode, e.Message, e.StackTrace); LP.NetModule.EventMgr.PushConnectorErrorEvent(connector, e.ErrorCode); } } catch (Exception e) { LPMiniDump.GenerateNormalDump(e); } Exit0: return; }
internal void DelayClose(LPSocker socker) { if (LOG_ERROR(socker != null)) { goto Exit0; } lock (m_DelayLocker) { socker.DelayCloseBeginTick = DateTime.Now.Ticks; if (socker.IsPassiveClosed) { //被动关闭,无需延迟close socket socker.DelayCloseDuration = 0; } else { //主动关闭,需要等未发送完的数据发送完 socker.DelayCloseDuration = 3000; } m_DelayCloseList.PushRear(socker); } Exit0: return; }
internal void DelayRelease(LPSocker socker) { if (LOG_ERROR(socker != null)) { goto Exit0; } lock (m_DelayLocker) { socker.DelayReleaseBeginTick = DateTime.Now.Ticks; if (socker.IsPassiveClosed) { //被动关闭,无需延迟close socket socker.DelayReleaseDuration = 0; } else { //主动关闭,需要延迟释放,因为socker的close操作只关闭了发, //如果不延迟释放,可能在收的过程访问到被释放的socker socker.DelayReleaseDuration = 1500; } m_DelayReleaseList.PushRear(socker); } Exit0: return; }
internal bool PushTerminateEvent(LPSocker socker) { bool result = false; LPTerminateNetEvent terminateEvent = null; if (LOG_ERROR(socker != null)) { goto Exit0; } terminateEvent = (LPTerminateNetEvent)_CreateEvent(ENetEventType.Terminate); if (LOG_ERROR(terminateEvent != null)) { goto Exit0; } terminateEvent.Flag = socker.ID; terminateEvent.Socker = socker; lock (m_EventListLocker[terminateEvent.Flag % m_EventListCount]) { result = m_EventLists[terminateEvent.Flag % m_EventListCount].PushRear(terminateEvent); if (LOG_ERROR(result)) { goto Exit0; } } return(true); Exit0: return(false); }
internal bool PushEstablishEvent(LPSocker socker) { bool result = false; LPEstablishNetEvent establishEvent = null; if (LOG_ERROR(socker != null)) { goto Exit0; } establishEvent = (LPEstablishNetEvent)_CreateEvent(ENetEventType.Establish); if (LOG_ERROR(establishEvent != null)) { goto Exit0; } establishEvent.Flag = socker.ID; establishEvent.Socker = socker; lock (m_EventListLocker[establishEvent.Flag % m_EventListCount]) { result = m_EventLists[establishEvent.Flag % m_EventListCount].PushRear(establishEvent); if (LOG_ERROR(result)) { goto Exit0; } } return(true); Exit0: return(false); }
private void _ProcTerminateEvent(LPNetEvent netEvent) { LPSocker socker = null; LPConnector connector = null; LPTerminateNetEvent terminateEvent = null; if (LOG_ERROR(netEvent != null)) { goto Exit0; } terminateEvent = (LPTerminateNetEvent)netEvent; socker = terminateEvent.Socker; if (!socker.IsAcceptCreate) { connector = (LPConnector)socker.Parent; m_NetMessageHandler.OnConnectDisconnected(socker, connector); } else { m_NetMessageHandler.OnDisconnected(socker); } socker.OnClose(); Exit0: return; }
public void OnSend(IAsyncResult ar) { bool result = false; int sendLen = 0; LPSocker socker = null; SocketError socketError = SocketError.SocketError; HandlerParam handlerParam = null; try { if (LOG_ERROR(ar != null)) { goto Exit0; } handlerParam = (HandlerParam)ar.AsyncState; if (LOG_ERROR(handlerParam != null)) { goto Exit0; } if (LOG_ERROR(handlerParam.HandlerType == EHandlerType.Socker)) { goto Exit0; } socker = (LPSocker)handlerParam.Handler; sendLen = socker.Sock.EndSend(ar, out socketError); if (sendLen <= 0 || socketError != SocketError.Success) { result = LP.NetModule.EventMgr.PushTerminateEvent(socker); if (LOG_ERROR(result)) { goto Exit0; } } else { socker.SendBuf.FinishRead(sendLen); socker.IsSending = false; } } catch (SocketException e) { LP.Logger.P_ERR("ErrorCode={0},SocketErrorCode={1}, Message:{2}, StackTrace:{3}", e.ErrorCode, e.SocketErrorCode, e.Message, e.StackTrace); } catch (Exception e) { LPMiniDump.GenerateNormalDump(e); } Exit0: return; }
//////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// bool DoGTLoginReq(LPSocker socker) { bool result = false; C2T_LOGIN_REQ msg = null; msg = new C2T_LOGIN_REQ(); if (LOG_ERROR(msg != null)) { goto Exit0; } msg.MsgId = (UInt16)C2T_MESSAGE_ID.c2t_login_req; msg.byValue = 1; msg.wValue = 2; msg.dwValue = 3; msg.qwValue = 4; msg.sValue = "I'm client, i want to login !"; result = m_SendMessageSerializer.Init(null, 0, null, 0); if (LOG_ERROR(result)) { goto Exit0; } result = msg.Serialize(m_SendMessageSerializer); if (LOG_ERROR(result)) { goto Exit0; } result = socker.Send(m_SendMessageSerializer.GetSerializeBuf(), 0, m_SendMessageSerializer.GetSerializeSize()); if (LOG_ERROR(result)) { goto Exit0; } result = m_SendMessageSerializer.UnInit(); if (LOG_ERROR(result)) { goto Exit0; } return(true); Exit0: result = m_SendMessageSerializer.UnInit(); if (LOG_ERROR(result)) { goto Exit0; } return(false); }
public void OnMessage(LPSocker socker, byte[] buf, int size) { int Index = 0; UInt16 MsgId = 0; UInt16 MsgSize = 0; int MsgHeadSize = 4; LP.Logger.IMP("OnMessage output : socker id is {0}", socker.ID); if (LOG_ERROR(socker != null)) { goto Exit0; } if (LOG_ERROR(buf != null)) { goto Exit0; } if (LOG_ERROR(size >= MsgHeadSize)) { goto Exit0; } MsgId = (UInt16)(buf[Index++] | buf[Index++]); if (LOG_ERROR(MsgId > LPProtocol.min_message_id && MsgId < LPProtocol.max_message_id)) { goto Exit0; } MsgSize = (UInt16)(buf[Index++] | buf[Index++]); if (LOG_ERROR(MsgSize == size)) { goto Exit0; } if (LOG_ERROR(m_MessageCallBackList[MsgId] != null)) { goto Exit0; } try { m_MessageCallBackList[MsgId](socker, buf, size); } catch (Exception e) { LPMiniDump.GenerateNormalDump(e); } Exit0: return; }
public void OnConnected(LPSocker socker) { bool result = false; LP.Logger.IMP("OnConnected output : socker id is {0}", socker.ID); result = DoGTLoginReq(socker); if (LOG_ERROR(result)) { goto Exit0; } Exit0: return; }
private void _PostSendThreadProc() { int sendDataSockerCount = 0; LPSocker socker = null; BASE_LIST_NODE node = null; BASE_LIST_NODE nextNode = null; while (m_CheckDelayRun) { sendDataSockerCount = 0; if (m_ValidSockerList.IsEmpty()) { Thread.Sleep(1); continue; } lock (m_ValidListLocker) { node = m_ValidSockerList.Head.Next; while (node != null && node != m_ValidSockerList.Rear) { nextNode = node.Next; socker = (LPSocker)node.Data; if (socker == null) { LOG_ERROR(false); node = nextNode; continue; } if (socker.PostSend()) { ++sendDataSockerCount; } node = nextNode; } } if (sendDataSockerCount > 0) { Thread.Sleep(1); } } return; }
public void OnDisconnected(LPSocker socker) { LP.Logger.IMP("OnDisconnected output : socker id is {0}", socker.ID); }
private void _CheckDelayThreadProc() { bool result = false; long ticks = 0; LPSocker socker = null; BASE_LIST_NODE node = null; BASE_LIST_NODE nextNode = null; while (m_CheckDelayRun) { Thread.Sleep(5); ticks = DateTime.Now.Ticks; lock (m_DelayLocker) { //检查延迟的关闭连接 node = m_DelayCloseList.Head.Next; while (node != null && node != m_DelayCloseList.Rear) { nextNode = node.Next; socker = (LPSocker)node.Data; if (socker == null) { LOG_ERROR(false); node = nextNode; continue; } if (ticks >= socker.DelayCloseBeginTick + socker.DelayCloseDuration) { socker.Sock.Close(); result = m_DelayCloseList.Remove(node); LOG_ERROR(result); DelayRelease(socker); } node = nextNode; } //检查延迟释放的连接 node = m_DelayReleaseList.Head.Next; while (node != null && node != m_DelayReleaseList.Rear) { nextNode = node.Next; socker = (LPSocker)node.Data; if (socker == null) { LOG_ERROR(false); node = nextNode; continue; } if (ticks >= socker.DelayReleaseBeginTick + socker.DelayReleaseDuration) { result = m_DelayReleaseList.Remove(node); LOG_ERROR(result); LP.NetModule.FreeSocker(socker); } node = nextNode; } } } return; }
internal LPSocker CreateSocker(Socket socket, ILPPacketParser packetParser, object parent, bool isAcceptCreate) { bool result = false; LPSocker socker = null; if (LOG_ERROR(socket != null)) { goto Exit0; } if (LOG_ERROR(packetParser != null)) { goto Exit0; } socker = new LPSocker(); if (LOG_ERROR(packetParser != null)) { goto Exit0; } socker.ID = _GenerateSockerID(); socker.Sock = socket; socker.Parent = parent; socker.IsAcceptCreate = isAcceptCreate; socker.IsClosed = false; socker.IsSending = false; socker.PacketParser = packetParser; socker.RecvBuf = new LPLoopBuf(); if (LOG_ERROR(socker.RecvBuf != null)) { goto Exit0; } result = socker.RecvBuf.Init(LP.RecvBufSize); if (LOG_ERROR(result)) { goto Exit0; } socker.SendBuf = new LPLoopBuf(); if (LOG_ERROR(socker.SendBuf != null)) { goto Exit0; } result = socker.SendBuf.Init(LP.SendBufSize); if (LOG_ERROR(result)) { goto Exit0; } lock (m_ValidListLocker) { result = m_ValidSockerList.PushRear(socker); if (LOG_ERROR(result)) { goto Exit0; } } return(socker); Exit0: return(null); }
public void OnConnectDisconnected(LPSocker socker, LPConnector connector) { LP.Logger.IMP("OnConnectDisconnected output : socker id is {0}, connector id is {1}", socker.ID, connector.ID); }
internal bool PushRecvEvent(LPSocker socker, LPLoopBuf recvLoopBuf, int len) { bool result = false; int retryCount = 0; int lineSize = 0; LPRecvNetEvent recvEvent = null; if (LOG_ERROR(socker != null)) { goto Exit0; } if (LOG_ERROR(recvLoopBuf != null)) { goto Exit0; } recvEvent = (LPRecvNetEvent)_CreateEvent(ENetEventType.Recv); if (LOG_ERROR(recvEvent != null)) { goto Exit0; } while (m_EventBuf.GetTotalWritableLen() < len) { LP.Logger.P_WRN("event buf not enough, sleep and try again !"); Thread.Sleep(1); ++retryCount; // 超过10秒,丢弃 if (retryCount > 10000) { recvLoopBuf.FinishRead(len); if (LOG_ERROR(false)) { goto Exit0; } } } lineSize = recvLoopBuf.GetOnceReadableLen(); if (lineSize > len) { lineSize = len; } result = m_EventBuf.Write(recvLoopBuf.BufBytes, recvLoopBuf.ReadInx, lineSize); LOG_ERROR(result); recvLoopBuf.FinishRead(lineSize); if (lineSize < len) { result = m_EventBuf.Write(recvLoopBuf.BufBytes, recvLoopBuf.ReadInx, len - lineSize); LOG_ERROR(result); recvLoopBuf.FinishRead(len - lineSize); } recvEvent.Flag = socker.ID; recvEvent.RecvLength = len; recvEvent.Socker = socker; lock (m_EventListLocker[recvEvent.Flag % m_EventListCount]) { result = m_EventLists[recvEvent.Flag % m_EventListCount].PushRear(recvEvent); if (LOG_ERROR(result)) { goto Exit0; } } return(true); Exit0: return(false); }
public void OnAccept(IAsyncResult ar) { bool result = false; LPSocker socker = null; Socket socket = null; LPListener listener = null; HandlerParam handlerParam = null; IAsyncResult retAr = null; try { if (LOG_ERROR(ar != null)) { goto Exit0; } handlerParam = (HandlerParam)ar.AsyncState; if (LOG_ERROR(handlerParam != null)) { goto Exit0; } if (LOG_ERROR(handlerParam.HandlerType == EHandlerType.Listener)) { goto Exit0; } listener = (LPListener)handlerParam.Handler; LP.Logger.IMP("accept success."); socket = listener.Listener.EndAcceptSocket(ar); if (LOG_ERROR(socket != null)) { goto Exit0; } retAr = listener.Listener.BeginAcceptSocket(LP.NetModule.Reactor.OnAccept, handlerParam); if (LOG_ERROR(retAr != null)) { goto Exit0; } socker = LP.NetModule.CreateSocker(socket, listener.PacketParser, listener, true); if (LOG_ERROR(socker != null)) { goto Exit0; } result = LP.NetModule.EventMgr.PushEstablishEvent(socker); if (LOG_ERROR(result)) { goto Exit0; } } catch (SocketException e) { //if(!listener.IsStoped) { LP.Logger.P_ERR("ErrorCode={0},SocketErrorCode={1}, Message:{2}, StackTrace:{3}", e.ErrorCode, e.SocketErrorCode, e.Message, e.StackTrace); } } catch (Exception e) { LPMiniDump.GenerateNormalDump(e); } Exit0: return; }
internal bool FreeSocker(LPSocker socker) { return(true); }
public void OnReceive(IAsyncResult ar) { bool result = false; int recvLen = 0; int useLen = 0; LPSocker socker = null; HandlerParam handlerParam = null; SocketError socketError = SocketError.SocketError; try { if (LOG_ERROR(ar != null)) { goto Exit0; } handlerParam = (HandlerParam)ar.AsyncState; if (LOG_ERROR(handlerParam != null)) { goto Exit0; } if (LOG_ERROR(handlerParam.HandlerType == EHandlerType.Socker)) { goto Exit0; } socker = (LPSocker)handlerParam.Handler; recvLen = socker.Sock.EndReceive(ar, out socketError); if (recvLen <= 0 || socketError != SocketError.Success) { result = LP.NetModule.EventMgr.PushTerminateEvent(socker); if (LOG_ERROR(result)) { goto Exit0; } } else { socker.RecvBuf.FinishWrite(recvLen); while (socker.RecvBuf.GetTotalReadableLen() > 0) { useLen = socker.PacketParser.Parse(socker.RecvBuf); if (0 == useLen) { //接收区已满,但还没有装下一个包,只好断开连接 if (socker.RecvBuf.GetTotalWritableLen() <= 0) { socker.Close(ESockerCloseType.RecvError, 1, false); if (LOG_ERROR(false)) { goto Exit0; } } break; } else if (useLen > 0) { if (useLen > socker.RecvBuf.GetTotalReadableLen()) { socker.Close(ESockerCloseType.RecvError, 2, false); if (LOG_ERROR(false)) { goto Exit0; } } result = LP.NetModule.EventMgr.PushRecvEvent(socker, socker.RecvBuf, useLen); if (!result) { socker.Close(ESockerCloseType.RecvError, 3, false); if (LOG_ERROR(false)) { goto Exit0; } } } else { socker.Close(ESockerCloseType.RecvError, 4, false); if (LOG_ERROR(false)) { goto Exit0; } } } socker.PostRecv(); } } catch (SocketException e) { LP.Logger.P_ERR("ErrorCode={0},SocketErrorCode={1}, Message:{2}, StackTrace:{3}", e.ErrorCode, e.SocketErrorCode, e.Message, e.StackTrace); } catch (Exception e) { LPMiniDump.GenerateNormalDump(e); } Exit0: return; }
public void OnAccepted(LPSocker socker) { LP.Logger.IMP("OnAccepted output : socker id is {0}", socker.ID); }