// 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"); }
// Send msg public void SendMsg(Message msg) { if (!IsConnected()) { CSLogger.LogError("GateServer is disconnected when send " + msg.ToString()); return; } CSLogger.LogNotice("Send Msg: " + msg.ToString()); lock (m_send_queue) { m_send_queue.Enqueue(msg); } m_event_wait_send.Set(); }
// Called this function in loop public void Update() { if (null == m_server_socket) { return; } if (!m_server_socket.IsConnected()) { CSLogger.LogError("Socket is disconnected, please re-connect it"); return; } Message msg = m_server_socket.GetMessage(); base.DispatchMsg(msg); // Try to Send heart beat msg TimerSendHeartBeatMsg(); }
// connect to a server public void Connect(string host, int port) { DisConnect(false); m_recv_queue = new Queue <Message>(); m_host = host; m_port = port; try { m_conn_thread = new Thread(new ThreadStart(ConnThread)); m_conn_thread.Start(); } catch (Exception /*e*/) { CSLogger.LogError("Connect: Exception"); } }
// Msg dispatch function protected bool DispatchMsg(Message msg) { if (null == msg) { return(false); } if (msg.msg_id == GC_HeartBeat_Msg.MSG_ID) { return(OnGC_HeartBeat_Msg((GC_HeartBeat_Msg)msg)); } if (msg.msg_id == GC_Echo_Response.MSG_ID) { return(OnGC_Echo_Response((GC_Echo_Response)msg)); } if (msg.msg_id == GC_Chat_Msg.MSG_ID) { return(OnGC_Chat_Msg((GC_Chat_Msg)msg)); } CSLogger.LogError("Recv invalid msg id from GateServer: " + msg.msg_id); return(false); }
// 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); }