private void RecMsgCb(IAsyncResult ret) { SimplePosConn conn = (SimplePosConn)ret.AsyncState; if (conn == null) { Console.WriteLine("The Conn is Null"); return; } try { int ind = 0; int count = conn.PosConnSocket.EndReceive(ret); conn.ByteCount = count; String s = System.Text.Encoding.Default.GetString(conn.GetContent(), ind, count); //回调发送 if (m_fSMsg != null) { m_fSMsg(s, conn); } else { Console.WriteLine("The CB Is Null"); } } catch (Exception e) { Console.WriteLine(e.ToString()); } conn.SPConnStart(); }
private void HandleMsg(string content, SimplePosConn conn) { if (content.Length == 0 || conn == null) return; PrintMsg (content , conn); string[] proto = content.Split ('|'); if (proto.Length <= 1) return; if (proto[0].Equals("Leave")) { LeaveMsg (conn); } else if (proto[0].Equals("Create")) { //可能当本次连接创建后已经有其他连接 需要向该conn发送其他人的信息(数据小会将数据合并:先发了创建又发了其他人的信息所以会导致数据合并) proto [1] = GetIndex (conn) + ""; content = string.Join ("|" , proto); content += this.GetMsgToOne (conn); byte[] sBy = System.Text.Encoding.Default.GetBytes (content); conn.PosConnSocket.Send (sBy); } else if (proto[0].Equals("Post")) { SendMsgToAll (content, conn); } }
private void PrintMsg(string content , SimplePosConn conn) { Socket sk = conn.PosConnSocket; string msgAddress = sk.RemoteEndPoint.ToString(); string msgSays = msgAddress + " content: " + content; Console.WriteLine (msgSays); }
private int GetIndex(SimplePosConn conn) { for (int i = 0; i < m_posConns.Length; ++i) { if (m_posConns [i] == conn) return i; } return -1; }
private void LeaveMsg(SimplePosConn conn) { Socket sk = conn.PosConnSocket; string msg = sk.RemoteEndPoint.ToString (); msg += "Leaved"; SendMsgToAll (msg , conn); conn.SPShutDown (); }
private void SendMsgToAll(string content , SimplePosConn conn) { byte[] sBy = System.Text.Encoding.Default.GetBytes (content); for (int i = 0; i < m_posConns.Length; ++i) { if (m_posConns [i] != null && m_posConns [i].PosConnUsed && m_posConns[i] != conn) { m_posConns [i].PosConnSocket.Send (sBy); } } }
private SimplePosConn NewSPConn() { for (int i = 0; i < m_posConns.Length; ++i) { if (m_posConns [i] == null) { m_posConns [i] = new SimplePosConn (); m_posConns [i].SPConnInit (HandleMsg); return m_posConns [i]; } else if(!m_posConns[i].PosConnUsed) { return m_posConns [i]; } } return null; }
private void AcceptConnCb(IAsyncResult ret) { Socket sk = m_socket.EndAccept (ret); SimplePosConn conn = NewSPConn (); if (conn == null) { Console.WriteLine ("The Connector Pool Is Full"); sk.Close (); return; } conn.PosConnSocket = sk; conn.PosConnUsed = true; conn.SPConnStart (); Console.WriteLine (sk.RemoteEndPoint.ToString() + "Is Connected"); m_socket.BeginAccept (AcceptConnCb, null); }
private string GetMsgToOne(SimplePosConn conn) { string ret = ""; for (int i = 0; i < m_posConns.Length; ++i) { if (m_posConns [i] != null && m_posConns [i].PosConnUsed && m_posConns [i] != conn) { string str = System.Text.Encoding.UTF8.GetString (m_posConns[i].GetContent() , 0 , m_posConns[i].ByteCount); string[] proto = str.Split ('|'); if (proto.Length <= 1 || proto [0] == "Leave") continue; proto[0] = "Post"; str = string.Join ("|",proto); ret = ret + '}' + str; //byte[] sBy = System.Text.Encoding.Default.GetBytes (str); //conn.PosConnSocket.Send (sBy); } } return ret; }