Expand|Select|Wrap|Line Numbers
- public class NetCore
- {
- private int MAX_CONNECTIONS = Settings.Default.MAX_CONNECTIONS;
- // ----
- public int m_Port;
- public int m_ActiveClients;
- public int i;
- public TcpClient[] m_Coneections;
- // ----
- private TcpListener m_Listner;
- private ProcessVar m_ListenVar;
- private ProcessVar m_PacketVar;
- // ----
- public NetCore(int iPort)
- {
- m_Port = iPort;
- // ----
- m_Coneections = new TcpClient[MAX_CONNECTIONS];
- m_ActiveClients = 0;
- // ----
- m_ListenVar.m_Thread = new Thread(ProcessListen);
- m_PacketVar.m_Thread = new Thread(ProcessPacket);
- // ----
- m_ListenVar.m_Idle = 1000;
- m_PacketVar.m_Idle = 1;
- }
- //--------------------------------------------------------------------------------
- // # Name : ProcessListen
- // # Desc : Listen to new Clients accsept them and call OnConnect
- // # Status : Finished
- //--------------------------------------------------------------------------------
- private void ProcessListen()
- {
- while (m_ListenVar.m_Factor)
- {
- if (m_Listner.Pending())
- {
- TcpClient Client = m_Listner.AcceptTcpClient();
- OnConnect(Client);
- }
- // ----
- Thread.Sleep(m_ListenVar.m_Idle);
- }
- }
- //--------------------------------------------------------------------------------
- // # Name : ProcessPacket
- // # Desc : get the packets and check if connection still alive
- // # Status : not found yet if client is connected or not
- //--------------------------------------------------------------------------------
- private void ProcessPacket()
- {
- byte[] Recv;
- // --------
- while (m_PacketVar.m_Factor)
- {
- if (m_ActiveClients > 0)
- {
- for (i = 0; i != m_ActiveClients; i++)
- {
- try
- {
- if (m_Coneections[i].Available > 0)
- {
- Recv = new byte[m_Coneections[i].Available];
- int ret = m_Coneections[i].Client.Receive(Recv, Recv.Length, );
- // ---
- if (ret > 0)
- {
- OnRecv(m_Coneections[i], Recv);
- }
- else
- {
- OnDisconnect(m_Coneections[i]);
- }
- }
- else if (!m_Coneections[i].Connected)
- {
- // ---
- OnDisconnect(m_Coneections[i]);
- continue;
- }
- }
- catch (Exception x)
- {
- OnDisconnect(m_Coneections[i]);
- }
- }
- }
- // ----
- Thread.Sleep(m_PacketVar.m_Idle);
- }
- }
- //--------------------------------------------------------------------------------
- public bool Create()
- {
- bool bResult = true;
- try
- {
- // ----
- m_Listner = new TcpListener(IPAddress.Any, m_Port);
- m_Listner.Start();
- // -----
- m_ListenVar.m_Factor = true;
- m_ListenVar.m_Thread.Start();
- // -----
- m_PacketVar.m_Factor = true;
- m_PacketVar.m_Thread.Start();
- // ----
- OnCreate();
- // -----
- bResult = true;
- }
- catch (Exception x)
- {
- OnCreate(x);
- // -----
- bResult = false;
- }
- // -----
- return bResult;
- }
- //--------------------------------------------------------------------------------
- public void Close()
- {
- OnClose();
- }
- //--------------------------------------------------------------------------------
- public virtual void OnConnect(TcpClient uClient)
- {
- m_Coneections[m_ActiveClients] = uClient;
- m_ActiveClients++;
- }
- //--------------------------------------------------------------------------------
- public virtual void OnDisconnect(TcpClient uClient)
- {
- m_ActiveClients--;
- }
- //--------------------------------------------------------------------------------
- public virtual void OnRecv(TcpClient uClient, byte[] bRecvBytes)
- {
- }
- public virtual void OnClose()
- {
- for (i = 0; i != m_ActiveClients; i++)
- {
- m_Coneections[i].Close();
- }
- // -----
- m_ListenVar.m_Factor = false;
- m_ListenVar.m_Thread.Abort();
- // -----
- m_PacketVar.m_Factor = false;
- m_PacketVar.m_Thread.Abort();
- // -----
- m_ActiveClients = 0;
- }
- //--------------------------------------------------------------------------------
- public virtual void OnCreate()
- {
- }
- //--------------------------------------------------------------------------------
- public virtual void OnCreate(Exception x)
- {
- }
- //--------------------------------------------------------------------------------
- private struct ProcessVar
- {
- public Thread m_Thread;
- public bool m_Factor;
- public int m_Idle;
- }
- }
Expand|Select|Wrap|Line Numbers
- //--------------------------------------------------------------------------------
- public class StateObject
- {
- public Socket workSocket = null;
- public const int BufferSize = 1024;
- public byte[] buffer = new byte[BufferSize];
- public StringBuilder sb = new StringBuilder();
- }
- //--------------------------------------------------------------------------------
- public class NetCore
- {
- // ------
- public int m_Port;
- public int m_maxQueue;
- // ------
- private Socket m_Listner;
- private IPEndPoint m_LocalEPoint;
- private ProcessVar m_ListenVar;
- private ManualResetEvent m_allDone;
- // ------
- private int m_iTemp;
- //--------------------------------------------------------------------------------
- public NetCore(int iPort)
- {
- // ----
- m_Port = iPort;
- // ----
- m_LocalEPoint = new IPEndPoint(IPAddress.Any, m_Port);
- m_Listner = new Socket(m_LocalEPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
- m_allDone = new ManualResetEvent(false);
- // ----
- m_ListenVar.m_Thread = new Thread(ProcessListen);
- //m_PacketVar.m_Thread = new Thread(ProcessPacket);
- // ----
- m_ListenVar.m_Idle = 1000;
- //m_PacketVar.m_Idle = 1;
- }
- //--------------------------------------------------------------------------------
- // # Name : Create
- // # Desc : Create the Listen Process , etc..
- // # Status : --
- //--------------------------------------------------------------------------------
- public bool Create()
- {
- bool bReturn = false;
- // ------
- try
- {
- // ------
- m_Listner.Bind(m_LocalEPoint);
- m_Listner.Listen(m_maxQueue);
- // ------
- m_ListenVar.m_Factor = true;
- m_ListenVar.m_Thread.Start();
- // ------
- OnCreate();
- // ------
- bReturn = true;
- }
- catch (SocketException x)
- {
- OnCreate(x);
- bReturn = false;
- }
- // ------
- return bReturn;
- }
- //--------------------------------------------------------------------------------
- // # Name : Close
- // # Desc : --
- // # Status : --
- //--------------------------------------------------------------------------------
- public void Close()
- {
- }
- //--------------------------------------------------------------------------------
- // # Name : ProcessListen
- // # Desc : Listen to new Clients accsept them and call OnConnect
- // # Status : Finished
- //--------------------------------------------------------------------------------
- private void ProcessListen()
- {
- while (m_ListenVar.m_Factor)
- {
- m_allDone.Reset();
- m_Listner.BeginAccept(new AsyncCallback(ProcessAccept), m_Listner);
- m_allDone.WaitOne();
- }
- }
- //--------------------------------------------------------------------------------
- private void ProcessAccept(IAsyncResult SyncResult)
- {
- m_allDone.Set();
- Socket Listener = (Socket)SyncResult.AsyncState;
- Socket Handler = Listener.EndAccept(SyncResult);
- OnConnect(Handler);
- StateObject StateObj = new StateObject();
- StateObj.workSocket = Handler;
- Handler.BeginReceive(StateObj.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ProcessRecv), StateObj);
- }
- //--------------------------------------------------------------------------------
- private void ProcessRecv(IAsyncResult SyncResult)
- {
- StateObject StateObj = (StateObject)SyncResult.AsyncState;
- Socket Handler = StateObj.workSocket;
- m_iTemp = Handler.EndReceive(SyncResult);
- if (m_iTemp > 0)
- {
- OnRecv(StateObj.buffer, Handler);
- }
- }
- //--------------------------------------------------------------------------------
- public virtual void OnCreate()
- {
- }
- //--------------------------------------------------------------------------------
- public virtual void OnCreate(SocketException x)
- {
- }
- //--------------------------------------------------------------------------------
- public virtual void OnConnect(Socket Client)
- {
- }
- //--------------------------------------------------------------------------------
- public virtual void OnDisconnect(Socket Client)
- {
- }
- //--------------------------------------------------------------------------------
- public virtual void OnRecv(byte[] buffer, Socket Client)
- {
- }
- //--------------------------------------------------------------------------------
- private struct ProcessVar
- {
- public Thread m_Thread;
- public bool m_Factor;
- public int m_Idle;
- }