// recv thread function private void RecvThread() { CSLogger.LogDebug("Enter RecvThread"); try { while (true) { if (m_socket == null || m_socket.Connected == false) { m_recv_thread = null; break; } ParseProcotol(); System.Threading.Thread.Sleep(10); } } catch (SocketException /*e*/) { CSLogger.LogDebug("RecvThread: SocketException "); } catch (Exception /*e*/) { CSLogger.LogDebug("RecvThread: Exception "); } CSLogger.LogDebug("Exit RecvThread"); }
// conn thread function private void ConnThread() { CSLogger.LogDebug("Enter ConnThread"); try { IPAddress ip_address = null; IPAddress[] ip_array = Dns.GetHostAddresses(m_host); foreach (IPAddress ip in ip_array) { if (ip.AddressFamily == AddressFamily.InterNetwork) { ip_address = ip; break; } } IPEndPoint ip_end_point = new IPEndPoint(ip_address, m_port); m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); m_socket.Blocking = true; m_socket.Connect(ip_end_point); m_recv_stream = new MemoryStream(); m_recv_thread = new Thread(new ThreadStart(RecvThread)); m_send_thread = new Thread(new ThreadStart(SendThread)); m_recv_thread.Start(); m_send_thread.Start(); m_conn_thread = null; } catch (System.Exception ex) { CSLogger.LogError(ex.ToString()); } CSLogger.LogDebug("Exit ConnThread"); }
// send thread function private void SendThread() { CSLogger.LogDebug("Enter SendThread"); try { Message msg = null; while (true) { if (m_socket == null || m_socket.Connected == false) { m_send_thread = null; break; } m_event_wait_send.WaitOne(); m_event_wait_send.Reset(); lock (m_send_queue) { while (m_send_queue.Count > 0) { msg = m_send_queue.Peek(); byte[] bytes = msg.ToBytes(); int result = 0; int byte_size = bytes.Length; while (result != byte_size) { try { result += m_socket.Send(bytes, result, byte_size - result, SocketFlags.None); } catch (System.Exception ex) { CSLogger.LogError(ex.ToString()); } } m_send_queue.Dequeue(); } } System.Threading.Thread.Sleep(10); } } catch (SocketException /*e*/) { CSLogger.LogDebug("SendThread: SocketException "); } catch (Exception /*e*/) { CSLogger.LogDebug("SendThread: Exception "); } CSLogger.LogDebug("Exit SendThread"); }
// Get message from queue public Message GetMessage() { Message msg = null; if (m_recv_queue != null && m_recv_queue.Count > 0) { lock (m_recv_queue) { msg = m_recv_queue.Dequeue(); CSLogger.LogDebug("Receive msg:" + msg.ToString()); } } return(msg); }
// Send msg to server public void SendMsgToServer(Message msg) { if (m_server_socket == null) { return; } if (!m_server_socket.IsConnected()) { CSLogger.LogNotice("Server Is DisConnected"); return; } if (null == msg) { CSLogger.LogError("SendMsgToServer: null == msg"); return; } if (null == m_server_socket) { CSLogger.LogError("SendMsgToServer: null == m_gs_socket"); return; } CSLogger.LogDebug("SendMsgToServer: " + msg.ToString()); m_server_socket.SendMsg(msg); }
// Default handle of msg: GC_Chat_Msg protected virtual bool OnGC_Chat_Msg(GC_Chat_Msg msg) { CSLogger.LogDebug("ServerMsgHandler: Recv msg: [" + msg.ToString() + "]"); return(false); }
// Default handle of msg: GC_Echo_Response protected virtual bool OnGC_Echo_Response(GC_Echo_Response msg) { CSLogger.LogDebug("ServerMsgHandler: Recv msg: [" + msg.ToString() + "]"); return(false); }