/// <summary> /// 事件分发函数 /// </summary> /// <param name="O"></param> private void HandMSG(object O) { object[] vs = (object[])O; ConnToClient CTC = (ConnToClient)vs[0]; ProtocolBytes PB = (ProtocolBytes)vs[1]; try { object[] OB = PB.GetDecode(); string ProtoName = PB.Protocol.ToString(); string MethodName = ProtoName; try { MethodInfo MI = HCP.GetType().GetMethod(MethodName); object[] OS = new object[] { CTC, OB }; MI.Invoke(HCP, OS); } catch (Exception e) { Console.WriteLine("132" + e.Message + MethodName); } } catch (Exception e) { Console.WriteLine(e.Message + " HandMSG"); } }
private void ReceiveCb(IAsyncResult ar) { ConnToClient CTC = (ConnToClient)ar.AsyncState; try { if (CTC.IsUse == false) { return; } int count = CTC.socket.EndReceive(ar); if (count <= 0) { CTC.Close(); return; } CTC.BufferCount += count; PrcessByte(CTC); CTC.socket.BeginReceive(CTC.ReadBuffer, CTC.BufferCount, CTC.RetCount(), SocketFlags.None, ReceiveCb, CTC); } catch (Exception e) { CTC.Close(); Console.WriteLine(e.Message); } }
private void PrcessByte(ConnToClient CTC) { if (CTC.BufferCount < sizeof(Int32))//接收到消息小于包头长度 { return; } Array.Copy(CTC.ReadBuffer, CTC.Lenbyte, sizeof(Int32)); CTC.MsgLen = BitConverter.ToInt32(CTC.Lenbyte, 0); if (CTC.BufferCount < CTC.MsgLen + sizeof(Int32)) { return; } ProtocolBytes Pb = (ProtocolBytes)PB.Decode(CTC.ReadBuffer, sizeof(Int32), CTC.MsgLen); object[] vs = new object[] { CTC, Pb }; object O = vs; Thread HandMSGT = new Thread(new ParameterizedThreadStart(HandMSG)); HandMSGT.Start(O); int count = CTC.BufferCount - CTC.MsgLen - sizeof(Int32); Array.Copy(CTC.ReadBuffer, sizeof(Int32) + CTC.MsgLen, CTC.ReadBuffer, 0, count); CTC.BufferCount = count; if (CTC.BufferCount > 0) { PrcessByte(CTC); } }
private void AcceptCb(IAsyncResult ar) { Socket RetCSocket = socket.EndAccept(ar); int Index = NewIndex(); SCTCS[Index] = new ConnToClient(); ConnToClient CTC = SCTCS[Index]; ClientOnlineNumber++; CTC.Init(RetCSocket); CTC.socket.BeginReceive(CTC.ReadBuffer, CTC.BufferCount, CTC.RetCount(), SocketFlags.None, ReceiveCb, CTC); socket.BeginAccept(AcceptCb, null); }
public void Hearbeat() { long TimeNow = Sys.GetListTime(); for (int i = 0; i < SCTCS.Length; i++) { if (SCTCS[i] == null) { continue; } ConnToClient CTC = SCTCS[i]; if (!CTC.IsUse) { continue; } if (CTC.LastHearBeatTime < TimeNow - HearbeatTime) { lock (CTC) CTC.Close(); } } }
public ConnToClient CheckCTC(ConnToClient CTC) { for (int i = 0; i < SCTCS.Length; i++) { if (SCTCS == null) { continue; } if (SCTCS[i] == null) { continue; } if (!SCTCS[i].IsUse) { continue; } if (SCTCS[i] == CTC) { return(SCTCS[i]); } } return(null); }
public void Send(ConnToClient CTC, ProtocolBytes bytes) { CTC.Send(bytes); }