private void ReceiveCb(IAsyncResult ar) { Conn conn = (Conn)ar.AsyncState; try { int count = conn.socket.EndReceive(ar); //关闭信号 if (count <= 0) { //Console.WriteLine("收到 [" + conn.GetAdress() +"] 断开链接"); //conn.Close(); //return; } else { BroadCast(conn.readBuff, count); } //数据处理 //string str = System.Text.Encoding.UTF8.GetString(conn.readBuff, 0, count); //Console.WriteLine("收到 [" + conn.GetAdress() +"] 数据:" + str); //str = conn.GetAdress() + ":" + str; //byte[] bytes = System.Text.Encoding.Default.GetBytes(str); //继续接收 conn.socket.BeginReceive(conn.readBuff, conn.buffCount, conn.BuffRemain(), SocketFlags.None, ReceiveCb, conn); } catch (Exception e) { Console.WriteLine("收到 [" + conn.GetAdress() + "] 断开链接"); conn.Close(); } }
//Accept回调 private void AcceptCb(IAsyncResult ar) { try { Socket socket = listenfd.EndAccept(ar); int index = NewIndex(); if (index < 0) { socket.Close(); Console.Write("[警告]链接已满"); } else { Conn conn = conns[index]; conn.Init(socket); string adr = conn.GetAdress(); Console.WriteLine("客户端连接 [" + adr + "] conn池ID:" + index); conn.socket.BeginReceive(conn.readBuff, conn.buffCount, conn.BuffRemain(), SocketFlags.None, ReceiveCb, conn); } listenfd.BeginAccept(AcceptCb, null); } catch (Exception e) { Console.WriteLine("AcceptCb失败:" + e.Message); } }
private void AcceptCb(IAsyncResult ar)//这个函数应该是刚连接上触发一次,用来给连接的socket分配id的 { try { Socket socket = listenfd.EndAccept(ar); int index = NewIndex(); if (index < 0) { socket.Close(); Console.WriteLine("[警告]连接已满"); } else { Conn conn = conns[index]; conn.Init(socket); string adr = conn.GetAddress(); Console.WriteLine("[连接]" + adr + " count " + index); conn.socket.BeginReceive(conn.readBuff, conn.buffCount, conn.BuffRemain(), SocketFlags.None, ReceiveCb, conn); } listenfd.BeginAccept(AcceptCb, null); } catch (Exception e) { Console.WriteLine("[错误]AcceptCb " + e.Message); } }
private void ReceiveCb(IAsyncResult ar) { Conn conn = (Conn)ar.AsyncState; try { int count = conn.socket.EndReceive(ar); //关闭信号 if (count <= 0) { Console.WriteLine("收到 [" + conn.GetAdress() + "] 断开链接"); conn.Close(); return; } //数据处理 string str = System.Text.Encoding.UTF8.GetString(conn.readBuff, 0, count); Console.WriteLine("收到 [" + conn.GetAdress() + "] 数据:" + str); HandleMsg(conn, str); //继续接收 conn.socket.BeginReceive(conn.readBuff, conn.buffCount, conn.BuffRemain(), SocketFlags.None, ReceiveCb, conn); } catch (Exception e) { Console.WriteLine("收到 [" + conn.GetAdress() + "] 断开链接"); conn.Close(); } }
private void AcceptCb(IAsyncResult ar) { try { Socket socket = listenfd.EndAccept(ar); int index = NewIndex(); if (index < 0) { socket.Close(); } else { Conn conn = conns[index]; conn.Init(socket); //客户端连接 Connnum++; conn.socket.BeginReceive(conn.readBuff, conn.buffCount, conn.BuffRemain(), SocketFlags.None, ReceiveCb, conn); listenfd.BeginAccept(AcceptCb, null); //返回ID SendSuccess(conn); } } catch (Exception e) { } }
private void ReceiveCb(IAsyncResult ar) { Conn conn = (Conn)ar.AsyncState; lock (conn) { try { int count = conn.socket.EndReceive(ar); //关闭信号 if (count <= 0) { Console.WriteLine("收到 [" + conn.GetAdress() + "] 断开链接"); conn.Close(); return; } conn.buffCount += count; ProcessData(conn); //继续接收 conn.socket.BeginReceive(conn.readBuff, conn.buffCount, conn.BuffRemain(), SocketFlags.None, ReceiveCb, conn); } catch (Exception e) { Console.WriteLine("收到 [" + conn.GetAdress() + "] 断开链接 " + e.Message); conn.Close(); } } }
private void ReceiveCb(IAsyncResult ar)//这个函数是连接成功后,持续接受信息的 { Conn conn = (Conn)ar.AsyncState; lock (conn) { try { int count = conn.socket.EndReceive(ar);//异步读取操作,返回读取的字节数 if (count <= 0) { Console.WriteLine("[断开]ReceiveCb " + conn.GetAddress()); Console.WriteLine("[错误]读取字节失败 count:" + count); //conn.Close(); return; } conn.buffCount += count; ProcessData(conn); //处理数据 conn.socket.BeginReceive(conn.readBuff, conn.buffCount, conn.BuffRemain(), SocketFlags.None, ReceiveCb, conn); //继续接收 } catch (Exception e) { Console.WriteLine("[断开]" + conn.GetAddress()); Console.WriteLine("[断开]" + e.Message); conn.Close(); } } }
void AcceptCb(IAsyncResult ar) { try { Socket sockt = listened.EndAccept(ar); int index = NewIndex(); if (index < 0) { sockt.Close(); Debug.Log("[警告]:连接已满"); } else { Conn c = conns[index]; c.Init(sockt); string adr = c.GetAddress(); Debug.Log("客户端连接:[" + adr + "]"); c.socket.BeginReceive(c.readByte, 0, c.BuffRemain(), SocketFlags.None, AcceptCb, c); listened.BeginAccept(AcceptCb, null); } } catch (Exception e) { Debug.Log("AcceptCb失败:" + e.Message); } }
/// <summary> /// Receive回调 /// 这里处理3件事情 /// 1.接收并处理消息,因为是多人聊天,服务端收到消息后,需要转发给所有人 /// 2.如果收到客户端关闭连接的信号,则断开连接 /// 3.继续调用BeginReceive接收下一个数据 /// </summary> /// <param name="ar"></param> private void ReceiveCb(IAsyncResult ar) { //获取BeginReceive传递的Conn对象 Conn conn = (Conn)ar.AsyncState; try { //获取接收的字节数 int count = conn.socket.EndReceive(ar); //关闭信号 if (count <= 0) { Console.WriteLine("收到字节数过小[" + conn.GetAddress() + "]断开连接"); conn.Close(); return; } //数据处理 string str = System.Text.Encoding.UTF8.GetString(conn.readBuff, 0, count); Console.WriteLine("收到[" + conn.GetAddress() + "] 数据:" + str); //处理数据 HandleMsg(conn, str); str = conn.GetAddress() + ":" + str; byte[] bytes = System.Text.Encoding.Default.GetBytes(str); //广播 for (int i = 0; i < conns.Length; i++) { if (conns[i] == null) { continue; } if (!conns[i].isUse) { continue; } if (conns[i] == conn) { continue; } Console.WriteLine("将消息转播给:" + conns[i].GetAddress()); conns[i].socket.Send(bytes); } //继续接收 conn.socket.BeginReceive(conn.readBuff, conn.buffCount, conn.BuffRemain(), SocketFlags.None, ReceiveCb, conn); } catch (Exception e) { Console.WriteLine("接收数据错误[" + e.Message + "]需要断开连接[" + conn.GetAddress() + "]"); conn.Close(); } }
//Accept回调 private void AcceptCb(IAsyncResult ar) { try { Socket socket = listenfd.EndAccept(ar); int index = NewIndex(); if (index < 0) { socket.Close(); Console.Write("[警告]链接已满"); } else { Conn conn = conns[index]; conn.Init(socket); string adr = conn.GetAdress(); Console.WriteLine("客户端连接 [" + adr + "] conn池ID:" + index); m_CurrentConnectedCount++; Console.WriteLine($"当前链接数:{m_CurrentConnectedCount}"); //[Serializable] conn.socket.BeginReceive(conn.readBuff, conn.buffCount, conn.BuffRemain(), SocketFlags.None, ReceiveCb, conn); } listenfd.BeginAccept(AcceptCb, null); if (m_CurrentConnectedCount == 1) { //var info = JsonConvert.SerializeObject(new JsonClass { className = "GameStartAction", data = new GameStartAction { NumberOfPlayes = 2 } }); //Console.WriteLine($"info"); //byte[] bytes = System.Text.Encoding.Default.GetBytes(info); BroadCast(BinarySerialization.SerializeObjectToByteArray(new GameStartAction { NumberOfPlayes = 2 })); } } catch (Exception e) { Console.WriteLine("AcceptCb失败:" + e.Message); } }
private void ReceiveCb(IAsyncResult ar) { Conn conn = (Conn)ar.AsyncState; lock (conn) { try { int count = conn.socket.EndReceive(ar); //关闭信号 if (count <= 0) { Logger.Default.Info("收到 [" + conn.GetAdress() + "] 断开链接"); conn.Close(); return; } conn.buffCount += count; ProcessData(conn); //继续接收 conn.socket.BeginReceive(conn.readBuff, conn.buffCount, conn.BuffRemain(), SocketFlags.None, ReceiveCb, conn); } catch (SocketException e) { if (conn.isUse) { Logger.Default.Error("收到 [SocketException] 断开链接 " + e.Message); if (conn.player != null) { conn.player.Logout(); } } } catch (Exception e) { if (conn.isUse) { Logger.Default.Error("收到 [" + conn.GetAdress() + "] 断开链接 " + e.Message); if (conn.player != null) { conn.player.Logout(); } } } } }
//客户端接入后 void AcceptCB(IAsyncResult _ar) { int index = GetConn(); if (index < 0) { Util.Out("未能取得可用连接", 1); return; } Socket socket = listener.EndAccept(_ar); Conn conn = conns[index]; conn.Init(socket); string ip = conn.GetAddress(); Util.Out("客户连接成功,ID:" + ip, 1); conn.socket.BeginReceive(conn.readBuff, conn.buffCount, conn.BuffRemain(), SocketFlags.None, ReceiveCB, conn); listener.BeginAccept(AcceptCB, null); }
private void ReceiveCb(IAsyncResult ar) { Conn conn = (Conn)ar.AsyncState; //获取BeginReceivec传入的Conn对象 lock (conn) { try { int count = conn.socket.EndReceive(ar); //获取接受的字节数 if (count < 0) { return; } conn.buffCount += count; ProcessData(conn); conn.socket.BeginReceive(conn.readBuff, conn.buffCount, conn.BuffRemain(), SocketFlags.None, ReceiveCb, conn); } catch (Exception e) { } } }
//Receive回调 private void ReceiveCb(IAsyncResult ar) { //获取传递的Conn对象 Conn conn = (Conn)ar.AsyncState; //加锁,防止多线程操作同一对象 lock (conn) { try{ if (conn.socket == null || !conn.socket.Connected) { Console.WriteLine("socket已经断开连接 出了问题,不能处理消息了"); conn.Close(); return; } int count = conn.socket.EndReceive(ar); //关闭信号 if (count <= 0) { Console.WriteLine("收到字节数过小[" + conn.GetAddress() + "]断开连接"); conn.Close(); return; } conn.buffCount += count; //判断缓存区数据是否可以处理 ProcessData(conn); //继续接收 conn.socket.BeginReceive(conn.readBuff, conn.buffCount, conn.BuffRemain(), SocketFlags.None, ReceiveCb, conn); } catch (Exception e) { Console.WriteLine("接收数据错误[" + e.Message + "]需要断开连接[" + conn.GetAddress() + "]"); conn.Close(); } } }
void ReceiveCB(IAsyncResult _ar) { Conn conn = (Conn)_ar.AsyncState; int count = conn.socket.EndReceive(_ar); if (count <= 0) { Util.Out("客户" + conn.GetAddress() + "断开连接", 1); conn.Close(); return; } string str = System.Text.Encoding.UTF8.GetString(conn.readBuff, 0, count); Util.Out("收到" + conn.GetAddress() + "的消息:" + str, 1); str = conn.GetAddress() + ":" + str; byte[] bytes = System.Text.Encoding.Default.GetBytes(str); foreach (Conn c in conns) { if (c == null || !c.inUse) { continue; } Util.Out("将消息广播给" + c.GetAddress()); c.socket.Send(bytes); } conn.socket.BeginReceive(conn.readBuff, conn.buffCount, conn.BuffRemain(), SocketFlags.None, ReceiveCB, conn); }
private void ReceiveCb(IAsyncResult ar) { Conn conn = (Conn)ar.AsyncState; lock (conn); { try { int count = conn.socket.EndReceive(ar); if (count <= 0) { } conn.buffCount += count; ProcessData(conn); conn.socket.BeginReceive(conn.readBuff, conn.buffCount, conn.BuffRemain(), SocketFlags.None, ReceiveCb, conn); }catch (Exception e) { Console.WriteLine("收到【" + conn.GetAdress() + "】断开连接"); conn.Close(); } } }
void ReceiveCb(IAsyncResult ar) { Conn conn = (Conn)ar.AsyncState; try { int count = conn.socket.EndReceive(ar); if (count <= 0) { conn.Close(); return; } string str = System.Text.Encoding.UTF8.GetString(conn.readByte, 0, count); Debug.Log("收到[" + conn.GetAddress() + "]数据:" + str); str = conn.GetAddress() + ":" + str; byte[] bytes = System.Text.Encoding.Default.GetBytes(str); for (int i = 0; i < conns.Length; ++i) { if (conns[i] == null) { continue; } if (!conns[i].isUse) { continue; } conns[i].socket.Send(bytes); } conn.socket.BeginReceive(conn.readByte, 1024, conn .BuffRemain(), SocketFlags.None, ReceiveCb, conn); } catch (Exception e) { conn.Close(); } }
private void ReceiveCb(IAsyncResult ar) { Conn conn = (Conn)ar.AsyncState; try { int count = conn.socket.EndReceive(ar); //关闭信号 if (count <= 0) { Console.WriteLine("收到 [" + conn.GetAdress() + "] 断开链接"); curPlayerCount--; Console.WriteLine("当前连接数:" + curPlayerCount); SendPlayerCount(curPlayerCount); conn.Close(); return; } //数据处理 string str = System.Text.Encoding.UTF8.GetString(conn.readBuff, 0, count); //Console.WriteLine("收到 [" + conn.GetAdress() + "] 数据:" + str); HandleMsg(conn, str); if (str == "Leave The Room......") { curPlayerCount--; Console.WriteLine("当前连接数:" + curPlayerCount); SendPlayerCount(curPlayerCount); SendMsg(conn, str); conn.Close(); return; } else if (str.StartsWith("POS")) { SendPlayerPos(conn, str); } else if (str.StartsWith("DIRPOS")) { SendPlayerDIRPos(conn, str); } else if (str.StartsWith("BulletPOS")) { SendBulletPos(conn, str); } else if (str.StartsWith("LEAVE")) { curPlayerCount--; Console.WriteLine("当前连接数:" + curPlayerCount); SendPlayerCount(curPlayerCount); SendLeave(conn, str); conn.Close(); return; } else { SendMsg(conn, str); } //继续接收 conn.socket.BeginReceive(conn.readBuff, conn.buffCount, conn.BuffRemain(), SocketFlags.None, ReceiveCb, conn); } catch (Exception e) { Console.WriteLine("Exception e:" + e); } }