// ユーザ情報パケット待ち受け private void Exec_UserInfo(ref ServerChildJob job, byte[] bytes) { UserInfoPacket packet = new UserInfoPacket(); if (WWProtocolV1Helper.GetUserInfoPacket(ref packet, bytes)) { try { job.userName = packet.name; job.userComment = packet.comment; // ユーザ情報をDbに登録 Sqlite3.sqlite3_exec(job.mainDb, "BEGIN", 0, 0, 0); int rc = Sqlite3.sqlite3_exec( job.mainDb, "INSERT INTO user (id, room_id, name, comment) value (" + job.socketID.ToString() + ", " + job.roomID.ToString() + ", " + "'" + job.userName + "', " + "'" + job.userComment + "')", 0, 0, 0); if (rc != Sqlite3.SQLITE_OK) { Exception e = new Exception("sqlite3_exec error."); throw e; } Sqlite3.sqlite3_exec(job.mainDb, "COMMIT", 0, 0, 0); // OKパケットをクライアントへ送信 byte[] okPacket = WWProtocolV1Helper.CreateOkPacket(++job.packetNum); job.sendBuffer.Add(new ArraySegment<byte>(okPacket)); } catch (Exception e) { job.WriteLog(TraceEventType.Error, e.Message); // 途中でエラー発生ならロールバック Sqlite3.sqlite3_exec(job.mainDb, "ROLLBACK", 0, 0, 0); } } }
// ルーム一覧リクエスト private void Exec_ReqRoomList(ref ServerChildJob job, byte[] bytes) { try { List<RoomListPacket> list = new List<RoomListPacket>(); // Dbからルーム一覧を取得 Sqlite3.sqlite3_exec(job.mainDb, "BEGIN", 0, 0, 0); int rc = Sqlite3.sqlite3_exec(job.mainDb, "SELECT * FROM room;", ReqRoomListCallback, list, 0); if (rc != Sqlite3.SQLITE_OK) { Exception e = new Exception("sqlite3_exec error."); throw e; } Sqlite3.sqlite3_exec(job.mainDb, "COMMIT", 0, 0, 0); // 1つでも取得できればクライアントへ送信 if (list.Count > 0) { byte[] beginPacket = WWProtocolV1Helper.CreateBeginRoomListPacket(++job.packetNum); job.sendBuffer.Add(new ArraySegment<byte>(beginPacket)); foreach (RoomListPacket var in list) { byte[] packet = WWProtocolV1Helper.CreateRoomListPacket( ++job.packetNum, var.name, var.comment, var.numMenber, var.maxMenber); job.sendBuffer.Add(new ArraySegment<byte>(packet)); } byte[] endPacket = WWProtocolV1Helper.CreateEndRoomListPacket(++job.packetNum); job.sendBuffer.Add(new ArraySegment<byte>(endPacket)); } } catch (Exception e) { job.WriteLog(TraceEventType.Error, e.Message); // 途中でエラー発生ならロールバック Sqlite3.sqlite3_exec(job.mainDb, "ROLLBACK", 0, 0, 0); } }
// ルーム退室 private void Exec_ByeRoom(ref ServerChildJob job, byte[] bytes) { // 未加入ならすぐ戻る if (job.roomID < 0) { return; } try { Sqlite3.sqlite3_exec(job.mainDb, "BEGIN", 0, 0, 0); int rc = Sqlite3.sqlite3_exec( job.mainDb, "UPDATE user SET room_id = -1 WHERE name = '" + job.roomName + "' ;", 0, 0, 0); if (rc != Sqlite3.SQLITE_OK) { Exception e = new Exception("sqlite3_exec error."); throw e; } Sqlite3.sqlite3_exec(job.mainDb, "COMMIT", 0, 0, 0); job.roomID = -1; job.roomName = ""; // OKパケットをクライアントへ送信 byte[] packet = WWProtocolV1Helper.CreateOkPacket(++job.packetNum); job.sendBuffer.Add(new ArraySegment<byte>(packet)); } catch (Exception e) { job.WriteLog(TraceEventType.Error, e.Message); // 途中でエラー発生ならロールバック Sqlite3.sqlite3_exec(job.mainDb, "ROLLBACK", 0, 0, 0); } }
// ルーム加入 private void Exec_JoinRoom(ref ServerChildJob job, byte[] bytes) { try { String roomName = WWProtocolV1Helper.GetJoinRoomPacket(bytes); List<int> roomIDList = new List<int>(); Sqlite3.sqlite3_exec(job.mainDb, "BEGIN", 0, 0, 0); int rc = Sqlite3.sqlite3_exec(job.mainDb, "SELECT id FROM room WHERE name = '" + roomName + "' ;", GetIDCallback, roomIDList, 0); if (rc != Sqlite3.SQLITE_OK) { Exception e = new Exception("sqlite3_exec error."); throw e; } Sqlite3.sqlite3_exec(job.mainDb, "COMMIT", 0, 0, 0); // ルーム加入処理 if (roomIDList.Count > 0) { job.roomID = roomIDList[0]; job.roomName = roomName; Sqlite3.sqlite3_exec(job.mainDb, "BEGIN", 0, 0, 0); rc = Sqlite3.sqlite3_exec( job.mainDb, "UPDATE user SET room_id = " + job.roomID.ToString() + " WHERE name = '" + job.roomName + "' ;", 0, 0, 0); if (rc != Sqlite3.SQLITE_OK) { Exception e = new Exception("sqlite3_exec error."); throw e; } Sqlite3.sqlite3_exec(job.mainDb, "COMMIT", 0, 0, 0); // OKパケットをクライアントへ送信 byte[] packet = WWProtocolV1Helper.CreateOkPacket(++job.packetNum); job.sendBuffer.Add(new ArraySegment<byte>(packet)); } else { // 見つからなかったのでFailedパケットをクライアントへ送信 byte[] packet = WWProtocolV1Helper.CreateFailedPacket(++job.packetNum); job.sendBuffer.Add(new ArraySegment<byte>(packet)); } } catch (Exception e) { job.WriteLog(TraceEventType.Error, e.Message); // 途中でエラー発生ならロールバック Sqlite3.sqlite3_exec(job.mainDb, "ROLLBACK", 0, 0, 0); } }
// ルーム一覧リクエスト待ち受け private void Exec_WaitReqRoomList(ref ServerChildJob job) { if (job.recvBuffer.Count > 0) { foreach (ArraySegment<byte> seg in job.recvBuffer) { byte[] bytes = seg.Array; if (WWProtocolV1Helper.IsCmdV1(bytes) && WWProtocolV1Helper.GetCmd(bytes) == WWProtocolV1Helper.Cmd.CMD_REQ_ROOM_LIST ) { try { Sqlite3.sqlite3_exec(job.mainDb, "BEGIN", 0, 0, 0); int rc = Sqlite3.sqlite3_exec(job.mainDb, "SELECT * FROM room;", 0, 0, 0); if (rc != Sqlite3.SQLITE_OK) { Exception e = new Exception("sqlite3_exec error."); throw e; } Sqlite3.sqlite3_exec(job.mainDb, "COMMIT", 0, 0, 0); // 処理したのでバッファから削除 job.recvBuffer.Remove(seg); } catch (Exception e) { job.WriteLog(TraceEventType.Error, e.Message); // 途中でエラー発生ならロールバック Sqlite3.sqlite3_exec(job.mainDb, "ROLLBACK", 0, 0, 0); } } } } }