protected override void OnStart() { _messenger = new Messenger(_handler); PacketStream stream; ConnectorInfo info; stream = _agent.Connect("127.0.0.1", Port.Database); stream.Write(new ConnectorInfo("Login")); _messenger.Register("Database", stream); Console.WriteLine("Database connected."); //Listen Proxy _agent.Bind("127.0.0.1", Port.Login, 1); stream = _agent.Listen(); info = (ConnectorInfo)stream.Read().body; _messenger.Register(info.name, stream); Console.WriteLine(info.name + " connected."); foreach (var key in _messenger.Keys) { var dispatcher = Task.Run(() => { while (stopped == false) _messenger.Dispatch(key); }); _dispatchers.Add(key, dispatcher); } _messenger.Start(); }
protected override void OnStart() { _messenger = new Messenger(_handler); Action listenner = () => { _agent.Bind("127.0.0.1", Port.Database, 1); var stream = _agent.Listen(); var info = (ConnectorInfo)stream.Read().body; _messenger.Register(info.name, stream); Console.WriteLine(info.name + " connected."); }; listenner();//Login listenner();//Town listenner();//Forest listenner();//Mine listenner();//Boss listenner();//Proxy foreach (var key in _messenger.Keys) { var dispatcher = Task.Run(() => { while (stopped == false) _messenger.Dispatch(key); }); _dispatchers.Add(key, dispatcher); } _messenger.Start(); }
void LoginQuery(Messenger messenger, string key, LoginQuery query) { try { try { using (var account = new StreamReader(new FileStream(accountPath + "\\" + query.id, FileMode.Open))) { var pw = account.ReadLine(); var name = account.ReadLine(); if (query.pw == pw) { messenger.Send("Login", new LoginAnswer(query.confirmID, true, name, "Town")); } else { throw new InvalidLoginException("PW가 다릅니다."); } } } catch (IOException) { throw new InvalidLoginException("존재하지 않는 ID입니다."); } } catch (InvalidLoginException e) { var answer = new LoginAnswer(query.confirmID, false, "", ""); answer.message = e.message; messenger.Send("Login", answer); } }
void LoginAnswer(Messenger messenger, string key, LoginAnswer query) { if (query.accepted) { var keys = (ICollection<string>)_server._clientMessenger.Keys; if (keys.Contains(query.name)) { query.accepted = false; _server._confirmMessenger.Send(query.confirmID.ToString(), query); } else { lock (_server._lock) { PacketStream stream = _server._confirmMessenger.Unregister(query.confirmID.ToString()); _server._clientMessenger.Register(query.name, stream); } _server._clientMessenger.Send(query.name, query); //_server._messenger.Send(query.world, new PlayerJoin()); } } else { _server._confirmMessenger.Send(query.confirmID.ToString(), query); } }
void SerializedPlayerQuery(Messenger messenger, string key, SerializedPlayerQuery query) { try { while (true) { try { var data = File.ReadAllBytes(serializedPlayerPath + "\\" + query.player); messenger.Send(query.sender, new SerializedPlayerAnswer(query.player, data)); return; } catch (FileNotFoundException) { throw; } catch (IOException) { } } } catch (FileNotFoundException) { try { while (true) { try { var data = File.ReadAllBytes(serializedPlayerPath + "\\Default"); messenger.Send(query.sender, new SerializedPlayerAnswer(query.player, data)); return; } catch (FileNotFoundException) { throw; } catch (IOException) { } } } catch (FileNotFoundException) { messenger.Send(query.sender, new SerializedPlayerAnswer(query.player, new byte[1])); } } }
void SignUp(Messenger messenger, string key, SignUp packet) { try { using (var account = new StreamWriter(new FileStream(accountPath + "\\" + packet.id, FileMode.Create))) { account.WriteLine(packet.pw); account.WriteLine(packet.id); } } catch (Exception e) { History.Log(e.ToString()); } }
void SerializedPlayerSave(Messenger messenger, string key, SerializedPlayerSave data) { File.WriteAllBytes(serializedPlayerPath + "\\" + data.player, data.bytes); }
void LoginQuery(Messenger messenger, string key, LoginQuery query) { _server._messenger.Send("Login", query); }
protected override void OnStart() { _messenger = new Messenger(this); _confirmMessenger = new Messenger(this); Action<string, Port> connector = (string ip, Port port) => { var stream = _agent.Connect(ip, port); stream.Write(new ConnectorInfo("Proxy")); _messenger.Register(port.ToString(), stream); Console.WriteLine(port.ToString() + " connected."); _worldByUser.Add(port.ToString(), port.ToString()); _serverKeys.Add(port.ToString()); _rpcBufferByWorld.Add(port.ToString(), new List<RPC>()); }; connector("127.0.0.1", Port.Database); connector("127.0.0.1", Port.Login); connector("127.0.0.1", Port.Town); connector("127.0.0.1", Port.Forest); connector("127.0.0.1", Port.Mine); connector("127.0.0.1", Port.Boss); foreach (var key in _messenger.Keys) { var dispatcher = Task.Run(() => { while (stopped == false) _messenger.Dispatch(key); }); _dispatchers.Add(key, dispatcher); } _accepter = Task.Run(() => { _agent.Bind("127.0.0.1", Port.Proxy, 4); while (stopped == false) { try { if (_agent.HasConnectReq()) { string stringID = _currentConfirmId.ToString(); int ID = _currentConfirmId; lock (_lock) { _confirmMessenger.Register(stringID, _agent.Listen()); _confirmMessenger.Send(stringID, new ConfirmID(ID)); } _currentConfirmId++; Console.WriteLine("Client connected."); } } catch (Exception e) { History.Log(e.ToString()); } } }); _confirm = Task.Run(() => { while (stopped == false) { try { lock (_lock) { foreach (var confirmID in _confirmMessenger.Keys) _confirmMessenger.Dispatch(confirmID); } } catch (Exception e) { History.Log(e.ToString()); } } }); _client = Task.Run(() => { while (stopped == false) { try { lock (_lock) { foreach (var key in _messenger.Keys) { if (_serverKeys.Contains(key) == false) _messenger.Dispatch(key); } } } catch (Exception e) { History.Log(e.ToString()); } } }); _messenger.Start(); _messenger.onDisconnected = key => { foreach (var target in _messenger.Keys) { if (target != key && _worldByUser[target] == _worldByUser[key]) _messenger.Send(target, new PlayerDisconnect(key)); } _rpcBufferByWorld[_worldByUser[key]].RemoveAll(rpc => rpc.sender == key); _dispatchers.Remove(key); _worldByUser.Remove(key); }; _confirmMessenger.Start(); }
void SignUp(Messenger messenger, string key, SignUp packet) { _messenger.Send("Database", packet); }
void RemoveBufferedRPC(Messenger messenger, string key, RemoveBufferedRPC packet) { var buffer = _rpcBufferByWorld[_worldByUser[packet.caller]]; for (int i = 0; i < buffer.Count; i++) { if (buffer[i].GetType().Name == packet.typeName) { NetworkInstantiate ni = buffer[i] as NetworkInstantiate; if (ni != null && ni.networkID == packet.networkID) buffer.RemoveAt(i); else { Sync sync = buffer[i] as Sync; if (sync != null && sync.signallerID == packet.networkID) buffer.RemoveAt(i); } } } }
void LoginAnswer(Messenger messenger, string key, LoginAnswer answer) { if (answer.accepted) { if (IsLoggedIn(answer.name)) { answer.accepted = false; answer.message = "이미 로그인 중입니다."; _confirmMessenger.Send(answer.confirmID.ToString(), answer); } else { try { lock (_lock) { PacketStream stream = _confirmMessenger.Unregister(answer.confirmID.ToString()); AddClient(answer.name, stream); } try { _worldByUser.Add(answer.name, answer.world); } catch (ArgumentException e) { History.Log(e.ToString()); _worldByUser[answer.name] = answer.world; } _messenger.Send(answer.name, answer); } catch(Exception e) { History.Log(e.ToString()); } } } else { _confirmMessenger.Send(answer.confirmID.ToString(), answer); } }
void BufferedRPCRequest(Messenger messenger, string key, BufferedRPCRequest packet) { SendBufferedRPC(packet.caller); }