public override void ExecuteCommand(MsgPackSession session, BinaryRequestInfo requestInfo) { var serializer = SerializationContext.Default.GetSerializer <TMsgPack>(); using (var stream = new MemoryStream(requestInfo.Body)) { var unpackedObject = serializer.Unpack(stream) as TMsgPack; ExecuteCommand(session, unpackedObject); } }
public void OnConnected(MsgPackSession session) { Client new_client = new Client(this, session); Session2ClientTable.Add(session, new_client); OutPut(session.LocalEndPoint.ToString() + " 进行了连接"); //Attach the Delegates new_client.Connected += OnConnected; }
internal void OnDisconnected(MsgPackSession session, CloseReason value) { lock (this) { OutPut(session.SessionID + " disconnected:" + value); Client client = Session2ClientTable[session]; client.Connected -= OnConnected; //删除session对应 Session2ClientTable.Remove(session); session = null; //删除uid对应 if (client != null) { client.OnDisconnected(); if (UId2ClientTable.ContainsValue(client)) { UId2ClientTable.Remove(client.UserID); } //广播离线信息 ClientList.Instance().RemoveClient(client.UserID); //更新到form1 UpdateUsers(ClientList.Instance().GetUserList(0)); MyData data = new MyData() { Description = PacketDescription.Hall2Cient, Protocol = protocol.UpdateHallLeave, Body = new List <string> { client.UserID.ToString() } }; foreach (Client other in UId2ClientTable.Values) { other.SendProfileReply(data); } } } }
public void OnDisconnected(MsgPackSession session, CloseReason value) { OutPut(session.SessionID + " disconnected:" + value); if (Session2ClientTable.TryGetValue(session, out Client client)) { client.Connected -= OnConnected; //删除session对应 Session2ClientTable.TryRemove(session, out Client remove); session = null; //删除uid对应 if (client != null) { client.OnDisconnected(); if (UId2ClientTable.TryGetValue(client.UserID, out Client current) && current == client) { UId2ClientTable.TryRemove(client.UserID, out remove); //广播离线信息 clientList.TryRemove(client.UserID, out string account); //更新到form1 UpdateUsers(clientList.ToDictionary(entry => entry.Key, entry => entry.Value)); MyData data = new MyData() { Description = PacketDescription.Hall2Cient, Protocol = Protocol.UpdateHallLeave, Body = new List <string> { client.UserID.ToString() } }; List <Client> clients = new List <Client>(UId2ClientTable.Values); foreach (Client other in clients) { other.SendProfileReply(data); } } } } }
public void OnRequesting(MsgPackSession session, BinaryRequestInfo requestInfo) { Client client = Session2ClientTable[session]; MyData data = null; try { data = PacketTranslator.Unpack(requestInfo.Body); } catch (Exception e) { Debug(string.Format("error at parse client request {0} {1}", e.Message, e.TargetSite)); Debug(string.Format("error messsage {0} {1}", requestInfo?.ToString(), requestInfo.Body?.ToString())); session.Close(); return; } TransferType request = PacketTranslator.GetTransferType(requestInfo.Key); //OutPut(string.Format("请求协议为{0}, 内容为{1}", data.Protocol.ToString(), data.Body[0])); if (client.UserName == null && !request.Equals(TransferType.TypeLogin)) { OutPut(string.Format("{0} 未登录客户端,关闭连接", client.IP)); session.Close(); return; } if (data.Description == PacketDescription.Client2Hall) { switch (request) { case TransferType.TypeLogin: //登录相关 switch (data.Protocol) { case protocol.Login: client.CheckUserName(data); break; case protocol.Register: //用户注册 client.Register(data); break; case protocol.PasswordChange: //密码修改 break; default: return; } break; case TransferType.TypeMessage: //公共事件 比如 聊天 MessageForward(client, data); break; case TransferType.TypeSwitch: //切换room和hall RoomSwitch(client, data); break; case TransferType.TypeUserProfile: //修改个人信息 client.UpDateProfile(data); break; default: Debug(string.Format("{0} 对聊天大厅无效的请求", client.UserName)); break;; } } else if (data.Description == PacketDescription.Client2Room) { switch (request) { case TransferType.TypeMessage: //公共事件 比如 聊天 MessageForward(client, data); break; case TransferType.TypeGameControll: //游戏内各操作 client.ControlGame(data); break; case TransferType.TypeSwitch: //切换room和hall RoomSwitch(client, data); break; default: Debug(string.Format("{0} 对ROOM无效的请求", client.UserName)); break;; } } else { OutPut("无效的客户端请求"); } }
/// <summary> ///客户端请求处理 /// </summary> /// <param name="session">会话</param> /// <param name="requestInfo">请求信息</param> void Server_NewRequestReceived(MsgPackSession session, BinaryRequestInfo requestInfo) { hall.OnRequesting(session, requestInfo); }
void Server_NewSessionConnected(MsgPackSession session) { hall.OnConnected(session); }
private void Server_SessionClosed(MsgPackSession session, SuperSocket.SocketBase.CloseReason value) { hall.OnDisconnected(session, value); }
public abstract void ExecuteCommand(MsgPackSession session, TMsgPack pack);
public override void ExecuteCommand(MsgPackSession session, MyData pack) { Console.WriteLine(pack.Name + ":" + pack.Other); }