public void Send(PacketSend pkt) { if (!CheckSocketState()) { DisconnectedHandler(); return; } ArrByte64K arrByte = pkt.ExportArrByte64K(); try { clientSocket.BeginSend(arrByte.arrByte64K, 0, arrByte.len, 0, new AsyncCallback(SendCallback), clientSocket); if (!sendDone.WaitOne(Timeoutmilliseconds)) { TimeOut(); } Receive(clientSocket); } catch (Exception e) { SockErrorStr = e.ToString(); DisconnectedHandler(); //Console.WriteLine(e.ToString()); } }
static void Main() { App_Update.Init(); App_Event.Init(); App_ClientSocket.InitSocket(EServerType.Type_Login, SIpPort.Create(ip_login, port_login)); // App_ClientSocket.InitSocket(EServerType.Type_Game, SIpPort.Create(ip_game, port_game)); AsynchronousClient clientSocket = App_ClientSocket.ClientSockets[EServerType.Type_Login]; clientSocket.conFinishedCallback = (bool succeed) => { if (succeed) { Log("Type_Login" + "===Connected Succeed"); PacketSend pk = PacketSend.Create(EEvents.Event_LoginHeart); pk.Write((byte)1); pk.Write("meng dong..你好"); App_ClientSocket.Send(pk, EServerType.Type_Login); pk = PacketSend.Create(EEvents.Event_ServerTime); App_ClientSocket.Send(pk, EServerType.Type_Login); } else { Log("Type_Login" + "===Connected Failed!!!"); } }; clientSocket.HandleConnect(); //if (!App_ClientSocket.ClientSockets.ContainsKey(EServerType.Type_Game)) //{ // Log("Error!!!===!App_ClientSocket.ClientSockets.ContainsKey(EServerType.Type_Game)"); // return; //} //clientSocket = App_ClientSocket.ClientSockets[EServerType.Type_Game]; //clientSocket.conFinishedCallback = (bool succeed) => //{ // if (succeed) // { // Log("Type_Game" + "===Connected Succeed"); // PacketSend pk = PacketSend.Create(EEvents.Event_GameHeart); // App_ClientSocket.Send(pk, EServerType.Type_Game); // } // else // { // Log("Type_Game" + "===Connected Failed!!!"); // } //}; //clientSocket.HandleConnect(); Start(); }
public static PacketSend Create(EEvents eventId) { var pk = new PacketSend(); //pk._arrByte64K.arrByte64K[0] = (byte)(15 >> 8); //pk._arrByte64K.arrByte64K[1] = (byte)(15);//第1、2位存储id pk._arrByte64K.arrByte64K[2] = (byte)((ushort)eventId >> 8); pk._arrByte64K.arrByte64K[3] = (byte)((ushort)eventId);//第3、4位存储id return(pk); }
/// <summary> /// 发送指定消息 /// </summary> /// <param name="pk"></param> /// <param name="type"></param> public static void Send(PacketSend pk, EServerType type) { dicClients[type].Send(pk); }