void SendMessageToClient(JStateObject state, byte messageType, TBase message) { int length = JThrift.Serialize(messageType, message, ref state.writeBuffer); server.Send(state.workSocket, state.writeBuffer, length); Debug.Log(string.Format("<color=green>R: ->C ({0}) - {1} </color> \n", state.name, message)); }
void Disconnect(JStateObject state) //Socket socket) { RemovePeer(state); state.workSocket.Shutdown(SocketShutdown.Both); state.workSocket.Close(); state.workSocket = null; }
public void InitializeRoom() { roomIndex = -1; participants.Clear(); spectators.Clear(); InitMap(); choState = null; hanState = null; sangcharims[0] = Sangcharim.kUnknown; sangcharims[1] = Sangcharim.kUnknown; }
void HandleMessage(JStateObject state, ReqSangcharim req) { sangcharims[state.indexInRoom] = req.Sangcharim; Debug.Log(string.Format("<color=green>R: ({0}-{1}) has sent {2} </color> \n", state.indexInRoom, state.name, req.Sangcharim)); if (sangcharims[0] != Sangcharim.kUnknown && sangcharims[1] != Sangcharim.kUnknown) { NtfSangcharim ntf = new NtfSangcharim(); ntf.Cho = sangcharims[0]; ntf.Han = sangcharims[1]; NotifyMessageToAllClients((byte)JgS2C_Msgs.kNtfSangcharim, ntf); UpdateMapForSangcharim(ntf.Cho, ntf.Han); //this.Invoke("StartGame", 0.5f); //StartGame(); workQueue.Add(StartGame); //this.Invoke("StartGame", 1f); } else if (state.indexInRoom == 1) // Is Han { NtfSangcharimHan ntf = new NtfSangcharimHan(); ntf.Han = sangcharims[HAN]; SendMessageToClient(choState, (byte)JgS2C_Msgs.kNtfSangcharimHan, ntf); } }
public void OnReadMessage(JStateObject state) { JgC2S_Msgs messageType = (JgC2S_Msgs) state.readBuffer[2]; Debug.Log(string.Format("<color=green>R: <-C ({0}) - {1} </color> \n", state.name, messageType)); switch (messageType) { case JgC2S_Msgs.kSangcharim: JThrift.Deserialize(state.readBuffer, Jg.Thrift_Offset, reqSangcharim); HandleMessage(state, reqSangcharim); break; case JgC2S_Msgs.kMovePawn: JThrift.Deserialize(state.readBuffer, Jg.Thrift_Offset, reqMovePawn); HandleMessage(state, reqMovePawn); break; default: Debug.Log(string.Format("<color=red>couldn't find a given case ( {0} ) </color>", messageType)); break; } }
void RemoveUser(JStateObject user) { users.Remove(user.gsn); }
void AddUser(JStateObject state, DummyUserInfo dummyInfo) { state.gsn = dummyInfo.gsn; JgUserInfo userInfo = new JgUserInfo(); userInfo.state = state; userInfo.gsn = dummyInfo.gsn; userInfo.userName = dummyInfo.email; userInfo.nickName = dummyInfo.nickName; userInfo.gameLevel = dummyInfo.gameLevel; userInfo.victoryCount = dummyInfo.victoryCount; userInfo.defeatCount = dummyInfo.defeatCount; users.Add(state.gsn, userInfo); }
public void RemoveParticipant(JStateObject state)// long gsn) { participants.Remove(state.gsn); states.Remove(state); }
void HandleMessage(JStateObject state, ReqMatch req) { JgUserInfo userInfo = users[state.gsn]; JgGameRoom room = FindRoom(userInfo.gameLevel); if (room == null) { Debug.Log("Couldn't find a room . \n"); } else { state.roomIndex = room.RoomIndex(); room.AddParticipant(state); if (room.ParticipantCount() == 2) { room.SetPlayerNationsAndNewMatch(); } } }
void HandleMessage(JStateObject state, ReqChannelInfo req) { }
void HandleMessage(JStateObject state, ReqLogin req) { Debug.Log(string.Format("<color=green>HandleMessage-ReqLogin : </color>",req.ToString())); AnsLogin ans = new AnsLogin(); DummyUserInfo dummyUserInfo = JgRefs.ins_.dummyDb.GetUserInfo(req.UserName); bool isLoginOk = ans.Usn > 0; if (isLoginOk) { state.gsn = dummyUserInfo.gsn; state.name = dummyUserInfo.nickName; ans.NickName = dummyUserInfo.nickName; ans.Level = dummyUserInfo.gameLevel; ans.VictoryCount = dummyUserInfo.victoryCount; ans.DefeatCount = dummyUserInfo.defeatCount; } else { ans.NickName = string.Empty; } ans.Comment = "I'm a server."; if (isLoginOk) { AddUser(state, dummyUserInfo); } Send(state.workSocket, (byte)JgS2C_Msgs.kLogin, ans); }
public void OnReadMessage(JStateObject state) { JgC2S_Msgs messageType = (JgC2S_Msgs) state.readBuffer[2];// fromClient[2]; if (LOG_ENABLED) Debug.Log(string.Format("<color=green>Server recieved </color>\n", messageType)); switch (messageType) { case JgC2S_Msgs.kLogin: JThrift.Deserialize(state.readBuffer, Jg.Thrift_Offset, reqLogin); HandleMessage(state, reqLogin); break; case JgC2S_Msgs.kChannelInfo: JThrift.Deserialize(state.readBuffer, Jg.Thrift_Offset, reqChannelInfo); HandleMessage(state, reqChannelInfo); break; case JgC2S_Msgs.kReqMatch: JThrift.Deserialize(state.readBuffer, Jg.Thrift_Offset, reqMatch); HandleMessage(state, reqMatch); break; // Domain of the Room ------------------------------------------------------ default: int roomIndex = state.roomIndex; rooms[roomIndex].OnReadMessage(state); break; } }
void Receive(JStateObject state) { state.bytesRead = 0; state.workSocket.BeginReceive( state.readBuffer, 0, JStateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state); }
//static StateObject mState = new StateObject(); public void AcceptCallback(IAsyncResult ar) { // Signal the main thread to continue. allDone.Set(); // Get the socket that handles the client request. Socket listener = (Socket) ar.AsyncState; Socket handler = listener.EndAccept(ar); //handler.Handle.ToInt32(); Debug.Log("<color=green>S: accepted </color>\n"); // Create the state object. JStateObject newClient = new JStateObject(handler); AddPear(newClient); Receive(newClient); }
void HandleMessage(JStateObject state, ReqMovePawn req) { NtfMovePawn ntf = new NtfMovePawn(); ntf.LocalId = state.indexInRoom; ntf.Location = req.Location; ntf.Target = req.Target; ntf.Dummy = 9999; NotifyMessageToAllClients((byte)JgS2C_Msgs.kNtfMovePawn, ntf); }
void AddPear(JStateObject newClient) { peers.Add(newClient.SocketHandle, newClient); socketsJustDebug.Add(newClient.SocketHandle); }
//public JgNation virtualPlayerNation; public void SetPlayerNationsAndNewMatch() { NtfMatch ntf = new NtfMatch(); bool isCho = false;//(new System.Random()).Next() % 2 == 0; //if (virtualPlayerNation == JgNation.kCho) if (isCho) { choState = states[0]; hanState = states[1]; } else { choState = states[1]; hanState = states[0]; } choState.indexInRoom = 0; hanState.indexInRoom = 1; ntf.LocalId = choState.indexInRoom; SendMessageToClient(choState, (byte)JgS2C_Msgs.kNtfMatch, ntf); ntf.LocalId = hanState.indexInRoom; SendMessageToClient(hanState, (byte)JgS2C_Msgs.kNtfMatch, ntf); }
void RemovePeer(JStateObject client) { bool removed = peers.Remove(client.SocketHandle); socketsJustDebug.Remove(client.SocketHandle); Debug.Log(string.Format("S: removed({0}) : Handle({1}), GSN({2}) \n", removed, client.SocketHandle, client.gsn)); }
private void ConnectCallback(IAsyncResult ar) { try { // Retrieve the socket from the state object. Socket client = (Socket) ar.AsyncState; // Complete the connection. client.EndConnect(ar); mState = new JStateObject(client); Debug.Log(string.Format("C: Socket has connected to {0} \n", client.RemoteEndPoint.ToString())); Read(client, true); } catch (Exception e) { Debug.Log("C: ConnectCallback " + e.ToString()); } }
// public long ParticiantGsn(int index) // { // return participants.Count > index ? participants[index] : -1; // } public void AddParticipant(JStateObject state)// long gsn) { participants.Add(state.gsn); states.Add(state); state.indexInRoom = states.Count - 1; }